0.0.5 • Published 3 years ago

@cmelgarejo/nestjsknexjs v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

nestjsknexjs

nestjsknexjs

Description

Knexjs module for Nest framework (node.js)

Installation

First install the module via yarn or npm and do not forget to install the driver package as well:

    npm i --save nestjsknexjs knex mysql   # for mysql/mariadb
    npm i --save nestjsknexjs knex pg      # for postgresql
    npm i --save nestjsknexjs knex sqlite  # for sqlite

or

    yarn add nestjsknexjs knex mysql   # for mysql/mariadb
    yarn add nestjsknexjs knex pg      # for postgresql
    yarn add nestjsknexjs knex sqlite  # for sqlite

Table of Contents

Usage

KnexModule

KnexModule is the primary entry point for this package and can be used synchronously

@Module({
  imports: [
    KnexModule.forRoot({
      config: {
        client: 'pg',
        connection: {
          host: '127.0.0.1',
          port: 5432,
          user: 'postgres',
          password: 'pgsql',
          database: 'test',
        },
      },
    }),
  ],
})

or asynchronously

@Module({
  imports: [
    KnexModule.forRootAsync({
      useFactory: () => ({
        config: {
          client: 'pg',
          connection: {
            host: '127.0.0.1',
            port: 5432,
            user: 'postgres',
            password: 'pgsql',
            database: 'test',
          },
        },
      }),
    }),
  ],
})

Example of use

UsersService:

import { Injectable } from '@nestjs/common';
import Knex from 'knex';
import { InjectModel } from 'nestjsknexjs';

@Injectable()
export class UsersService {
  constructor(@InjectModel() private readonly knex: Knex) {}

  async findAll() {
    const users = await this.knex.table('users');
    return { users };
  }
}

UsersController:

import { Controller, Get } from '@nestjs/common';
import { UsersService } from './users.service';

@Controller()
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Get()
  async getHello() {
    return await this.usersService.findAll();
  }
}

Create Migrations

  npx knex init
  npx knex migrate:make [name_migrations]

Example file migrations:

exports.up = function(knex) {
  return knex.schema
    .createTable('users', function(table) {
      table.increments('id');
      table.string('first_name', 255).notNullable();
      table.string('last_name', 255).notNullable();
    })
    .createTable('products', function(table) {
      table.increments('id');
      table.string('name', 255).notNullable();
      table.decimal('price').notNullable();
    });
};

exports.down = function (knex) {
  return knex.schema.dropTable('products').dropTable('users');
};

Run Migrations

In knexfile.js update with your config settings:

module.exports = {
  development: {
    client: 'mysql',
    connection: {
      host: '127.0.0.1',
      user: 'root',
      password: 'root',
      database: 'test',
    },
  },
}

then we run the following command from the terminal

  npx knex migrate:latest

Create Seeds

  npx knex seed:make [name_seed]

Example file seeds

exports.seed = function (knex) {
  return knex('users')
    .del()
    .then(function () {
      return knex('users').insert([
        { id: 1, first_name: 'firstname1', last_name: 'lastname1' },
        { id: 2, first_name: 'firstname2', last_name: 'lastname2' },
        { id: 3, first_name: 'firstname3', last_name: 'lastname3' },
      ]);
    });
};

Run Seeds

  npx knex seed:run

For more information on Knex.js see here

Test locally

Run migration and tests

  cd tests; npx knex migrate:latest; npx knex seed:run; cd ..; npm run test:e2e

Contribute

Feel free to help this library, I'm quite busy with also another Nestjs packages, but the community will appreciate the effort of improving this library. Make sure you follow the guidelines

Stay in touch

License

MIT licensed

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago