boredgame/src/lib/server/api/services/drizzle.service.ts

34 lines
888 B
TypeScript
Raw Normal View History

2024-09-04 23:04:41 +00:00
import { type NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres'
import pg from 'pg'
import { type Disposable, injectable } from 'tsyringe'
import { config } from '../common/config'
import * as schema from '../databases/tables'
2024-09-04 23:04:41 +00:00
@injectable()
export class DrizzleService implements Disposable {
protected readonly pool: pg.Pool
db: NodePgDatabase<typeof schema>
2024-09-04 23:04:41 +00:00
readonly schema: typeof schema = schema
constructor() {
const pool = new pg.Pool({
user: config.postgres.user,
password: config.postgres.password,
host: config.postgres.host,
port: Number(config.postgres.port).valueOf(),
database: config.postgres.database,
ssl: config.postgres.ssl,
max: config.postgres.max,
2024-09-04 23:04:41 +00:00
})
this.pool = pool
this.db = drizzle(pool, {
schema,
logger: process.env.NODE_ENV === 'development',
2024-09-04 23:04:41 +00:00
})
}
dispose(): Promise<void> | void {
this.pool.end()
}
}