| 1 | import { Injectable, OnModuleDestroy } from '@nestjs/common'; |
| 2 | import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres'; |
| 3 | import { Pool } from 'pg'; |
| 4 | import * as schema from './schema'; |
| 5 | |
| 6 | @Injectable() |
| 7 | export class DatabaseService implements OnModuleDestroy { |
| 8 | private pool: Pool; |
| 9 | public db: NodePgDatabase<typeof schema>; |
| 10 | |
| 11 | constructor() { |
| 12 | const connectionString = |
| 13 | process.env.DATABASE_URL || |
| 14 | 'postgresql://eventflowapp:eventflow_password@localhost:5432/eventflowapp?schema=public'; |
| 15 | |
| 16 | this.pool = new Pool({ connectionString }); |
| 17 | this.db = drizzle(this.pool, { schema }); |
| 18 | |
| 19 | console.log('Database connected'); |
| 20 | } |
| 21 | |
| 22 | async onModuleDestroy() { |
| 23 | await this.pool.end(); |
| 24 | } |
| 25 | |
| 26 | get schema() { |
| 27 | return schema; |
| 28 | } |
| 29 | } |