# nestjs-knex-module

> This is a dynamic module to use [Knex](https://knexjs.org/) in a Nestjs application. It follows [this guide](https://dev.to/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an), but has some minimal changes regardi

Latest version **0.1.2** (published 2021-11-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-knex-module
pnpm add nestjs-knex-module
yarn add nestjs-knex-module
bun add nestjs-knex-module
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2021-11-09 |
| First published | 2021-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | lostlizard |
| Keywords | nestjs |

## Links

- npm: https://www.npmjs.com/package/nestjs-knex-module
- npm.io page: https://npm.io/package/nestjs-knex-module

## Dependencies (2)

- [pg](https://npm.io/package/pg.md) ^8.7.1
- [knex](https://npm.io/package/knex.md) ^0.95.13

## Recent versions

- 0.1.2 (latest) — 2021-11-09
- 0.1.1 — 2021-11-07
- 0.1.0 — 2021-11-07

## README

# Nestjs Knex Module
This is a dynamic module to use [Knex](https://knexjs.org/) in a Nestjs application.
It follows [this guide](https://dev.to/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an), but has some minimal changes regarding typings.

It is a dynamic module, so it can be configured like this:
```typescript
@Module({
  imports: [KnexModule.register({
    client: 'pg',
    connection: {
      host: 'localhost',
      user: 'myuser',
      password: 'mypassword',
      database: 'mydb',
      port: 5432,
    }
  })],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

Alternatively you can also create a file `knexfile.ts`:
```ts
import { Knex } from 'knex';

let KnexConfig: Knex.Config = {
  client: 'pg',
  connection: {
    host: 'localhost',
    user: 'myuser',
    password: 'mypassword',
    database: 'mydb',
    port: 5432,
  },
  migrations: {
    directory: 'src/migrations',
    tableName: 'migrations',
    extension: 'ts'
  },
  seeds: {
    directory: 'src/seeders',
    extension: 'ts'
  }
};
export default KnexConfig
```

And import this file in your module:
```ts
import KnexConfig from '../knexfile';

@Module({
  imports: [
    KnexModule.register({...KnexConfig}),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

Then you can inject Knex in your services:
```ts
@Injectable()
export class UsersService {
  constructor(@Inject(KNEX_CONNECTION) private readonly knex: Knex) {}

  getAll() {
    return this.knex<User>('users').select(['*']);
  }
}
```
<!-- <h1 align="center"></h1>

<div align="center">
  <a href="http://nestjs.com/" target="_blank">
    <img src="https://nestjs.com/img/logo_text.svg" width="150" alt="Nest Logo" />
  </a>
</div>

<h3 align="center">Knex -- generated by @nestjsplus/dyn-schematics</h3>

<div align="center">
  <a href="https://nestjs.com" target="_blank">
    <img src="https://img.shields.io/badge/built%20with-NestJs-red.svg" alt="Built with NestJS">
  </a>
  <a href="https://github.com/nestjsplus/dyn-schematics" target="_blank">
    <img src="https://img.shields.io/badge/Built%20with-%40nestjsplus%2Fdyn--schematics-brightgreen" alt="Built with @nestjsplus/dyn-schematics">
  </a>
</div>

This dynamic module was generated with [Nest Dynamic Package Generator Schematics](https://github.com/nestjsplus/dyn-schematics).  You can read more about using the generator [here](https://github.com/nestjsplus/dyn-schematics).

### Installation

To install this generated project:

```bash
npm install
```

(or yarn equivalent)

### Running

If you selected `yes` for the question `Generate a testing client?`, a small testing module was automatically generated
called KnexClientModule.  You can test that the template was properly generated by running

```bash
npm run start:dev
```

Then connect to [http://localhost:3000](http://localhost:3000).

### Customizing

The files in the project have comments that should help guide you.

You can also refer to [this article](https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370) for details on the concepts behind this module pattern.

You can read more about using the generator [here](https://github.com/nestjsplus/dyn-schematics).

### About @nestjsplus/dyn-schematics

[Nest Dynamic Package Generator Schematics](https://github.com/nestjsplus/dyn-schematics) generates a starter template for building NestJS dynamic packages.  It uses the `@nestjs/cli` core package, and provides customized schematics for generating modular NestJS applications.  See [here](https://github.com/nestjsplus/dyn-schematics) for the full set of available schematics, and documentation. -->

---
_Source: https://npm.io/package/nestjs-knex-module · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
