import { Injectable, Scope } from '@nestjs/common';
import { Pool, PoolConfig } from 'pg';
import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres';
import { ConfigService } from '@nestjs/config';
import { sql } from 'drizzle-orm';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { join } from 'path';
@Injectable({
scope: Scope.TRANSIENT,
})
export class DrizzleService {
private pool: Pool;
private db: NodePgDatabase;
constructor(private configService: ConfigService) {
void this.connect();
}
private async connect(): Promise<void> {
const poolConfig = {
connectionString: this.configService.getOrThrow('database.databaseUrl'),
ssl: false,
keepAlive: true,
max: this.configService.get('database.maxConnections') ?? 1,
};
this.pool = new Pool(poolConfig);
this.db = drizzle(this.pool, {
logger: true,
});
try {
await migrate(this.db, {
migrationsFolder: join(__dirname, '/migrations'),
});
} catch (error) {
console.log(error, '-=-=-=-=-=-=->>');
}
}
import { Injectable, Scope } from '@nestjs/common';
import { Pool, PoolConfig } from 'pg';
import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres';
import { ConfigService } from '@nestjs/config';
import { sql } from 'drizzle-orm';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { join } from 'path';
@Injectable({
scope: Scope.TRANSIENT,
})
export class DrizzleService {
private pool: Pool;
private db: NodePgDatabase;
constructor(private configService: ConfigService) {
void this.connect();
}
private async connect(): Promise<void> {
const poolConfig = {
connectionString: this.configService.getOrThrow('database.databaseUrl'),
ssl: false,
keepAlive: true,
max: this.configService.get('database.maxConnections') ?? 1,
};
this.pool = new Pool(poolConfig);
this.db = drizzle(this.pool, {
logger: true,
});
try {
await migrate(this.db, {
migrationsFolder: join(__dirname, '/migrations'),
});
} catch (error) {
console.log(error, '-=-=-=-=-=-=->>');
}
}