feat: add Drizzle module and service

Implement Drizzle service and integration in the project. A new Drizzle module was added to the nest app. The Drizzle service also includes configuration for postgres database and management of migration client. An adjustment was also made to tsconfig.json to enable ES module interoperation.
This commit is contained in:
Mathis H (Avnyr) 2024-07-09 14:08:03 +02:00
parent 1971a0dcfd
commit e575b669ce
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
4 changed files with 71 additions and 2 deletions

View File

@ -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: [],

View File

@ -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 {}

View File

@ -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<Record<string, postgres.PostgresType>>;
private standardClient: postgres.Sql<Record<string, postgres.PostgresType>>;
constructor(
private configService: ConfigService
) { }
async onModuleInit() {
const dbConfig = {
host: this.configService.get<string>("POSTGRES_HOST"),
port: Number(this.configService.get<string>("POSTGRES_PORT")),
database: this.configService.get<string>("POSTGRES_DATABASE"),
user: this.configService.get<string>("POSTGRES_USER"),
password: this.configService.get<string>("POSTGRES_PASSWORD"),
ssl: Boolean(this.configService.get<string>("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);
}
}

View File

@ -11,6 +11,7 @@
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,