| 28 | * `DatabaseAdapter` implementation for postgres-compatible clients. |
| 29 | */ |
| 30 | export class PostgresDatabaseAdapter implements DatabaseAdapter { |
| 31 | /** |
| 32 | * The SQL dialect identifier reported by this adapter. |
| 33 | */ |
| 34 | dialect = 'postgres' |
| 35 | |
| 36 | /** |
| 37 | * Feature flags describing the postgres behaviors supported by this adapter. |
| 38 | */ |
| 39 | capabilities |
| 40 | |
| 41 | #client: PostgresQueryable |
| 42 | #transactions = new Map<string, TransactionState>() |
| 43 | #transactionCounter = 0 |
| 44 | |
| 45 | constructor(client: PostgresQueryable) { |
| 46 | this.#client = client |
| 47 | this.capabilities = { |
| 48 | returning: true, |
| 49 | savepoints: true, |
| 50 | upsert: true, |
| 51 | transactionalDdl: true, |
| 52 | migrationLock: true, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Compiles a data-manipulation operation to postgres SQL statements. |
| 58 | * @param operation Operation to compile. |
| 59 | * @returns Compiled SQL statements. |
| 60 | */ |
| 61 | compileSql(operation: DataManipulationOperation): SqlStatement[] { |
| 62 | let compiled = compilePostgresOperation(operation) |
| 63 | return [{ text: compiled.text, values: compiled.values }] |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Executes a postgres data-manipulation request. |
| 68 | * @param request Request to execute. |
| 69 | * @returns Execution result. |
| 70 | */ |
| 71 | async execute(request: DataManipulationRequest): Promise<DataManipulationResult> { |
| 72 | if (request.operation.kind === 'insertMany' && request.operation.values.length === 0) { |
| 73 | return { |
| 74 | affectedRows: 0, |
| 75 | insertId: undefined, |
| 76 | rows: request.operation.returning ? [] : undefined, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | let statement = compilePostgresOperation(request.operation) |
| 81 | let client = this.#resolveClient(request.transaction) |
| 82 | let result = await client.query(statement.text, statement.values) |
| 83 | let rows = normalizeRows(result.rows) |
| 84 | |
| 85 | if (request.operation.kind === 'count' || request.operation.kind === 'exists') { |
| 86 | rows = normalizeCountRows(rows) |
| 87 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…