Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kazu728/nestjs-kysely
Kysely module for NestJS
https://github.com/kazu728/nestjs-kysely
kysely nestjs plugin
Last synced: 10 days ago
JSON representation
Kysely module for NestJS
- Host: GitHub
- URL: https://github.com/kazu728/nestjs-kysely
- Owner: kazu728
- License: mit
- Created: 2021-12-02T04:04:06.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-20T08:54:17.000Z (10 months ago)
- Last Synced: 2024-05-20T19:19:44.249Z (9 months ago)
- Topics: kysely, nestjs, plugin
- Language: TypeScript
- Homepage:
- Size: 326 KB
- Stars: 55
- Watchers: 1
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nestjs-kysely
[![npm version](https://badge.fury.io/js/nestjs-kysely.svg)](https://badge.fury.io/js/nestjs-kysely.svg)
[![check](https://github.com/kzmat/nestjs-kysely/actions/workflows/check.yml/badge.svg)](https://github.com/kzmat/nestjs-kysely/actions/workflows/check.yml)
[![codecov](https://codecov.io/gh/kzmat/nestjs-kysely/branch/master/graph/badge.svg?token=5PN87HH33L)](https://codecov.io/gh/kzmat/nestjs-kysely)`nestjs-kysely` implements a module that provides the client of Kysely, which is a type-safe query builder.
## install
```
yarn add nestjs-kysely kysely
```## Example
```
yarn add nestjs-kysely kysely mysql2
```Register KyselyModule for your app.
```ts
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.
```ts
import { Controller, Get } from "@nestjs/common";
import { InjectKysely } from "nestjs-kysely";
import { Kysely } from 'kysely';
import { DB } from "./@types";@Controller()
export class AppController {
constructor(@InjectKysely() private readonly db: Kysely) {}@Get()
async getHello(): Promise {
const result = await this.db
.selectFrom("person")
.innerJoin("pet", "pet.owner_id", "person.id")
.selectAll()
.execute();return JSON.stringify(result);
}
}
```# Multiple namespace
Register KyselyModule with multiple DBs for your app.
## Example forRoot
```ts
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 {}
```## Example forRootAsync
```ts
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 {}
```## Use it
You can then inject the Kysely client into any of your injectables by using a custom decorator with namespace as input.
```ts
import { Controller, Get } from "@nestjs/common";
import { InjectKysely } from "nestjs-kysely";
import { Kysely } from 'kysely';
import { DB } from "./@types";@Controller()
export class AppController {
constructor(
@InjectKysely('mysql') private readonly mysql: Kysely,
@InjectKysely('sqlite') private readonly sqlite: Kysely,
) {}@Get()
async getHello(): Promise {
const result = await this.db
.selectFrom("person")
.innerJoin("pet", "pet.owner_id", "person.id")
.selectAll()
.execute();return JSON.stringify(result);
}
}
```# License
MIT