2024-09-07 00:35:16 +00:00
|
|
|
import { config } from '$lib/server/api/common/config'
|
2024-09-04 23:04:41 +00:00
|
|
|
import * as schema from '$lib/server/api/databases/tables'
|
|
|
|
|
import { type NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres'
|
|
|
|
|
import pg from 'pg'
|
|
|
|
|
import { type Disposable, injectable } from 'tsyringe'
|
|
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
|
export class DrizzleService implements Disposable {
|
|
|
|
|
protected readonly pool: pg.Pool
|
|
|
|
|
readonly db: NodePgDatabase<typeof schema>
|
|
|
|
|
readonly schema: typeof schema = schema
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
const pool = new pg.Pool({
|
2024-09-07 00:35:16 +00:00
|
|
|
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,
|
2024-09-07 00:35:16 +00:00
|
|
|
logger: process.env.NODE_ENV === 'development',
|
2024-09-04 23:04:41 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dispose(): Promise<void> | void {
|
|
|
|
|
this.pool.end()
|
|
|
|
|
}
|
|
|
|
|
}
|