import { Kysely, Migrator, PostgresDialect, FileMigrationProvider } from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js' /// // @deno-types="npm:@types/pg" import { Pool } from "npm:pg"; import { Database } from "./scheme.ts" import { CompiledConfig } from "../cfg/config.ts"; import { promises as fs } from 'node:fs' import * as path from 'node:path' export type SetupDbOutput = { pool: Pool, db: Kysely, } export const setupDB = async (cfg: CompiledConfig): Promise => { const pool = new Pool(cfg.pgPoolCfg) const dialect = new PostgresDialect({ pool }) const db = new Kysely({ dialect }) if (cfg.migrationCfg && cfg.migrationCfg.applyMigrations) { const migrator = new Migrator({ db, provider: new FileMigrationProvider({ fs, path, migrationFolder: cfg.migrationCfg.migrationsPath, }), allowUnorderedMigrations: true }) await migrator.migrateToLatest() } return { pool, db } }