| 10 | } from 'kysely' |
| 11 | |
| 12 | class CRDialect extends SqliteDialect { |
| 13 | database: () => Promise<CRDatabase> |
| 14 | |
| 15 | constructor(config: CRDialectConfig) { |
| 16 | super(config as unknown as SqliteDialectConfig) |
| 17 | this.database = async () => |
| 18 | typeof config.database === 'function' |
| 19 | ? await config.database() |
| 20 | : config.database |
| 21 | } |
| 22 | |
| 23 | createDriver() { |
| 24 | const load = this.database |
| 25 | const waiter = mutex() |
| 26 | |
| 27 | let db: CRDatabase |
| 28 | let connection: DatabaseConnection |
| 29 | |
| 30 | return { |
| 31 | async init() { |
| 32 | db = await load() |
| 33 | connection = { |
| 34 | async executeQuery<O>(query: CompiledQuery) { |
| 35 | return { |
| 36 | rows: (await db.execO(query.sql, query.parameters)) as O[], |
| 37 | } |
| 38 | }, |
| 39 | async *streamQuery() { |
| 40 | throw new Error("Sqlite driver doesn't support streaming") |
| 41 | }, |
| 42 | } |
| 43 | }, |
| 44 | async acquireConnection() { |
| 45 | await waiter.lock() |
| 46 | return connection |
| 47 | }, |
| 48 | async beginTransaction(connection: DatabaseConnection) { |
| 49 | await connection.executeQuery(CompiledQuery.raw('begin')) |
| 50 | }, |
| 51 | async commitTransaction(connection: DatabaseConnection) { |
| 52 | await connection.executeQuery(CompiledQuery.raw('commit')) |
| 53 | }, |
| 54 | async rollbackTransaction(connection: DatabaseConnection) { |
| 55 | await connection.executeQuery(CompiledQuery.raw('rollback')) |
| 56 | }, |
| 57 | async releaseConnection() { |
| 58 | waiter.unlock() |
| 59 | }, |
| 60 | async destroy() { |
| 61 | db.close() |
| 62 | }, |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | function mutex() { |
| 68 | let promise: Promise<void> | undefined |
nothing calls this directly
no outgoing calls
no test coverage detected