@powersync/op-sqlite 0.0.0-dev-20250121082529 → 0.0.0-dev-20250121114305
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/lib/{typescript/commonjs/src → src}/db/OPSQLiteConnection.d.ts +0 -1
- package/lib/src/db/OPSQLiteConnection.js +118 -0
- package/lib/{typescript/module/src → src}/db/OPSqliteAdapter.d.ts +0 -1
- package/lib/src/db/OPSqliteAdapter.js +267 -0
- package/lib/{typescript/commonjs/src → src}/db/OPSqliteDBOpenFactory.d.ts +0 -1
- package/lib/src/db/OPSqliteDBOpenFactory.js +15 -0
- package/lib/{typescript/commonjs/src → src}/db/SqliteOptions.d.ts +1 -2
- package/lib/src/db/SqliteOptions.js +29 -0
- package/lib/{typescript/commonjs/src → src}/index.d.ts +0 -1
- package/lib/src/index.js +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +6 -75
- package/android/build.gradle +0 -124
- package/android/gradle.properties +0 -5
- package/android/src/main/AndroidManifest.xml +0 -3
- package/android/src/main/AndroidManifestNew.xml +0 -2
- package/android/src/main/java/com/powersync/opsqlite/PowerSyncOpSqlitePackage.kt +0 -31
- package/ios/PowerSyncOpSqlite.h +0 -5
- package/ios/PowerSyncOpSqlite.mm +0 -6
- package/lib/commonjs/db/OPSQLiteConnection.js +0 -103
- package/lib/commonjs/db/OPSQLiteConnection.js.map +0 -1
- package/lib/commonjs/db/OPSqliteAdapter.js +0 -249
- package/lib/commonjs/db/OPSqliteAdapter.js.map +0 -1
- package/lib/commonjs/db/OPSqliteDBOpenFactory.js +0 -26
- package/lib/commonjs/db/OPSqliteDBOpenFactory.js.map +0 -1
- package/lib/commonjs/db/SqliteOptions.js +0 -33
- package/lib/commonjs/db/SqliteOptions.js.map +0 -1
- package/lib/commonjs/index.js +0 -19
- package/lib/commonjs/index.js.map +0 -1
- package/lib/module/db/OPSQLiteConnection.js +0 -98
- package/lib/module/db/OPSQLiteConnection.js.map +0 -1
- package/lib/module/db/OPSqliteAdapter.js +0 -244
- package/lib/module/db/OPSqliteAdapter.js.map +0 -1
- package/lib/module/db/OPSqliteDBOpenFactory.js +0 -21
- package/lib/module/db/OPSqliteDBOpenFactory.js.map +0 -1
- package/lib/module/db/SqliteOptions.js +0 -29
- package/lib/module/db/SqliteOptions.js.map +0 -1
- package/lib/module/index.js +0 -4
- package/lib/module/index.js.map +0 -1
- package/lib/typescript/commonjs/package.json +0 -1
- package/lib/typescript/commonjs/src/db/OPSQLiteConnection.d.ts.map +0 -1
- package/lib/typescript/commonjs/src/db/OPSqliteAdapter.d.ts +0 -45
- package/lib/typescript/commonjs/src/db/OPSqliteAdapter.d.ts.map +0 -1
- package/lib/typescript/commonjs/src/db/OPSqliteDBOpenFactory.d.ts.map +0 -1
- package/lib/typescript/commonjs/src/db/SqliteOptions.d.ts.map +0 -1
- package/lib/typescript/commonjs/src/index.d.ts.map +0 -1
- package/lib/typescript/commonjs/tsconfig.build.tsbuildinfo +0 -1
- package/lib/typescript/module/package.json +0 -1
- package/lib/typescript/module/src/db/OPSQLiteConnection.d.ts +0 -27
- package/lib/typescript/module/src/db/OPSQLiteConnection.d.ts.map +0 -1
- package/lib/typescript/module/src/db/OPSqliteAdapter.d.ts.map +0 -1
- package/lib/typescript/module/src/db/OPSqliteDBOpenFactory.d.ts +0 -12
- package/lib/typescript/module/src/db/OPSqliteDBOpenFactory.d.ts.map +0 -1
- package/lib/typescript/module/src/db/SqliteOptions.d.ts +0 -52
- package/lib/typescript/module/src/db/SqliteOptions.d.ts.map +0 -1
- package/lib/typescript/module/src/index.d.ts +0 -2
- package/lib/typescript/module/src/index.d.ts.map +0 -1
- package/lib/typescript/module/tsconfig.build.tsbuildinfo +0 -1
- package/powersync-op-sqlite.podspec +0 -26
- package/src/db/OPSQLiteConnection.ts +0 -131
- package/src/db/OPSqliteAdapter.ts +0 -299
- package/src/db/OPSqliteDBOpenFactory.ts +0 -25
- package/src/db/SqliteOptions.ts +0 -71
- package/src/index.ts +0 -4
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { BaseObserver, RowUpdateType } from '@powersync/common';
|
|
11
|
+
export class OPSQLiteConnection extends BaseObserver {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super();
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.DB = options.baseDB;
|
|
16
|
+
this.updateBuffer = [];
|
|
17
|
+
this.DB.rollbackHook(() => {
|
|
18
|
+
this.updateBuffer = [];
|
|
19
|
+
});
|
|
20
|
+
this.DB.updateHook((update) => {
|
|
21
|
+
this.addTableUpdate(update);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
addTableUpdate(update) {
|
|
25
|
+
let opType;
|
|
26
|
+
switch (update.operation) {
|
|
27
|
+
case 'INSERT':
|
|
28
|
+
opType = RowUpdateType.SQLITE_INSERT;
|
|
29
|
+
break;
|
|
30
|
+
case 'DELETE':
|
|
31
|
+
opType = RowUpdateType.SQLITE_DELETE;
|
|
32
|
+
break;
|
|
33
|
+
case 'UPDATE':
|
|
34
|
+
opType = RowUpdateType.SQLITE_UPDATE;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
this.updateBuffer.push({
|
|
38
|
+
table: update.table,
|
|
39
|
+
opType,
|
|
40
|
+
rowId: update.rowId
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
flushUpdates() {
|
|
44
|
+
if (!this.updateBuffer.length) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const groupedUpdates = this.updateBuffer.reduce((grouping, update) => {
|
|
48
|
+
const { table } = update;
|
|
49
|
+
const updateGroup = grouping[table] || (grouping[table] = []);
|
|
50
|
+
updateGroup.push(update);
|
|
51
|
+
return grouping;
|
|
52
|
+
}, {});
|
|
53
|
+
const batchedUpdate = {
|
|
54
|
+
groupedUpdates,
|
|
55
|
+
rawUpdates: this.updateBuffer,
|
|
56
|
+
tables: Object.keys(groupedUpdates)
|
|
57
|
+
};
|
|
58
|
+
this.updateBuffer = [];
|
|
59
|
+
this.iterateListeners((l) => { var _a; return (_a = l.tablesUpdated) === null || _a === void 0 ? void 0 : _a.call(l, batchedUpdate); });
|
|
60
|
+
}
|
|
61
|
+
close() {
|
|
62
|
+
return this.DB.close();
|
|
63
|
+
}
|
|
64
|
+
execute(query, params) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
var _a, _b, _c;
|
|
67
|
+
const res = yield this.DB.execute(query, params);
|
|
68
|
+
return {
|
|
69
|
+
insertId: res.insertId,
|
|
70
|
+
rowsAffected: res.rowsAffected,
|
|
71
|
+
rows: {
|
|
72
|
+
_array: (_a = res.rows) !== null && _a !== void 0 ? _a : [],
|
|
73
|
+
length: (_c = (_b = res.rows) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0,
|
|
74
|
+
item: (index) => { var _a; return (_a = res.rows) === null || _a === void 0 ? void 0 : _a[index]; }
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
executeBatch(query_1) {
|
|
80
|
+
return __awaiter(this, arguments, void 0, function* (query, params = []) {
|
|
81
|
+
var _a;
|
|
82
|
+
const tuple = [[query, params[0]]];
|
|
83
|
+
params.slice(1).forEach((p) => tuple.push([query, p]));
|
|
84
|
+
const result = yield this.DB.executeBatch(tuple);
|
|
85
|
+
return {
|
|
86
|
+
rowsAffected: (_a = result.rowsAffected) !== null && _a !== void 0 ? _a : 0
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
getAll(sql, parameters) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
var _a;
|
|
93
|
+
const result = yield this.DB.execute(sql, parameters);
|
|
94
|
+
return ((_a = result.rows) !== null && _a !== void 0 ? _a : []);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
getOptional(sql, parameters) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
var _a, _b;
|
|
100
|
+
const result = yield this.DB.execute(sql, parameters);
|
|
101
|
+
return (_b = (_a = result.rows) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
get(sql, parameters) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
const result = yield this.getOptional(sql, parameters);
|
|
107
|
+
if (!result) {
|
|
108
|
+
throw new Error('Result set is empty');
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
refreshSchema() {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
yield this.get("PRAGMA table_info('sqlite_master')");
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -42,4 +42,3 @@ export declare class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> i
|
|
|
42
42
|
protected internalTransaction<T>(connection: OPSQLiteConnection, fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
43
43
|
refreshSchema(): Promise<void>;
|
|
44
44
|
}
|
|
45
|
-
//# sourceMappingURL=OPSqliteAdapter.d.ts.map
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { BaseObserver } from '@powersync/common';
|
|
11
|
+
import { ANDROID_DATABASE_PATH, getDylibPath, IOS_LIBRARY_PATH, open } from '@op-engineering/op-sqlite';
|
|
12
|
+
import Lock from 'async-lock';
|
|
13
|
+
import { OPSQLiteConnection } from './OPSQLiteConnection';
|
|
14
|
+
import { Platform } from 'react-native';
|
|
15
|
+
var LockType;
|
|
16
|
+
(function (LockType) {
|
|
17
|
+
LockType["READ"] = "read";
|
|
18
|
+
LockType["WRITE"] = "write";
|
|
19
|
+
})(LockType || (LockType = {}));
|
|
20
|
+
const READ_CONNECTIONS = 5;
|
|
21
|
+
export class OPSQLiteDBAdapter extends BaseObserver {
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super();
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.readQueue = [];
|
|
26
|
+
this.name = this.options.name;
|
|
27
|
+
this.locks = new Lock();
|
|
28
|
+
this.readConnections = null;
|
|
29
|
+
this.writeConnection = null;
|
|
30
|
+
this.initialized = this.init();
|
|
31
|
+
}
|
|
32
|
+
init() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous } = this.options.sqliteOptions;
|
|
35
|
+
const dbFilename = this.options.name;
|
|
36
|
+
this.writeConnection = yield this.openConnection(dbFilename);
|
|
37
|
+
const statements = [
|
|
38
|
+
`PRAGMA busy_timeout = ${lockTimeoutMs}`,
|
|
39
|
+
`PRAGMA journal_mode = ${journalMode}`,
|
|
40
|
+
`PRAGMA journal_size_limit = ${journalSizeLimit}`,
|
|
41
|
+
`PRAGMA synchronous = ${synchronous}`
|
|
42
|
+
];
|
|
43
|
+
for (const statement of statements) {
|
|
44
|
+
for (let tries = 0; tries < 30; tries++) {
|
|
45
|
+
try {
|
|
46
|
+
yield this.writeConnection.execute(statement);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
if (e instanceof Error && e.message.includes('database is locked') && tries < 29) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Changes should only occur in the write connection
|
|
60
|
+
this.writeConnection.registerListener({
|
|
61
|
+
tablesUpdated: (notification) => this.iterateListeners((cb) => { var _a; return (_a = cb.tablesUpdated) === null || _a === void 0 ? void 0 : _a.call(cb, notification); })
|
|
62
|
+
});
|
|
63
|
+
this.readConnections = [];
|
|
64
|
+
for (let i = 0; i < READ_CONNECTIONS; i++) {
|
|
65
|
+
const conn = yield this.openConnection(dbFilename);
|
|
66
|
+
yield conn.execute('PRAGMA query_only = true');
|
|
67
|
+
this.readConnections.push({ busy: false, connection: conn });
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
openConnection(filenameOverride) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
const dbFilename = filenameOverride !== null && filenameOverride !== void 0 ? filenameOverride : this.options.name;
|
|
75
|
+
const DB = this.openDatabase(dbFilename, (_b = (_a = this.options.sqliteOptions) === null || _a === void 0 ? void 0 : _a.encryptionKey) !== null && _b !== void 0 ? _b : undefined);
|
|
76
|
+
//Load extensions for all connections
|
|
77
|
+
this.loadAdditionalExtensions(DB);
|
|
78
|
+
this.loadPowerSyncExtension(DB);
|
|
79
|
+
yield DB.execute('SELECT powersync_init()');
|
|
80
|
+
return new OPSQLiteConnection({
|
|
81
|
+
baseDB: DB
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
getDbLocation(dbLocation) {
|
|
86
|
+
if (Platform.OS === 'ios') {
|
|
87
|
+
return dbLocation !== null && dbLocation !== void 0 ? dbLocation : IOS_LIBRARY_PATH;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
return dbLocation !== null && dbLocation !== void 0 ? dbLocation : ANDROID_DATABASE_PATH;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
openDatabase(dbFilename, encryptionKey) {
|
|
94
|
+
//This is needed because an undefined/null dbLocation will cause the open function to fail
|
|
95
|
+
const location = this.getDbLocation(this.options.dbLocation);
|
|
96
|
+
//Simarlily if the encryption key is undefined/null when using SQLCipher it will cause the open function to fail
|
|
97
|
+
if (encryptionKey) {
|
|
98
|
+
return open({
|
|
99
|
+
name: dbFilename,
|
|
100
|
+
location: location,
|
|
101
|
+
encryptionKey: encryptionKey
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
return open({
|
|
106
|
+
name: dbFilename,
|
|
107
|
+
location: location
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
loadAdditionalExtensions(DB) {
|
|
112
|
+
var _a;
|
|
113
|
+
if (((_a = this.options.sqliteOptions) === null || _a === void 0 ? void 0 : _a.extensions) && this.options.sqliteOptions.extensions.length > 0) {
|
|
114
|
+
for (const extension of this.options.sqliteOptions.extensions) {
|
|
115
|
+
DB.loadExtension(extension.path, extension.entryPoint);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
loadPowerSyncExtension(DB) {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
if (Platform.OS === 'ios') {
|
|
122
|
+
const libPath = getDylibPath('co.powersync.sqlitecore', 'powersync-sqlite-core');
|
|
123
|
+
DB.loadExtension(libPath, 'sqlite3_powersync_init');
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
DB.loadExtension('libpowersync', 'sqlite3_powersync_init');
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
close() {
|
|
131
|
+
this.initialized.then(() => {
|
|
132
|
+
this.writeConnection.close();
|
|
133
|
+
this.readConnections.forEach((c) => c.connection.close());
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
readLock(fn, options) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
yield this.initialized;
|
|
139
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
const execute = () => __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
// Find an available connection that is not busy
|
|
142
|
+
const availableConnection = this.readConnections.find((conn) => !conn.busy);
|
|
143
|
+
// If we have an available connection, use it
|
|
144
|
+
if (availableConnection) {
|
|
145
|
+
availableConnection.busy = true;
|
|
146
|
+
try {
|
|
147
|
+
resolve(yield fn(availableConnection.connection));
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
reject(error);
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
availableConnection.busy = false;
|
|
154
|
+
// After query execution, process any queued tasks
|
|
155
|
+
this.processQueue();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
// If no available connections, add to the queue
|
|
160
|
+
this.readQueue.push(execute);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
execute();
|
|
164
|
+
}));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
processQueue() {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
if (this.readQueue.length > 0) {
|
|
170
|
+
const next = this.readQueue.shift();
|
|
171
|
+
if (next) {
|
|
172
|
+
next();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
writeLock(fn, options) {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
yield this.initialized;
|
|
180
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
try {
|
|
182
|
+
yield this.locks
|
|
183
|
+
.acquire(LockType.WRITE, () => __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
resolve(yield fn(this.writeConnection));
|
|
185
|
+
}), { timeout: options === null || options === void 0 ? void 0 : options.timeoutMs })
|
|
186
|
+
.then(() => {
|
|
187
|
+
// flush updates once a write lock has been released
|
|
188
|
+
this.writeConnection.flushUpdates();
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
catch (ex) {
|
|
192
|
+
reject(ex);
|
|
193
|
+
}
|
|
194
|
+
}));
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
readTransaction(fn, options) {
|
|
198
|
+
return this.readLock((ctx) => this.internalTransaction(ctx, fn));
|
|
199
|
+
}
|
|
200
|
+
writeTransaction(fn, options) {
|
|
201
|
+
return this.writeLock((ctx) => this.internalTransaction(ctx, fn));
|
|
202
|
+
}
|
|
203
|
+
getAll(sql, parameters) {
|
|
204
|
+
return this.readLock((ctx) => ctx.getAll(sql, parameters));
|
|
205
|
+
}
|
|
206
|
+
getOptional(sql, parameters) {
|
|
207
|
+
return this.readLock((ctx) => ctx.getOptional(sql, parameters));
|
|
208
|
+
}
|
|
209
|
+
get(sql, parameters) {
|
|
210
|
+
return this.readLock((ctx) => ctx.get(sql, parameters));
|
|
211
|
+
}
|
|
212
|
+
execute(query, params) {
|
|
213
|
+
return this.writeLock((ctx) => ctx.execute(query, params));
|
|
214
|
+
}
|
|
215
|
+
executeBatch(query_1) {
|
|
216
|
+
return __awaiter(this, arguments, void 0, function* (query, params = []) {
|
|
217
|
+
return this.writeLock((ctx) => ctx.executeBatch(query, params));
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
internalTransaction(connection, fn) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
let finalized = false;
|
|
223
|
+
const commit = () => __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
if (finalized) {
|
|
225
|
+
return { rowsAffected: 0 };
|
|
226
|
+
}
|
|
227
|
+
finalized = true;
|
|
228
|
+
return connection.execute('COMMIT');
|
|
229
|
+
});
|
|
230
|
+
const rollback = () => __awaiter(this, void 0, void 0, function* () {
|
|
231
|
+
if (finalized) {
|
|
232
|
+
return { rowsAffected: 0 };
|
|
233
|
+
}
|
|
234
|
+
finalized = true;
|
|
235
|
+
return connection.execute('ROLLBACK');
|
|
236
|
+
});
|
|
237
|
+
try {
|
|
238
|
+
yield connection.execute('BEGIN');
|
|
239
|
+
const result = yield fn({
|
|
240
|
+
execute: (query, params) => connection.execute(query, params),
|
|
241
|
+
get: (query, params) => connection.get(query, params),
|
|
242
|
+
getAll: (query, params) => connection.getAll(query, params),
|
|
243
|
+
getOptional: (query, params) => connection.getOptional(query, params),
|
|
244
|
+
commit,
|
|
245
|
+
rollback
|
|
246
|
+
});
|
|
247
|
+
yield commit();
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
catch (ex) {
|
|
251
|
+
yield rollback();
|
|
252
|
+
throw ex;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
refreshSchema() {
|
|
257
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
258
|
+
yield this.initialized;
|
|
259
|
+
yield this.writeConnection.refreshSchema();
|
|
260
|
+
if (this.readConnections) {
|
|
261
|
+
for (let readConnection of this.readConnections) {
|
|
262
|
+
yield readConnection.connection.refreshSchema();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { OPSQLiteDBAdapter } from './OPSqliteAdapter';
|
|
2
|
+
import { DEFAULT_SQLITE_OPTIONS } from './SqliteOptions';
|
|
3
|
+
export class OPSqliteOpenFactory {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.options = options;
|
|
6
|
+
this.sqliteOptions = Object.assign(Object.assign({}, DEFAULT_SQLITE_OPTIONS), this.options.sqliteOptions);
|
|
7
|
+
}
|
|
8
|
+
openDB() {
|
|
9
|
+
return new OPSQLiteDBAdapter({
|
|
10
|
+
name: this.options.dbFilename,
|
|
11
|
+
dbLocation: this.options.dbLocation,
|
|
12
|
+
sqliteOptions: this.sqliteOptions
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -24,7 +24,7 @@ export interface SqliteOptions {
|
|
|
24
24
|
* Encryption key for the database.
|
|
25
25
|
* If set, the database will be encrypted using SQLCipher.
|
|
26
26
|
*/
|
|
27
|
-
encryptionKey?: string;
|
|
27
|
+
encryptionKey?: string | null;
|
|
28
28
|
/**
|
|
29
29
|
* Load extensions using the path and entryPoint.
|
|
30
30
|
* More info can be found here https://op-engineering.github.io/op-sqlite/docs/api#loading-extensions.
|
|
@@ -49,4 +49,3 @@ declare enum SqliteSynchronous {
|
|
|
49
49
|
}
|
|
50
50
|
export declare const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions>;
|
|
51
51
|
export {};
|
|
52
|
-
//# sourceMappingURL=SqliteOptions.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SQLite journal mode. Set on the primary connection.
|
|
2
|
+
// This library is written with WAL mode in mind - other modes may cause
|
|
3
|
+
// unexpected locking behavior.
|
|
4
|
+
var SqliteJournalMode;
|
|
5
|
+
(function (SqliteJournalMode) {
|
|
6
|
+
// Use a write-ahead log instead of a rollback journal.
|
|
7
|
+
// This provides good performance and concurrency.
|
|
8
|
+
SqliteJournalMode["wal"] = "WAL";
|
|
9
|
+
SqliteJournalMode["delete"] = "DELETE";
|
|
10
|
+
SqliteJournalMode["truncate"] = "TRUNCATE";
|
|
11
|
+
SqliteJournalMode["persist"] = "PERSIST";
|
|
12
|
+
SqliteJournalMode["memory"] = "MEMORY";
|
|
13
|
+
SqliteJournalMode["off"] = "OFF";
|
|
14
|
+
})(SqliteJournalMode || (SqliteJournalMode = {}));
|
|
15
|
+
// SQLite file commit mode.
|
|
16
|
+
var SqliteSynchronous;
|
|
17
|
+
(function (SqliteSynchronous) {
|
|
18
|
+
SqliteSynchronous["normal"] = "NORMAL";
|
|
19
|
+
SqliteSynchronous["full"] = "FULL";
|
|
20
|
+
SqliteSynchronous["off"] = "OFF";
|
|
21
|
+
})(SqliteSynchronous || (SqliteSynchronous = {}));
|
|
22
|
+
export const DEFAULT_SQLITE_OPTIONS = {
|
|
23
|
+
journalMode: SqliteJournalMode.wal,
|
|
24
|
+
synchronous: SqliteSynchronous.normal,
|
|
25
|
+
journalSizeLimit: 6 * 1024 * 1024,
|
|
26
|
+
lockTimeoutMs: 30000,
|
|
27
|
+
encryptionKey: null,
|
|
28
|
+
extensions: []
|
|
29
|
+
};
|
package/lib/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { OPSqliteOpenFactory } from './db/OPSqliteDBOpenFactory';
|