|
| 1 | +import { |
| 2 | + type DBAdapter, |
| 3 | + type PgPrimitive, |
| 4 | + type ExecuteOptions, |
| 5 | +} from '@cardstack/runtime-common'; |
| 6 | +import migrate from 'node-pg-migrate'; |
| 7 | +import { join } from 'path'; |
| 8 | +import { Pool, Client } from 'pg'; |
| 9 | + |
| 10 | +import postgresConfig from './pg-config'; |
| 11 | + |
| 12 | +function config() { |
| 13 | + return postgresConfig({ |
| 14 | + database: 'boxel', |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +export default class PgAdapter implements DBAdapter { |
| 19 | + private pool: Pool; |
| 20 | + |
| 21 | + constructor() { |
| 22 | + let { user, host, database, password, port } = config(); |
| 23 | + console.log(`connecting to DB ${user}@${host}:${port}/${database}`); |
| 24 | + this.pool = new Pool({ |
| 25 | + user, |
| 26 | + host, |
| 27 | + database, |
| 28 | + password, |
| 29 | + port, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + async startClient() { |
| 34 | + await this.migrateDb(); |
| 35 | + } |
| 36 | + |
| 37 | + async close() { |
| 38 | + if (this.pool) { |
| 39 | + await this.pool.end(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + async execute( |
| 44 | + sql: string, |
| 45 | + opts?: ExecuteOptions, |
| 46 | + ): Promise<Record<string, PgPrimitive>[]> { |
| 47 | + let client = await this.pool.connect(); |
| 48 | + try { |
| 49 | + let { rows } = await client.query({ text: sql, values: opts?.bind }); |
| 50 | + return rows; |
| 51 | + } catch (e: any) { |
| 52 | + console.error( |
| 53 | + `Error executing SQL ${e.result.message}:\n${sql}${ |
| 54 | + opts?.bind ? ' with bindings: ' + JSON.stringify(opts?.bind) : '' |
| 55 | + }`, |
| 56 | + e, |
| 57 | + ); |
| 58 | + throw e; |
| 59 | + } finally { |
| 60 | + client.release(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private async migrateDb() { |
| 65 | + const config = postgresConfig(); |
| 66 | + let client = new Client( |
| 67 | + Object.assign({}, config, { database: 'postgres' }), |
| 68 | + ); |
| 69 | + try { |
| 70 | + await client.connect(); |
| 71 | + let response = await client.query( |
| 72 | + `select count(*)=1 as has_database from pg_database where datname=$1`, |
| 73 | + [config.database], |
| 74 | + ); |
| 75 | + if (!response.rows[0].has_database) { |
| 76 | + await client.query(`create database ${config.database}`); |
| 77 | + } |
| 78 | + } finally { |
| 79 | + client.end(); |
| 80 | + } |
| 81 | + |
| 82 | + await migrate({ |
| 83 | + direction: 'up', |
| 84 | + migrationsTable: 'migrations', |
| 85 | + singleTransaction: true, |
| 86 | + checkOrder: false, |
| 87 | + databaseUrl: { |
| 88 | + user: config.user, |
| 89 | + host: config.host, |
| 90 | + database: config.database, |
| 91 | + password: config.password, |
| 92 | + port: config.port, |
| 93 | + }, |
| 94 | + count: Infinity, |
| 95 | + dir: join(__dirname, 'migrations'), |
| 96 | + ignorePattern: '.*\\.eslintrc\\.js', |
| 97 | + log: (...args) => console.log(...args), |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
0 commit comments