nestjs-kysely
implements a module that provides the client of Kysely, which is a type-safe query builder.
yarn add nestjs-kysely kysely
yarn add nestjs-kysely kysely mysql2
Register KyselyModule for your app.
import { Module } from "@nestjs/common";
import { MysqlDialect } from "kysely";
import { createPool } from "mysql2";
import { KyselyModule } from "nestjs-kysely";
import { AppController } from "./app.controller";
@Module({
imports: [
KyselyModule.forRoot({
dialect: new MysqlDialect({
pool: createPool({
host: "127.0.0.1",
user: "root",
password: "password",
database: "kysely_test",
}),
}),
}),
],
controllers: [AppController],
})
export class AppModule {}
You can then inject the Kysely client into any of your injectables by using a custom decorator.
import { Controller, Get } from "@nestjs/common";
import { InjectKysely } from "nestjs-kysely";
import { DB } from "./@types";
@Controller()
export class AppController {
constructor(@InjectKysely() private readonly db: DB) {}
@Get()
async getHello(): Promise<string> {
const result = await this.db
.selectFrom("person")
.innerJoin("pet", "pet.owner_id", "person.id")
.selectAll()
.execute();
return JSON.stringify(result);
}
}
Register KyselyModule with multiple DBs for your app.
import { Module } from "@nestjs/common";
import { MysqlDialect, SqliteDialect } from "kysely";
import { createPool } from "mysql2";
import Database from "better-sqlite3";
import { KyselyModule } from "nestjs-kysely";
import { AppController } from "./app.controller";
@Module({
imports: [
KyselyModule.forRoot([
{
namespace: 'mysql',
dialect: new MysqlDialect({
pool: createPool({
host: "127.0.0.1",
user: "root",
password: "password",
database: "kysely_test",
}),
}),
},
{
namespace: 'sqlite',
dialect: new SqliteDialect({
database: new Database(),
}),
}
]),
],
controllers: [AppController],
})
export class AppModule {}
import { Module } from "@nestjs/common";
import { MysqlDialect, SqliteDialect } from "kysely";
import { createPool } from "mysql2";
import Database from "better-sqlite3";
import { KyselyModule } from "nestjs-kysely";
import { AppController } from "./app.controller";
@Module({
imports: [
KyselyModule.forRootAsync({
namespace: 'mysql',
useFactory: () => (
{
dialect: new MysqlDialect({
pool: createPool({
host: "127.0.0.1",
user: "root",
password: "password",
database: "kysely_test",
}),
}),
}
)
}),
KyselyModule.forRootAsync({
namespace: 'sqlite',
useFactory: () => (
{
dialect: new SqliteDialect({
database: new Database(),
}),
}
)
}),
],
controllers: [AppController],
})
export class AppModule {}
You can then inject the Kysely client into any of your injectables by using a custom decorator with namespace as input.
import { Controller, Get } from "@nestjs/common";
import { InjectKysely } from "nestjs-kysely";
import { DB } from "./@types";
@Controller()
export class AppController {
constructor(
@InjectKysely('mysql') private readonly mysql: Kysely<unknown>,
@InjectKysely('sqlite') private readonly sqlite: Kysely<unknown>,
) {}
@Get()
async getHello(): Promise<string> {
const result = await this.db
.selectFrom("person")
.innerJoin("pet", "pet.owner_id", "person.id")
.selectAll()
.execute();
return JSON.stringify(result);
}
}
MIT