@powersync/react-native 1.31.0 → 1.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +25 -82
- package/dist/index.js.map +1 -1
- package/lib/db/adapters/react-native-quick-sqlite/RNQSDBAdapter.d.ts +41 -27
- package/lib/db/adapters/react-native-quick-sqlite/RNQSDBAdapter.js +26 -83
- package/lib/db/adapters/react-native-quick-sqlite/RNQSDBAdapter.js.map +1 -1
- package/package.json +3 -3
- package/src/db/adapters/react-native-quick-sqlite/RNQSDBAdapter.ts +30 -101
|
@@ -1,41 +1,55 @@
|
|
|
1
|
-
import { BaseObserver, DBAdapter, DBAdapterListener, LockContext as PowerSyncLockContext,
|
|
1
|
+
import { BaseObserver, DBAdapter, DBAdapterListener, LockContext as PowerSyncLockContext, DBLockOptions, ConnectionPool, LockContext, Transaction } from '@powersync/common';
|
|
2
2
|
import type { QuickSQLiteConnection, LockContext as RNQSLockContext } from '@journeyapps/react-native-quick-sqlite';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
export declare class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {
|
|
3
|
+
import { QueryResult, SqlExecutor } from '@powersync/common/dist/index.cjs';
|
|
4
|
+
declare class RNQSConnectionPool extends BaseObserver<DBAdapterListener> implements ConnectionPool {
|
|
7
5
|
protected baseDB: QuickSQLiteConnection;
|
|
8
6
|
name: string;
|
|
9
|
-
getAll: <T>(sql: string, parameters?: any[]) => Promise<T[]>;
|
|
10
|
-
getOptional: <T>(sql: string, parameters?: any[]) => Promise<T | null>;
|
|
11
|
-
get: <T>(sql: string, parameters?: any[]) => Promise<T>;
|
|
12
7
|
constructor(baseDB: QuickSQLiteConnection, name: string);
|
|
13
8
|
close(): void;
|
|
14
9
|
readLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
15
|
-
readTransaction<T>(fn: (tx: PowerSyncTransaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
16
10
|
writeLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
17
|
-
|
|
11
|
+
generateContext<T extends RNQSLockContext>(ctx: T): QuickSqliteContext;
|
|
12
|
+
refreshSchema(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
declare class QuickSqliteExecutor implements Omit<SqlExecutor, 'executeBatch'> {
|
|
15
|
+
readonly context: RNQSLockContext;
|
|
16
|
+
constructor(context: RNQSLockContext);
|
|
18
17
|
execute(query: string, params?: any[]): Promise<import("@journeyapps/react-native-quick-sqlite").QueryResult>;
|
|
19
18
|
/**
|
|
20
19
|
* 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
|
|
21
20
|
*/
|
|
22
21
|
executeRaw(query: string, params?: any[]): Promise<any[][]>;
|
|
22
|
+
}
|
|
23
|
+
declare const QuickSqliteContext_base: (new (...args: any[]) => {
|
|
24
|
+
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
25
|
+
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
26
|
+
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
27
|
+
executeBatch(query: string, params?: any[][]): Promise<import("@powersync/common").QueryResult>;
|
|
28
|
+
execute: (query: string, params?: any[] | undefined) => Promise<import("@powersync/common").QueryResult>;
|
|
29
|
+
executeRaw: (query: string, params?: any[] | undefined) => Promise<any[][]>;
|
|
30
|
+
}) & typeof QuickSqliteExecutor;
|
|
31
|
+
declare class QuickSqliteContext extends QuickSqliteContext_base implements LockContext {
|
|
32
|
+
}
|
|
33
|
+
declare const RNQSDBAdapter_base: (new (...args: any[]) => {
|
|
34
|
+
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
35
|
+
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
36
|
+
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
37
|
+
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
38
|
+
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
39
|
+
execute(query: string, params?: any[]): Promise<import("@powersync/common").QueryResult>;
|
|
40
|
+
executeRaw(query: string, params?: any[]): Promise<any[][]>;
|
|
41
|
+
executeBatch(query: string, params?: any[][]): Promise<import("@powersync/common").QueryResult>;
|
|
42
|
+
name: string;
|
|
43
|
+
close: () => void | Promise<void>;
|
|
44
|
+
readLock: <T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions) => Promise<T>;
|
|
45
|
+
writeLock: <T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions) => Promise<T>;
|
|
46
|
+
refreshSchema: () => Promise<void>;
|
|
47
|
+
registerListener(listener: Partial<DBAdapterListener>): () => void;
|
|
48
|
+
}) & typeof RNQSConnectionPool;
|
|
49
|
+
/**
|
|
50
|
+
* Adapter for React Native Quick SQLite
|
|
51
|
+
*/
|
|
52
|
+
export declare class RNQSDBAdapter extends RNQSDBAdapter_base implements DBAdapter {
|
|
23
53
|
executeBatch(query: string, params?: any[][]): Promise<QueryResult>;
|
|
24
|
-
generateContext<T extends RNQSLockContext>(ctx: T): T & {
|
|
25
|
-
executeRaw: (sql: string, params?: any[]) => Promise<unknown[][]>;
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* This provides a top-level read only execute method which is executed inside a read-lock.
|
|
29
|
-
* This is necessary since the high level `execute` method uses a write-lock under
|
|
30
|
-
* the hood. Helper methods such as `get`, `getAll` and `getOptional` are read only,
|
|
31
|
-
* and should use this method.
|
|
32
|
-
*/
|
|
33
|
-
private readOnlyExecute;
|
|
34
|
-
/**
|
|
35
|
-
* Adds DB get utils to lock contexts and transaction contexts
|
|
36
|
-
* @param tx
|
|
37
|
-
* @returns
|
|
38
|
-
*/
|
|
39
|
-
private generateDBHelpers;
|
|
40
|
-
refreshSchema(): Promise<void>;
|
|
41
54
|
}
|
|
55
|
+
export {};
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import { BaseObserver } from '@powersync/common';
|
|
2
|
-
|
|
3
|
-
* Adapter for React Native Quick SQLite
|
|
4
|
-
*/
|
|
5
|
-
export class RNQSDBAdapter extends BaseObserver {
|
|
1
|
+
import { BaseObserver, DBGetUtilsDefaultMixin, DBAdapterDefaultMixin } from '@powersync/common';
|
|
2
|
+
class RNQSConnectionPool extends BaseObserver {
|
|
6
3
|
baseDB;
|
|
7
4
|
name;
|
|
8
|
-
getAll;
|
|
9
|
-
getOptional;
|
|
10
|
-
get;
|
|
11
5
|
constructor(baseDB, name) {
|
|
12
6
|
super();
|
|
13
7
|
this.baseDB = baseDB;
|
|
@@ -16,41 +10,48 @@ export class RNQSDBAdapter extends BaseObserver {
|
|
|
16
10
|
baseDB.registerTablesChangedHook((update) => {
|
|
17
11
|
this.iterateListeners((cb) => cb.tablesUpdated?.(update));
|
|
18
12
|
});
|
|
19
|
-
const topLevelUtils = this.generateDBHelpers({
|
|
20
|
-
// Arrow function binds `this` for use in readOnlyExecute
|
|
21
|
-
execute: (sql, params) => this.readOnlyExecute(sql, params)
|
|
22
|
-
});
|
|
23
|
-
// Only assigning get helpers
|
|
24
|
-
this.getAll = topLevelUtils.getAll;
|
|
25
|
-
this.getOptional = topLevelUtils.getOptional;
|
|
26
|
-
this.get = topLevelUtils.get;
|
|
27
13
|
}
|
|
28
14
|
close() {
|
|
29
15
|
return this.baseDB.close();
|
|
30
16
|
}
|
|
31
17
|
readLock(fn, options) {
|
|
32
|
-
return this.baseDB.readLock((dbTx) => fn(this.
|
|
33
|
-
}
|
|
34
|
-
readTransaction(fn, options) {
|
|
35
|
-
return this.baseDB.readTransaction((dbTx) => fn(this.generateDBHelpers(this.generateContext(dbTx))), options);
|
|
18
|
+
return this.baseDB.readLock((dbTx) => fn(this.generateContext(dbTx)), options);
|
|
36
19
|
}
|
|
37
20
|
writeLock(fn, options) {
|
|
38
|
-
return this.baseDB.writeLock((dbTx) => fn(this.
|
|
21
|
+
return this.baseDB.writeLock((dbTx) => fn(this.generateContext(dbTx)), options);
|
|
22
|
+
}
|
|
23
|
+
generateContext(ctx) {
|
|
24
|
+
return new QuickSqliteContext(ctx);
|
|
25
|
+
}
|
|
26
|
+
async refreshSchema() {
|
|
27
|
+
await this.baseDB.refreshSchema();
|
|
39
28
|
}
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
}
|
|
30
|
+
class QuickSqliteExecutor {
|
|
31
|
+
context;
|
|
32
|
+
constructor(context) {
|
|
33
|
+
this.context = context;
|
|
42
34
|
}
|
|
43
35
|
execute(query, params) {
|
|
44
|
-
return this.
|
|
36
|
+
return this.context.execute(query, params);
|
|
45
37
|
}
|
|
46
38
|
/**
|
|
47
39
|
* 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
|
|
48
40
|
*/
|
|
49
41
|
async executeRaw(query, params) {
|
|
50
|
-
const result = await this.
|
|
42
|
+
const result = await this.context.execute(query, params);
|
|
51
43
|
const rows = result.rows?._array ?? [];
|
|
52
44
|
return rows.map((row) => Object.values(row));
|
|
53
45
|
}
|
|
46
|
+
}
|
|
47
|
+
class QuickSqliteContext extends DBGetUtilsDefaultMixin(QuickSqliteExecutor) {
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adapter for React Native Quick SQLite
|
|
51
|
+
*/
|
|
52
|
+
export class RNQSDBAdapter extends DBAdapterDefaultMixin(RNQSConnectionPool) {
|
|
53
|
+
// We don't want the default implementation here, RNQS does not support executeBatch for lock contexts so that would
|
|
54
|
+
// be less efficient.
|
|
54
55
|
async executeBatch(query, params = []) {
|
|
55
56
|
const commands = [];
|
|
56
57
|
for (let i = 0; i < params.length; i++) {
|
|
@@ -61,63 +62,5 @@ export class RNQSDBAdapter extends BaseObserver {
|
|
|
61
62
|
rowsAffected: result.rowsAffected ? result.rowsAffected : 0
|
|
62
63
|
};
|
|
63
64
|
}
|
|
64
|
-
generateContext(ctx) {
|
|
65
|
-
return {
|
|
66
|
-
...ctx,
|
|
67
|
-
// 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
|
|
68
|
-
executeRaw: async (sql, params) => {
|
|
69
|
-
const result = await ctx.execute(sql, params);
|
|
70
|
-
const rows = result.rows?._array ?? [];
|
|
71
|
-
return rows.map((row) => Object.values(row));
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* This provides a top-level read only execute method which is executed inside a read-lock.
|
|
77
|
-
* This is necessary since the high level `execute` method uses a write-lock under
|
|
78
|
-
* the hood. Helper methods such as `get`, `getAll` and `getOptional` are read only,
|
|
79
|
-
* and should use this method.
|
|
80
|
-
*/
|
|
81
|
-
readOnlyExecute(sql, params) {
|
|
82
|
-
return this.baseDB.readLock((ctx) => ctx.execute(sql, params));
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Adds DB get utils to lock contexts and transaction contexts
|
|
86
|
-
* @param tx
|
|
87
|
-
* @returns
|
|
88
|
-
*/
|
|
89
|
-
generateDBHelpers(tx) {
|
|
90
|
-
return {
|
|
91
|
-
...tx,
|
|
92
|
-
/**
|
|
93
|
-
* Execute a read-only query and return results
|
|
94
|
-
*/
|
|
95
|
-
getAll: async (sql, parameters) => {
|
|
96
|
-
const res = await tx.execute(sql, parameters);
|
|
97
|
-
return res.rows?._array ?? [];
|
|
98
|
-
},
|
|
99
|
-
/**
|
|
100
|
-
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
|
|
101
|
-
*/
|
|
102
|
-
getOptional: async (sql, parameters) => {
|
|
103
|
-
const res = await tx.execute(sql, parameters);
|
|
104
|
-
return res.rows?.item(0) ?? null;
|
|
105
|
-
},
|
|
106
|
-
/**
|
|
107
|
-
* Execute a read-only query and return the first result, error if the ResultSet is empty.
|
|
108
|
-
*/
|
|
109
|
-
get: async (sql, parameters) => {
|
|
110
|
-
const res = await tx.execute(sql, parameters);
|
|
111
|
-
const first = res.rows?.item(0);
|
|
112
|
-
if (!first) {
|
|
113
|
-
throw new Error('Result set is empty');
|
|
114
|
-
}
|
|
115
|
-
return first;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
async refreshSchema() {
|
|
120
|
-
await this.baseDB.refreshSchema();
|
|
121
|
-
}
|
|
122
65
|
}
|
|
123
66
|
//# sourceMappingURL=RNQSDBAdapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RNQSDBAdapter.js","sourceRoot":"","sources":["../../../../src/db/adapters/react-native-quick-sqlite/RNQSDBAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,
|
|
1
|
+
{"version":3,"file":"RNQSDBAdapter.js","sourceRoot":"","sources":["../../../../src/db/adapters/react-native-quick-sqlite/RNQSDBAdapter.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/react-native",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.32.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"homepage": "https://docs.powersync.com/",
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"@journeyapps/react-native-quick-sqlite": "^2.5.1",
|
|
29
|
-
"@powersync/common": "^1.
|
|
29
|
+
"@powersync/common": "^1.49.0",
|
|
30
30
|
"react": "*",
|
|
31
31
|
"react-native": "*"
|
|
32
32
|
},
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"async-mutex": "^0.5.0",
|
|
40
|
-
"@powersync/common": "1.
|
|
40
|
+
"@powersync/common": "1.49.0",
|
|
41
41
|
"@powersync/react": "1.9.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
@@ -3,25 +3,17 @@ import {
|
|
|
3
3
|
DBAdapter,
|
|
4
4
|
DBAdapterListener,
|
|
5
5
|
LockContext as PowerSyncLockContext,
|
|
6
|
-
Transaction as PowerSyncTransaction,
|
|
7
6
|
DBLockOptions,
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
ConnectionPool,
|
|
8
|
+
DBGetUtilsDefaultMixin,
|
|
9
|
+
LockContext,
|
|
10
|
+
DBAdapterDefaultMixin,
|
|
11
|
+
Transaction
|
|
10
12
|
} from '@powersync/common';
|
|
11
|
-
import type {
|
|
12
|
-
|
|
13
|
-
LockContext as RNQSLockContext,
|
|
14
|
-
TransactionContext as RNQSTransactionContext
|
|
15
|
-
} from '@journeyapps/react-native-quick-sqlite';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Adapter for React Native Quick SQLite
|
|
19
|
-
*/
|
|
20
|
-
export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {
|
|
21
|
-
getAll: <T>(sql: string, parameters?: any[]) => Promise<T[]>;
|
|
22
|
-
getOptional: <T>(sql: string, parameters?: any[]) => Promise<T | null>;
|
|
23
|
-
get: <T>(sql: string, parameters?: any[]) => Promise<T>;
|
|
13
|
+
import type { QuickSQLiteConnection, LockContext as RNQSLockContext } from '@journeyapps/react-native-quick-sqlite';
|
|
14
|
+
import { QueryResult, SqlExecutor } from '@powersync/common/dist/index.cjs';
|
|
24
15
|
|
|
16
|
+
class RNQSConnectionPool extends BaseObserver<DBAdapterListener> implements ConnectionPool {
|
|
25
17
|
constructor(
|
|
26
18
|
protected baseDB: QuickSQLiteConnection,
|
|
27
19
|
public name: string
|
|
@@ -31,15 +23,6 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
|
|
|
31
23
|
baseDB.registerTablesChangedHook((update) => {
|
|
32
24
|
this.iterateListeners((cb) => cb.tablesUpdated?.(update));
|
|
33
25
|
});
|
|
34
|
-
|
|
35
|
-
const topLevelUtils = this.generateDBHelpers({
|
|
36
|
-
// Arrow function binds `this` for use in readOnlyExecute
|
|
37
|
-
execute: (sql: string, params?: any[]) => this.readOnlyExecute(sql, params)
|
|
38
|
-
});
|
|
39
|
-
// Only assigning get helpers
|
|
40
|
-
this.getAll = topLevelUtils.getAll;
|
|
41
|
-
this.getOptional = topLevelUtils.getOptional;
|
|
42
|
-
this.get = topLevelUtils.get;
|
|
43
26
|
}
|
|
44
27
|
|
|
45
28
|
close() {
|
|
@@ -47,34 +30,46 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
|
|
|
47
30
|
}
|
|
48
31
|
|
|
49
32
|
readLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
|
|
50
|
-
return this.baseDB.readLock((dbTx) => fn(this.
|
|
33
|
+
return this.baseDB.readLock((dbTx) => fn(this.generateContext(dbTx)), options);
|
|
51
34
|
}
|
|
52
35
|
|
|
53
|
-
|
|
54
|
-
return this.baseDB.
|
|
36
|
+
writeLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
|
|
37
|
+
return this.baseDB.writeLock((dbTx) => fn(this.generateContext(dbTx)), options);
|
|
55
38
|
}
|
|
56
39
|
|
|
57
|
-
|
|
58
|
-
return
|
|
40
|
+
generateContext<T extends RNQSLockContext>(ctx: T) {
|
|
41
|
+
return new QuickSqliteContext(ctx);
|
|
59
42
|
}
|
|
60
43
|
|
|
61
|
-
|
|
62
|
-
|
|
44
|
+
async refreshSchema() {
|
|
45
|
+
await this.baseDB.refreshSchema();
|
|
63
46
|
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class QuickSqliteExecutor implements Omit<SqlExecutor, 'executeBatch'> {
|
|
50
|
+
constructor(readonly context: RNQSLockContext) {}
|
|
64
51
|
|
|
65
52
|
execute(query: string, params?: any[]) {
|
|
66
|
-
return this.
|
|
53
|
+
return this.context.execute(query, params);
|
|
67
54
|
}
|
|
68
|
-
|
|
69
55
|
/**
|
|
70
56
|
* 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
|
|
71
57
|
*/
|
|
72
58
|
async executeRaw(query: string, params?: any[]): Promise<any[][]> {
|
|
73
|
-
const result = await this.
|
|
59
|
+
const result = await this.context.execute(query, params);
|
|
74
60
|
const rows = result.rows?._array ?? [];
|
|
75
61
|
return rows.map((row) => Object.values(row));
|
|
76
62
|
}
|
|
63
|
+
}
|
|
77
64
|
|
|
65
|
+
class QuickSqliteContext extends DBGetUtilsDefaultMixin(QuickSqliteExecutor) implements LockContext {}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Adapter for React Native Quick SQLite
|
|
69
|
+
*/
|
|
70
|
+
export class RNQSDBAdapter extends DBAdapterDefaultMixin(RNQSConnectionPool) implements DBAdapter {
|
|
71
|
+
// We don't want the default implementation here, RNQS does not support executeBatch for lock contexts so that would
|
|
72
|
+
// be less efficient.
|
|
78
73
|
async executeBatch(query: string, params: any[][] = []): Promise<QueryResult> {
|
|
79
74
|
const commands: any[] = [];
|
|
80
75
|
|
|
@@ -87,70 +82,4 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
|
|
|
87
82
|
rowsAffected: result.rowsAffected ? result.rowsAffected : 0
|
|
88
83
|
};
|
|
89
84
|
}
|
|
90
|
-
|
|
91
|
-
generateContext<T extends RNQSLockContext>(ctx: T) {
|
|
92
|
-
return {
|
|
93
|
-
...ctx,
|
|
94
|
-
// 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
|
|
95
|
-
executeRaw: async (sql: string, params?: any[]) => {
|
|
96
|
-
const result = await ctx.execute(sql, params);
|
|
97
|
-
const rows = result.rows?._array ?? [];
|
|
98
|
-
return rows.map((row) => Object.values(row));
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* This provides a top-level read only execute method which is executed inside a read-lock.
|
|
105
|
-
* This is necessary since the high level `execute` method uses a write-lock under
|
|
106
|
-
* the hood. Helper methods such as `get`, `getAll` and `getOptional` are read only,
|
|
107
|
-
* and should use this method.
|
|
108
|
-
*/
|
|
109
|
-
private readOnlyExecute(sql: string, params?: any[]) {
|
|
110
|
-
return this.baseDB.readLock((ctx) => ctx.execute(sql, params));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Adds DB get utils to lock contexts and transaction contexts
|
|
115
|
-
* @param tx
|
|
116
|
-
* @returns
|
|
117
|
-
*/
|
|
118
|
-
private generateDBHelpers<T extends { execute: (sql: string, params?: any[]) => Promise<QueryResult> }>(
|
|
119
|
-
tx: T
|
|
120
|
-
): T & DBGetUtils {
|
|
121
|
-
return {
|
|
122
|
-
...tx,
|
|
123
|
-
/**
|
|
124
|
-
* Execute a read-only query and return results
|
|
125
|
-
*/
|
|
126
|
-
getAll: async <T>(sql: string, parameters?: any[]): Promise<T[]> => {
|
|
127
|
-
const res = await tx.execute(sql, parameters);
|
|
128
|
-
return res.rows?._array ?? [];
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
|
|
133
|
-
*/
|
|
134
|
-
getOptional: async <T>(sql: string, parameters?: any[]): Promise<T | null> => {
|
|
135
|
-
const res = await tx.execute(sql, parameters);
|
|
136
|
-
return res.rows?.item(0) ?? null;
|
|
137
|
-
},
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Execute a read-only query and return the first result, error if the ResultSet is empty.
|
|
141
|
-
*/
|
|
142
|
-
get: async <T>(sql: string, parameters?: any[]): Promise<T> => {
|
|
143
|
-
const res = await tx.execute(sql, parameters);
|
|
144
|
-
const first = res.rows?.item(0);
|
|
145
|
-
if (!first) {
|
|
146
|
-
throw new Error('Result set is empty');
|
|
147
|
-
}
|
|
148
|
-
return first;
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async refreshSchema() {
|
|
154
|
-
await this.baseDB.refreshSchema();
|
|
155
|
-
}
|
|
156
85
|
}
|