-
Notifications
You must be signed in to change notification settings - Fork 120
/
adapter.ts
53 lines (43 loc) · 1.63 KB
/
adapter.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
import { Database } from './database'
import { Row, SqlValue } from '../../util/types'
import { Statement } from '../../util'
import { BatchDatabaseAdapter as GenericDatabaseAdapter } from '../generic'
import { capSQLiteSet } from '@capacitor-community/sqlite'
import { RunResult } from '../../electric/adapter'
export class DatabaseAdapter extends GenericDatabaseAdapter {
readonly db: Database
readonly defaultNamespace = 'main'
constructor(db: Database) {
super()
this.db = db
}
async _query(statement: Statement): Promise<Row[]> {
const wrapInTransaction = false
const result = await this.db.query(
statement.sql,
statement.args,
wrapInTransaction
)
return result.values ?? []
}
async _run(statement: Statement): Promise<RunResult> {
const wrapInTransaction = false
// if no bind values are provided, use `execute` API which
// has less overhead and native side pre-processing
const result = await (statement.args && statement.args.length > 0
? this.db.run(statement.sql, statement.args, wrapInTransaction)
: this.db.execute(statement.sql, wrapInTransaction))
const rowsAffected = result.changes?.changes ?? 0
return { rowsAffected: rowsAffected }
}
async execBatch(statements: Statement[]): Promise<RunResult> {
const set: capSQLiteSet[] = statements.map(({ sql, args }) => ({
statement: sql,
values: (args ?? []) as SqlValue[],
}))
const wrapInTransaction = true
const result = await this.db.executeSet(set, wrapInTransaction)
const rowsAffected = result.changes?.changes ?? 0
return { rowsAffected: rowsAffected }
}
}