@brandonnpm2/nestjs-knex
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

@brandongit2/nestjs-knex

A NestJS module for using Knex.js.

Installation

yarn add @brandonnpm2/nestjs-knex knex

Examples

KnexModule.register(config)

import {KnexModule} from "@brandonnpm2/nestjs-knex"
import {Module} from "@nestjs/common"

import {AppController} from "./app.controller"

@Module({
  imports: [
    KnexModule.register({
      client: `pg`,
      connection: `...`,
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

InjectKnex()

import {InjectKnex} from "@brandonnpm2/nestjs-knex"
import {Controller, Get} from "@nestjs/common"

import type Knex from "knex"

@Controller()
export class AppController {
  constructor(@InjectKnex() private readonly knex: Knex) {}

  @Get()
  async getUser(email: string) {
    const user = await this.knex.table("users").first(`*`).where({email: "user@example.com"})
    return user
  }
}

Mocking for tests

import {KNEX_TOKEN, KnexModule} from "@brandonnpm2/nestjs-redis"
import {Test} from "@nestjs/testing"

import type Knex from "knex"

describe(`test`, () => {
  let knex: Knex

  beforeAll(async () => {
    const module = await Test.createTestingModule({
      imports: [
        KnexModule.register({
          client: `pg`,
          connection: `...`,
        }),
      ],
    })

    knex = module.get(KNEX_TOKEN)
  })

  beforeEach(async () => {
    await knex.raw(`start transaction`)
  })

  afterEach(async () => {
    await knex.raw(`rollback`)
  })

  test(`basic test`, async () => {
    const AMY = {
      id: 0,
      firstName: `Amy`,
      lastName: `Brown`,
      age: 29,
    }

    await knex.table(`users`).insert(AMY)

    const res = await knex.table(`users`).first(`*`).where({id: AMY.id})
    expect(res).toMatchObject(AMY)
  })
})

Readme

Keywords

none

Package Sidebar

Install

npm i @brandonnpm2/nestjs-knex

Weekly Downloads

1

Version

2.1.0

License

MIT

Unpacked Size

9.31 kB

Total Files

12

Last publish

Collaborators

  • brandonnpm2