@sqb/nestjs
v6.8.0
Published
Nestjs module for data connection using SQB
Readme
@sqb/nestjs
Nestjs module for data connection using SQB.
[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![CI Tests][ci-test-image]][ci-test-url] [![Test Coverage][coveralls-image]][coveralls-url]
About SQB
SQB is an extensible, multi-dialect SQL query builder and Database connection wrapper for NodeJS.
About @sqb/nestjs
@sqb/nestjs wires an @sqb/connect SqbClient into a NestJS application as an
injectable, application-scoped provider. SqbModule.forRoot() accepts connection options directly
(or falls back to SQB_* environment variables — see below), while SqbModule.forRootAsync()
resolves them from a factory, e.g. NestJS's ConfigService. Either way, the module opens the
connection on onApplicationBootstrap and closes it gracefully on onApplicationShutdown.
import { Module } from '@nestjs/common';
import { SqbModule } from '@sqb/nestjs';
@Module({
imports: [
SqbModule.forRoot({
useValue: {
dialect: 'postgres',
host: 'localhost',
database: 'mydb',
},
}),
],
})
export class AppModule {}The client is provided under the SqbClient token by default (override it with token in the
module options if you need to register more than one connection), so it can be injected like any
other NestJS provider:
import { Injectable } from '@nestjs/common';
import { SqbClient } from '@sqb/connect';
@Injectable()
export class UsersService {
constructor(private readonly client: SqbClient) {}
findAll() {
return this.client.getRepository('User').findMany();
}
}For async, factory-based configuration:
SqbModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
dialect: 'postgres',
host: config.get('DB_HOST'),
database: config.get('DB_NAME'),
}),
});Installation
$ npm install @sqb/nestjs --saveNode Compatibility
- Node.js 22 or later.
Environment Variables
The library supports configuration through environment variables. Environment variables below is accepted. All environment variables starts with prefix (SQB_). This can be configured while registering the module.
