1.0.2 • Published 2 years ago

nestjs-knexjs v1.0.2

Weekly downloads
96
License
MIT
Repository
github
Last release
2 years ago

Why?

We love Nestjs framework and Knex.js SQL Builder, so, I decided to create this package to abstract the use of Knex.js with Nestjs, which uses TypeScript.

Installation

To install this generated project:

$ npm install nestjs-knexjs knex

Install one of the following wich you will use

$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install oracledb
$ npm install mssql

(or yarn equivalent)

Usage

First of all inside src/nestjs-knex-client folder I've created a nestjs application example using this package, with good pratices, as using dotenv, and knex configuration to each environment. So, check this out!

  1. Import NestjsKnexModule and setup inside core module of your application:

        @Module({
            imports: [NestjsKnexModule.register({
                client: 'pg',
                connection: {
                    host: 'host',
                    user: 'user',
                    password: 'pass',
                    database: 'database',
                    port: 5432,
                },
            })],
        })
        export class AppModule { }
  2. Now use NestjsKnexService inside your service constructor to provide Knex connection:

        private readonly knex: Knex = null;
    
        constructor(private nestjsKnexService: NestjsKnexService) {
            this.knex = this.nestjsKnexService.getKnexConnection();
        }
    
        async getAll() {
            return await this.knex('your-table').select('*');
        }

    Or

        constructor(@Inject(KNEX_CONNECTION) private readonly knex: Knex) { }
    
        async getAll() {
            return await this.knex('your-table').select('*');
        }

Contributing

All contributions are welcome. Use the pull request as a way to contribute to the evolution of the package. My intention is over time to add pipelines with github actions as a way to facilitate contributions.

Contribution Guidelines

This package is configured with commitlint, so, to contribute you have to know about the rules of commitlint

Author

Felipe Bueno