|
| 1 | +import { DatabaseConnection, QueryResult } from "kysely"; |
| 2 | +import { Driver } from "kysely"; |
| 3 | +import { CompiledQuery } from "kysely"; |
| 4 | +import * as RDSDataService from "aws-sdk/clients/rdsdataservice"; |
| 5 | + |
| 6 | +export type DataApiDriverConfig = { |
| 7 | + client: RDSDataService; |
| 8 | + secretArn: string; |
| 9 | + resourceArn: string; |
| 10 | + database: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export class DataApiDriver extends Driver { |
| 14 | + #config: DataApiDriverConfig; |
| 15 | + |
| 16 | + constructor(config: DataApiDriverConfig) { |
| 17 | + super(); |
| 18 | + this.#config = config; |
| 19 | + } |
| 20 | + |
| 21 | + protected override async init(): Promise<void> {} |
| 22 | + |
| 23 | + protected override async acquireConnection(): Promise<DatabaseConnection> { |
| 24 | + return new DataApiConnection(this.#config); |
| 25 | + } |
| 26 | + |
| 27 | + override async beginTransaction(conn: DataApiConnection) { |
| 28 | + await conn.beginTransaction(); |
| 29 | + } |
| 30 | + |
| 31 | + override async commitTransaction(conn: DataApiConnection) { |
| 32 | + await conn.commitTransaction(); |
| 33 | + } |
| 34 | + |
| 35 | + override async rollbackTransaction(conn: DataApiConnection) { |
| 36 | + await conn.rollbackTransaction(); |
| 37 | + } |
| 38 | + |
| 39 | + protected override async releaseConnection( |
| 40 | + _connection: DatabaseConnection |
| 41 | + ): Promise<void> {} |
| 42 | + |
| 43 | + protected override async destroy(): Promise<void> {} |
| 44 | +} |
| 45 | + |
| 46 | +class DataApiConnection implements DatabaseConnection { |
| 47 | + #config: DataApiDriverConfig; |
| 48 | + #transactionId?: string; |
| 49 | + |
| 50 | + constructor(config: DataApiDriverConfig) { |
| 51 | + this.#config = config; |
| 52 | + } |
| 53 | + |
| 54 | + async beginTransaction() { |
| 55 | + const r = await this.#config.client |
| 56 | + .beginTransaction({ |
| 57 | + secretArn: this.#config.secretArn, |
| 58 | + resourceArn: this.#config.resourceArn, |
| 59 | + database: this.#config.database, |
| 60 | + }) |
| 61 | + .promise(); |
| 62 | + this.#transactionId = r.transactionId; |
| 63 | + } |
| 64 | + |
| 65 | + async commitTransaction() { |
| 66 | + if (!this.#transactionId) |
| 67 | + throw new Error("Cannot commit a transaction before creating it"); |
| 68 | + await this.#config.client |
| 69 | + .commitTransaction({ |
| 70 | + secretArn: this.#config.secretArn, |
| 71 | + resourceArn: this.#config.resourceArn, |
| 72 | + transactionId: this.#transactionId, |
| 73 | + }) |
| 74 | + .promise(); |
| 75 | + } |
| 76 | + |
| 77 | + async rollbackTransaction() { |
| 78 | + if (!this.#transactionId) |
| 79 | + throw new Error("Cannot rollback a transaction before creating it"); |
| 80 | + await this.#config.client |
| 81 | + .rollbackTransaction({ |
| 82 | + secretArn: this.#config.secretArn, |
| 83 | + resourceArn: this.#config.resourceArn, |
| 84 | + transactionId: this.#transactionId, |
| 85 | + }) |
| 86 | + .promise(); |
| 87 | + } |
| 88 | + |
| 89 | + async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> { |
| 90 | + const r = await this.#config.client |
| 91 | + .executeStatement({ |
| 92 | + transactionId: this.#transactionId, |
| 93 | + secretArn: this.#config.secretArn, |
| 94 | + resourceArn: this.#config.resourceArn, |
| 95 | + sql: compiledQuery.sql, |
| 96 | + parameters: compiledQuery.bindings as any, |
| 97 | + database: this.#config.database, |
| 98 | + includeResultMetadata: true, |
| 99 | + }) |
| 100 | + .promise(); |
| 101 | + if (!r.columnMetadata) { |
| 102 | + return { |
| 103 | + numUpdatedOrDeletedRows: r.numberOfRecordsUpdated, |
| 104 | + }; |
| 105 | + } |
| 106 | + const rows = r.records |
| 107 | + ?.filter((r) => r.length !== 0) |
| 108 | + .map( |
| 109 | + (rec) => |
| 110 | + Object.fromEntries( |
| 111 | + rec.map((val, i) => [ |
| 112 | + r.columnMetadata![i].name, |
| 113 | + val.stringValue || |
| 114 | + val.blobValue || |
| 115 | + val.longValue || |
| 116 | + val.arrayValue || |
| 117 | + val.doubleValue || |
| 118 | + (val.isNull ? null : val.booleanValue), |
| 119 | + ]) |
| 120 | + ) as O |
| 121 | + ); |
| 122 | + const result: QueryResult<O> = { |
| 123 | + rows, |
| 124 | + }; |
| 125 | + return result; |
| 126 | + } |
| 127 | +} |
0 commit comments