diff --git a/src/app.module.ts b/src/app.module.ts index bb41419..0320cd1 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,7 +1,8 @@ import { Module } from "@nestjs/common"; -import { ConfigModule } from "@nestjs/config"; +import { ConfigModule, ConfigService } from "@nestjs/config"; import { LogService } from "./logger/logger.service"; import { ThrottlerModule } from "@nestjs/throttler"; +import { DrizzleModule } from './drizzle/drizzle.module'; @Module({ imports: [ @@ -11,7 +12,8 @@ import { ThrottlerModule } from "@nestjs/throttler"; }]), ConfigModule.forRoot({ isGlobal: true - }) + }), + DrizzleModule ], exports: [LogService], controllers: [], diff --git a/src/drizzle/drizzle.module.ts b/src/drizzle/drizzle.module.ts new file mode 100644 index 0000000..f52a867 --- /dev/null +++ b/src/drizzle/drizzle.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { DrizzleService } from './drizzle.service'; +import { ConfigModule } from "@nestjs/config"; + +@Module({ + imports: [ConfigModule], + providers: [DrizzleService], + exports: [DrizzleService] +}) +export class DrizzleModule {} diff --git a/src/drizzle/drizzle.service.ts b/src/drizzle/drizzle.service.ts new file mode 100644 index 0000000..764e601 --- /dev/null +++ b/src/drizzle/drizzle.service.ts @@ -0,0 +1,56 @@ +// biome-ignore lint/style/useImportType: used by Next.js +import { Injectable, OnModuleInit, OnModuleDestroy } from "@nestjs/common"; +// biome-ignore lint/style/useImportType: used by Next.js +import { ConfigService } from "@nestjs/config"; +import { drizzle } from 'drizzle-orm/postgres-js'; +import { migrate } from 'drizzle-orm/postgres-js/migrator'; +import postgres from "postgres"; + + +@Injectable() +export class DrizzleService implements OnModuleInit, OnModuleDestroy { + private migrationClient: postgres.Sql>; + private standardClient: postgres.Sql>; + + constructor( + private configService: ConfigService + ) { } + + async onModuleInit() { + + const dbConfig = { + host: this.configService.get("POSTGRES_HOST"), + port: Number(this.configService.get("POSTGRES_PORT")), + database: this.configService.get("POSTGRES_DATABASE"), + user: this.configService.get("POSTGRES_USER"), + password: this.configService.get("POSTGRES_PASSWORD"), + ssl: Boolean(this.configService.get("POSTGRES_SSL")), + } + + this.migrationClient = postgres({ + ...dbConfig, + max: 1, + }) + + this.standardClient = postgres({ + ...dbConfig + }) + } + + //onModuleDestroy + async onModuleDestroy() { + await this.migrationClient.end(); + await this.standardClient.end(); + } + + getMigrationClient() { + return migrate(drizzle(this.migrationClient), { + migrationsFolder: "./migrations", + migrationsSchema: "public" + }); + } + + getClient() { + return drizzle(this.standardClient); + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 95f5641..0b907cb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ "outDir": "./dist", "baseUrl": "./", "incremental": true, + "esModuleInterop": true, "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false,