| 32 | import { DialectOptions } from "."; |
| 33 | |
| 34 | class SqliteDriver implements Driver { |
| 35 | connection?: DatabaseConnection; |
| 36 | private connectionMutex = new Mutex(); |
| 37 | private handle?: string; |
| 38 | constructor(private readonly config: { name: string }) {} |
| 39 | |
| 40 | async init(): Promise<void> { |
| 41 | this.handle = await desktop!.sqlite.open.mutate({ |
| 42 | filePath: this.config.name |
| 43 | }); |
| 44 | this.connection = new SqliteWorkerConnection(this.handle); |
| 45 | } |
| 46 | |
| 47 | async acquireConnection(): Promise<DatabaseConnection> { |
| 48 | if (!this.connection) throw new Error("Driver not initialized."); |
| 49 | |
| 50 | // SQLite only has one single connection. We use a mutex here to wait |
| 51 | // until the single connection has been released. |
| 52 | await this.connectionMutex.waitForUnlock(); |
| 53 | await this.connectionMutex.acquire(); |
| 54 | return this.connection; |
| 55 | } |
| 56 | |
| 57 | async beginTransaction(connection: DatabaseConnection): Promise<void> { |
| 58 | await connection.executeQuery(CompiledQuery.raw("begin")); |
| 59 | } |
| 60 | |
| 61 | async commitTransaction(connection: DatabaseConnection): Promise<void> { |
| 62 | await connection.executeQuery(CompiledQuery.raw("commit")); |
| 63 | } |
| 64 | |
| 65 | async rollbackTransaction(connection: DatabaseConnection): Promise<void> { |
| 66 | await connection.executeQuery(CompiledQuery.raw("rollback")); |
| 67 | } |
| 68 | |
| 69 | async releaseConnection(): Promise<void> { |
| 70 | this.connectionMutex.release(); |
| 71 | } |
| 72 | |
| 73 | async destroy(): Promise<void> { |
| 74 | if (!this.handle) return; |
| 75 | await desktop!.sqlite.close.mutate({ id: this.handle }); |
| 76 | } |
| 77 | |
| 78 | async delete() { |
| 79 | if (!this.handle) return; |
| 80 | await desktop!.sqlite.delete.mutate({ id: this.handle }); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | class SqliteWorkerConnection implements DatabaseConnection { |
| 85 | constructor(private readonly handle: string) {} |
nothing calls this directly
no outgoing calls
no test coverage detected