|
| 1 | +import type { PoolClient } from 'pg'; |
| 2 | +import { sql, type DatabasePool, type DatabasePoolConnection } from 'slonik'; |
| 3 | +import { raw } from 'slonik-sql-tag-raw'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Bridge {slonik.DatabasePool} to an {pg.Pool} for usage with Postgraphile Workers. |
| 7 | + * |
| 8 | + * This is a very very pragmatic approach, since slonik moved away from using {pg.Pool} internally. |
| 9 | + * https://github.com/gajus/slonik/issues/768 |
| 10 | + **/ |
| 11 | +export class PgPoolBridge { |
| 12 | + constructor(private pool: DatabasePool) {} |
| 13 | + |
| 14 | + end(): never { |
| 15 | + throw new Error('Not implemented.'); |
| 16 | + } |
| 17 | + |
| 18 | + async connect(): Promise<PoolClient> { |
| 19 | + const pgClientAvailableP = Promise.withResolvers<any>(); |
| 20 | + const pgClientReleasedP = Promise.withResolvers<void>(); |
| 21 | + |
| 22 | + // slonik connect works in a way where the client is acquired for the callback handler closure only. |
| 23 | + // It is released once the Promise returned from the handler resolves. |
| 24 | + // We need to be a bit creative to support the "pg.Pool" API and obviously |
| 25 | + // trust graphile-workers to call the `release` method on our fake {pg.Client} :) |
| 26 | + // so the client is released back to the pool |
| 27 | + void this.pool.connect(async client => { |
| 28 | + pgClientAvailableP.resolve(new PgClientBridge(client, pgClientReleasedP.resolve)); |
| 29 | + await pgClientReleasedP.promise; |
| 30 | + }); |
| 31 | + |
| 32 | + return pgClientAvailableP.promise; |
| 33 | + } |
| 34 | + |
| 35 | + /** Some of graphile-workers logic just calls the `query` method on {pg.Pool} - without first acquiring a connection. */ |
| 36 | + query(query: unknown, values?: unknown, callback?: unknown): any { |
| 37 | + // not used, but just in case so we can catch it in the future... |
| 38 | + if (typeof callback !== 'undefined') { |
| 39 | + throw new Error('PgClientBridge.query: callback not supported'); |
| 40 | + } |
| 41 | + |
| 42 | + if ((typeof query !== 'string' && typeof query !== 'object') || !query) { |
| 43 | + throw new Error('PgClientBridge.query: unsupported query input'); |
| 44 | + } |
| 45 | + |
| 46 | + if (typeof query === 'string') { |
| 47 | + return this.pool.query(sql.unsafe`${raw(query, values as any)}`); |
| 48 | + } |
| 49 | + |
| 50 | + return this.pool.query(sql.unsafe`${raw((query as any).text as any, (query as any).values)}`); |
| 51 | + } |
| 52 | + |
| 53 | + on(): this { |
| 54 | + // Note: we can skip setting up event handlers, as graphile workers is only setting up error handlers to avoid uncaught exceptions |
| 55 | + // For us, the error handlers are already set up by slonik |
| 56 | + // https://github.com/graphile/worker/blob/5650fbc4406fa3ce197b2ab582e08fd20974e50c/src/lib.ts#L351-L359 |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + removeListener(): this { |
| 61 | + // Note: we can skip tearing down event handlers, since we ship setting them up in the first place |
| 62 | + // https://github.com/graphile/worker/blob/5650fbc4406fa3ce197b2ab582e08fd20974e50c/src/lib.ts#L351-L359 |
| 63 | + return this; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class PgClientBridge { |
| 68 | + constructor( |
| 69 | + private connection: DatabasePoolConnection, |
| 70 | + /** This is invoked for again releasing the connection. */ |
| 71 | + public release: () => void, |
| 72 | + ) {} |
| 73 | + |
| 74 | + query(query: unknown, values?: unknown, callback?: unknown): any { |
| 75 | + if (typeof callback !== 'undefined') { |
| 76 | + throw new Error('PgClientBridge.query: callback not supported'); |
| 77 | + } |
| 78 | + |
| 79 | + if ((typeof query !== 'string' && typeof query !== 'object') || !query) { |
| 80 | + throw new Error('PgClientBridge.query: unsupported query input'); |
| 81 | + } |
| 82 | + |
| 83 | + if (typeof query === 'string') { |
| 84 | + return this.connection.query(sql.unsafe`${raw(query, values as any)}`); |
| 85 | + } |
| 86 | + |
| 87 | + return this.connection.query( |
| 88 | + sql.unsafe`${raw((query as any).text as any, (query as any).values)}`, |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments