Skip to content

quazex/nestjs-knex

Repository files navigation

NestJS Knex Module

Core features:

Installation

To install the package, run:

npm install @quazex/nestjs-knex knex

Usage

Importing the Module

To use the Knex module in your NestJS application, import it into your root module (e.g., AppModule).

import { Module } from '@nestjs/common';
import { KnexModule } from '@quazex/nestjs-knex';

@Module({
    imports: [
        KnexModule.forRoot({
            client: 'pg',
            connection: {
                host: 'localhost',
                port: 5432,
                user: 'test',
                password: 'test',
                database: 'test',
            },
        }),
    ],
})
export class AppModule {}

Using Knex Service

Once the module is registered, you can inject instance of the Knex into your providers:

import { Injectable } from '@nestjs/common';
import { InjectKnex } from '@quazex/nestjs-knex';
import { Knex } from 'knex';

@Injectable()
export class DatabaseService {
    constructor(@InjectKnex() private readonly client: Knex) {}

    async insert(document: object) {
        await this.client('table').insert(document);
    }

    async select(id: string) {
        return await this.client('table').select('*').where({ id }).first();
    }
}

Async Configuration

If you need dynamic configuration, use forRootAsync:

import { Module } from '@nestjs/common';
import { KnexModule } from '@quazex/nestjs-knex';

@Module({
    imports: [
        KnexModule.forRootAsync({
            useFactory: async(config: SomeConfigProvider) => ({
                client: 'pg',
                connection: {
                    host: config.PG_HOST,
                    port: config.PG_PORT,
                    user: config.PG_USER,
                    password: config.PG_PASSWORD,
                    database: config.PG_DATABASE,
                },
            }),
            inject: [
                SomeConfigProvider,
            ],
        }),
    ],
})
export class AppModule {}

Connection and graceful shutdown

By default, this module doesn't manage client connection on application bootstrap or shutdown. You can read more about lifecycle hooks on the NestJS documentation page.

// main.ts
const app = await NestFactory.create(AppModule);

// Starts listening for shutdown hooks
app.enableShutdownHooks(); // <<<

await app.listen(process.env.PORT ?? 3000);
// app.bootstrap.ts
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
import { InjectKnex } from '@quazex/nestjs-knex';
import { Knex } from 'knex';

@Injectable()
export class AppBootstrap implements OnApplicationShutdown {
    constructor(@InjectKnex() private readonly client: Knex) {}

    public async onApplicationShutdown(): Promise<void> {
        await this.client.destroy();
    }
}

License

MIT

About

NestJS module for Knex library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •