-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpglite-driver.ts
61 lines (49 loc) · 1.47 KB
/
pglite-driver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { PGlite } from '@electric-sql/pglite'
import {
CompiledQuery,
type DatabaseConnection,
type QueryResult,
type TransactionSettings,
} from 'kysely'
export class PGliteDriver {
#client: PGlite
constructor(client: PGlite) {
this.#client = client
}
async acquireConnection(): Promise<DatabaseConnection> {
return new PGliteConnection(this.#client)
}
async beginTransaction(
connection: DatabaseConnection,
_settings: TransactionSettings,
): Promise<void> {
await connection.executeQuery(CompiledQuery.raw('BEGIN'))
}
async commitTransaction(connection: DatabaseConnection): Promise<void> {
await connection.executeQuery(CompiledQuery.raw('COMMIT'))
}
async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
await connection.executeQuery(CompiledQuery.raw('ROLLBACK'))
}
async destroy(): Promise<void> {
await this.#client.close()
}
async init(): Promise<void> {}
async releaseConnection(_connection: DatabaseConnection): Promise<void> {}
}
class PGliteConnection implements DatabaseConnection {
#client: PGlite
constructor(client: PGlite) {
this.#client = client
}
async executeQuery<R>(
compiledQuery: CompiledQuery<any>,
): Promise<QueryResult<R>> {
return await this.#client.query<R>(compiledQuery.sql, [
...compiledQuery.parameters,
])
}
async *streamQuery(): AsyncGenerator<never, void, unknown> {
throw new Error('PGlite does not support streaming.')
}
}