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