1.0.1 • Published 10 months ago

nestely v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

build-deploy License: MIT Coverage Status npm type definitions current-version

Table of Contents

Table of contents generated with markdown-toc

Nestely

About

Nestely is a NestJs module that integrates Kysely into your NestJs application.

What else does this module do?

Nothing else really. It just provides a way to inject the Kysely instance into your services/controllers/repositories. I created this module just to avoid the same boilerplate code in every NestJs project I create.

This project has been tested with

  • Kysely 0.24
  • SQLite3

Installation

Install the latest version of Nestely using npm:

npm install nestely

NestJs Configuration

In order to configure the module with your NestJs application, you need to import the NestelyModule into your AppModule (or any other module).

import { Module } from '@nestjs/common';
import { NestelyModule } from 'nestely';

@Module({
  imports: [
    NestelyModule.register({
      plugins: [new CamelCasePlugin()],
      dialect: new SqliteDialect({
        database: new SQLite(':memory:'),
      }),
      isGlobal: true,
    }),
    // Asynchronous configuration
    NestelyModule.registerAsync({
      useFactory: () => ({
        dialect: new SqliteDialect({
          database: new SQLite(':memory:'),
        }),
        plugins: [new CamelCasePlugin()],
      }),
      isGlobal: false, // default is true
    }),
  ],
})
export class AppModule {}

Using the Kysely instance

Once the module is configured, you can inject the Kysely instance into your services/controllers/repositories using the @InjectKysely() decorator.

import { Injectable } from '@nestjs/common';
import { InjectKysely, Kysely } from 'nestely';
import { DB } from './db.interface';

@Injectable()
export class AppService {
  // provide the DB interface
  constructor(@InjectKysely() private readonly kysely: Kysely<DB>) {}

  async findAll(): Promise<void> {
    const result = await sql`select 1+1 as result`.execute(this.kysely);
    console.log(result); // { rows: [ { result: 2 } ] }
  }
}