|
| 1 | +import type {CompiledQuery, DatabaseConnection, QueryResult, TransactionSettings} from 'kysely' |
| 2 | +import type {Sql} from 'postgres' |
| 3 | + |
| 4 | +import {PostgresJSDialectError} from './errors.js' |
| 5 | +import type {PostgresJSDialectConfig} from './types.js' |
| 6 | +import {freeze} from './utils.js' |
| 7 | + |
| 8 | +export class PostgresJSConnection implements DatabaseConnection { |
| 9 | + readonly #config: PostgresJSDialectConfig |
| 10 | + #sql: Sql |
| 11 | + #transaction?: Sql |
| 12 | + |
| 13 | + constructor(config: PostgresJSDialectConfig, sql: Sql) { |
| 14 | + this.#config = freeze({...config}) |
| 15 | + this.#sql = sql |
| 16 | + } |
| 17 | + |
| 18 | + async beginTransaction(settings: TransactionSettings): Promise<void> { |
| 19 | + if (this.#transaction) { |
| 20 | + throw new PostgresJSDialectError('transaction already begun!') |
| 21 | + } |
| 22 | + |
| 23 | + const {isolationLevel} = settings |
| 24 | + |
| 25 | + this.#transaction = this.#config.postgres({...this.#config.options, max: 1}) |
| 26 | + |
| 27 | + const statement = `start transaction${isolationLevel ? ` ${isolationLevel}` : ''}` |
| 28 | + |
| 29 | + await this.#transaction.unsafe(statement) |
| 30 | + } |
| 31 | + |
| 32 | + async commitTransaction(): Promise<void> { |
| 33 | + if (!this.#transaction) { |
| 34 | + throw new PostgresJSDialectError('no transaction to commit!') |
| 35 | + } |
| 36 | + |
| 37 | + await this.#transaction`commit` |
| 38 | + |
| 39 | + this.#releaseTransaction() |
| 40 | + } |
| 41 | + |
| 42 | + async executeQuery<R>(compiledQuery: CompiledQuery<unknown>): Promise<QueryResult<R>> { |
| 43 | + const result = await this.#resolveExecutor().unsafe<R[]>(compiledQuery.sql, compiledQuery.parameters.slice() as any) |
| 44 | + |
| 45 | + const rows = Array.from(result.values()) |
| 46 | + |
| 47 | + if (['INSERT', 'UPDATE', 'DELETE'].includes(result.command)) { |
| 48 | + const numAffectedRows = BigInt(result.count) |
| 49 | + |
| 50 | + return {numAffectedRows, rows} |
| 51 | + } |
| 52 | + |
| 53 | + return {rows} |
| 54 | + } |
| 55 | + |
| 56 | + async rollbackTransaction(): Promise<void> { |
| 57 | + if (!this.#transaction) { |
| 58 | + throw new PostgresJSDialectError('no transaction to rollback!') |
| 59 | + } |
| 60 | + |
| 61 | + await this.#transaction`rollback` |
| 62 | + |
| 63 | + this.#releaseTransaction() |
| 64 | + } |
| 65 | + |
| 66 | + async *streamQuery<R>( |
| 67 | + compiledQuery: CompiledQuery<unknown>, |
| 68 | + chunkSize: number, |
| 69 | + ): AsyncIterableIterator<QueryResult<R>> { |
| 70 | + if (!Number.isInteger(chunkSize) || chunkSize <= 0) { |
| 71 | + throw new PostgresJSDialectError('chunkSize must be a positive integer') |
| 72 | + } |
| 73 | + |
| 74 | + const cursor = this.#resolveExecutor() |
| 75 | + .unsafe<R[]>(compiledQuery.sql, compiledQuery.parameters.slice() as any) |
| 76 | + .cursor(chunkSize) |
| 77 | + |
| 78 | + for await (const rows of cursor) { |
| 79 | + yield {rows} |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + #resolveExecutor(): Sql { |
| 84 | + return this.#transaction || this.#sql |
| 85 | + } |
| 86 | + |
| 87 | + #releaseTransaction(): void { |
| 88 | + if (this.#transaction) { |
| 89 | + this.#transaction.end() |
| 90 | + this.#transaction = undefined |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments