@op-engineering/op-sqlite 14.1.4 → 15.0.1
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/android/CMakeLists.txt +23 -62
- package/android/build.gradle +103 -163
- package/android/gradle.properties +5 -1
- package/android/src/main/AndroidManifest.xml +2 -1
- package/cpp/DBHostObject.cpp +17 -0
- package/lib/module/Storage.js +9 -2
- package/lib/module/Storage.js.map +1 -1
- package/lib/{commonjs/index.js → module/functions.js} +20 -82
- package/lib/module/functions.js.map +1 -0
- package/lib/module/index.js +3 -319
- package/lib/module/index.js.map +1 -1
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/Storage.d.ts +4 -0
- package/lib/typescript/src/Storage.d.ts.map +1 -1
- package/lib/typescript/src/functions.d.ts +61 -0
- package/lib/typescript/src/functions.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +14 -62
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/op-sqlite.podspec +2 -11
- package/package.json +48 -19
- package/src/Storage.ts +10 -2
- package/src/functions.ts +456 -0
- package/src/index.ts +14 -445
- package/android/.project +0 -17
- package/android/.settings/org.eclipse.buildship.core.prefs +0 -13
- package/android/src/paper/java/com/op/sqlite/NativeOPSQLiteSpec.java +0 -77
- package/lib/commonjs/NativeOPSQLite.js +0 -9
- package/lib/commonjs/NativeOPSQLite.js.map +0 -1
- package/lib/commonjs/Storage.js +0 -60
- package/lib/commonjs/Storage.js.map +0 -1
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/package.json +0 -1
package/src/functions.ts
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { NativeModules, Platform } from 'react-native';
|
|
2
|
+
import {
|
|
3
|
+
type _InternalDB,
|
|
4
|
+
type DBParams,
|
|
5
|
+
type DB,
|
|
6
|
+
type _PendingTransaction,
|
|
7
|
+
type SQLBatchTuple,
|
|
8
|
+
type BatchQueryResult,
|
|
9
|
+
type Scalar,
|
|
10
|
+
type QueryResult,
|
|
11
|
+
type Transaction,
|
|
12
|
+
type OPSQLiteProxy,
|
|
13
|
+
} from './index';
|
|
14
|
+
|
|
15
|
+
declare global {
|
|
16
|
+
var __OPSQLiteProxy: object | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (global.__OPSQLiteProxy == null) {
|
|
20
|
+
if (NativeModules.OPSQLite == null) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
'Base module not found. Did you do a pod install/clear the gradle cache?'
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Call the synchronous blocking install() function
|
|
27
|
+
const installed = NativeModules.OPSQLite.install();
|
|
28
|
+
if (!installed) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings, check the native logs for more info`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check again if the constructor now exists. If not, throw an error.
|
|
35
|
+
if (global.__OPSQLiteProxy == null) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
'OPSqlite native object is not available. Something is wrong. Check the native logs for more information.'
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const proxy = global.__OPSQLiteProxy;
|
|
43
|
+
export const OPSQLite = proxy as OPSQLiteProxy;
|
|
44
|
+
|
|
45
|
+
function enhanceDB(db: _InternalDB, options: DBParams): DB {
|
|
46
|
+
const lock = {
|
|
47
|
+
queue: [] as _PendingTransaction[],
|
|
48
|
+
inProgress: false,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const startNextTransaction = () => {
|
|
52
|
+
if (lock.inProgress) {
|
|
53
|
+
// Transaction is already in process bail out
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (lock.queue.length) {
|
|
58
|
+
lock.inProgress = true;
|
|
59
|
+
const tx = lock.queue.shift();
|
|
60
|
+
|
|
61
|
+
if (!tx) {
|
|
62
|
+
throw new Error('Could not get a operation on database');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setImmediate(() => {
|
|
66
|
+
tx.start();
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function sanitizeArrayBuffersInArray(
|
|
72
|
+
params?: any[] | any[][]
|
|
73
|
+
): any[] | undefined {
|
|
74
|
+
if (!params) {
|
|
75
|
+
return params;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return params.map((p) => {
|
|
79
|
+
if (Array.isArray(p)) {
|
|
80
|
+
return sanitizeArrayBuffersInArray(p);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (ArrayBuffer.isView(p)) {
|
|
84
|
+
return p.buffer;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return p;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// spreading the object does not work with HostObjects (db)
|
|
92
|
+
// We need to manually assign the fields
|
|
93
|
+
let enhancedDb = {
|
|
94
|
+
delete: db.delete,
|
|
95
|
+
attach: db.attach,
|
|
96
|
+
detach: db.detach,
|
|
97
|
+
executeBatch: async (
|
|
98
|
+
commands: SQLBatchTuple[]
|
|
99
|
+
): Promise<BatchQueryResult> => {
|
|
100
|
+
const sanitizedCommands = commands.map(([query, params]) => {
|
|
101
|
+
if (params) {
|
|
102
|
+
return [query, sanitizeArrayBuffersInArray(params)];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return [query];
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
async function run() {
|
|
109
|
+
try {
|
|
110
|
+
enhancedDb.executeSync('BEGIN TRANSACTION;');
|
|
111
|
+
|
|
112
|
+
let res = await db.executeBatch(sanitizedCommands as any[]);
|
|
113
|
+
|
|
114
|
+
enhancedDb.executeSync('COMMIT;');
|
|
115
|
+
|
|
116
|
+
return res;
|
|
117
|
+
} catch (executionError) {
|
|
118
|
+
try {
|
|
119
|
+
enhancedDb.executeSync('ROLLBACK;');
|
|
120
|
+
} catch (rollbackError) {
|
|
121
|
+
throw rollbackError;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
throw executionError;
|
|
125
|
+
} finally {
|
|
126
|
+
lock.inProgress = false;
|
|
127
|
+
startNextTransaction();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return await new Promise((resolve, reject) => {
|
|
132
|
+
const tx: _PendingTransaction = {
|
|
133
|
+
start: () => {
|
|
134
|
+
run().then(resolve).catch(reject);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
lock.queue.push(tx);
|
|
139
|
+
startNextTransaction();
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
loadFile: db.loadFile,
|
|
143
|
+
updateHook: db.updateHook,
|
|
144
|
+
commitHook: db.commitHook,
|
|
145
|
+
rollbackHook: db.rollbackHook,
|
|
146
|
+
loadExtension: db.loadExtension,
|
|
147
|
+
getDbPath: db.getDbPath,
|
|
148
|
+
reactiveExecute: db.reactiveExecute,
|
|
149
|
+
sync: db.sync,
|
|
150
|
+
close: db.close,
|
|
151
|
+
executeWithHostObjects: async (
|
|
152
|
+
query: string,
|
|
153
|
+
params?: Scalar[]
|
|
154
|
+
): Promise<QueryResult> => {
|
|
155
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
156
|
+
|
|
157
|
+
return sanitizedParams
|
|
158
|
+
? await db.executeWithHostObjects(query, sanitizedParams as Scalar[])
|
|
159
|
+
: await db.executeWithHostObjects(query);
|
|
160
|
+
},
|
|
161
|
+
executeRaw: async (query: string, params?: Scalar[]) => {
|
|
162
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
163
|
+
|
|
164
|
+
return db.executeRaw(query, sanitizedParams as Scalar[]);
|
|
165
|
+
},
|
|
166
|
+
executeRawSync: (query: string, params?: Scalar[]) => {
|
|
167
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
168
|
+
return db.executeRawSync(query, sanitizedParams as Scalar[]);
|
|
169
|
+
},
|
|
170
|
+
// Wrapper for executeRaw, drizzleORM uses this function
|
|
171
|
+
// at some point I changed the API but they did not pin their dependency to a specific version
|
|
172
|
+
// so re-inserting this so it starts working again
|
|
173
|
+
executeRawAsync: async (query: string, params?: Scalar[]) => {
|
|
174
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
175
|
+
|
|
176
|
+
return db.executeRaw(query, sanitizedParams as Scalar[]);
|
|
177
|
+
},
|
|
178
|
+
executeSync: (query: string, params?: Scalar[]): QueryResult => {
|
|
179
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
180
|
+
|
|
181
|
+
let intermediateResult = sanitizedParams
|
|
182
|
+
? db.executeSync(query, sanitizedParams as Scalar[])
|
|
183
|
+
: db.executeSync(query);
|
|
184
|
+
|
|
185
|
+
let rows: Record<string, Scalar>[] = [];
|
|
186
|
+
for (let i = 0; i < (intermediateResult.rawRows?.length ?? 0); i++) {
|
|
187
|
+
let row: Record<string, Scalar> = {};
|
|
188
|
+
let rawRow = intermediateResult.rawRows![i]!;
|
|
189
|
+
for (let j = 0; j < intermediateResult.columnNames!.length; j++) {
|
|
190
|
+
let columnName = intermediateResult.columnNames![j]!;
|
|
191
|
+
let value = rawRow[j]!;
|
|
192
|
+
|
|
193
|
+
row[columnName] = value;
|
|
194
|
+
}
|
|
195
|
+
rows.push(row);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let res = {
|
|
199
|
+
...intermediateResult,
|
|
200
|
+
rows,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
delete res.rawRows;
|
|
204
|
+
|
|
205
|
+
return res;
|
|
206
|
+
},
|
|
207
|
+
executeAsync: async (
|
|
208
|
+
query: string,
|
|
209
|
+
params?: Scalar[] | undefined
|
|
210
|
+
): Promise<QueryResult> => {
|
|
211
|
+
return db.execute(query, params);
|
|
212
|
+
},
|
|
213
|
+
execute: async (
|
|
214
|
+
query: string,
|
|
215
|
+
params?: Scalar[] | undefined
|
|
216
|
+
): Promise<QueryResult> => {
|
|
217
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
218
|
+
|
|
219
|
+
let intermediateResult = await db.execute(
|
|
220
|
+
query,
|
|
221
|
+
sanitizedParams as Scalar[]
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
let rows: Record<string, Scalar>[] = [];
|
|
225
|
+
for (let i = 0; i < (intermediateResult.rawRows?.length ?? 0); i++) {
|
|
226
|
+
let row: Record<string, Scalar> = {};
|
|
227
|
+
let rawRow = intermediateResult.rawRows![i]!;
|
|
228
|
+
for (let j = 0; j < intermediateResult.columnNames!.length; j++) {
|
|
229
|
+
let columnName = intermediateResult.columnNames![j]!;
|
|
230
|
+
let value = rawRow[j]!;
|
|
231
|
+
|
|
232
|
+
row[columnName] = value;
|
|
233
|
+
}
|
|
234
|
+
rows.push(row);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let res = {
|
|
238
|
+
...intermediateResult,
|
|
239
|
+
rows,
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
delete res.rawRows;
|
|
243
|
+
|
|
244
|
+
return res;
|
|
245
|
+
},
|
|
246
|
+
prepareStatement: (query: string) => {
|
|
247
|
+
const stmt = db.prepareStatement(query);
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
bindSync: (params: Scalar[]) => {
|
|
251
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
252
|
+
|
|
253
|
+
stmt.bindSync(sanitizedParams!);
|
|
254
|
+
},
|
|
255
|
+
bind: async (params: Scalar[]) => {
|
|
256
|
+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
|
|
257
|
+
|
|
258
|
+
await stmt.bind(sanitizedParams!);
|
|
259
|
+
},
|
|
260
|
+
execute: stmt.execute,
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
transaction: async (
|
|
264
|
+
fn: (tx: Transaction) => Promise<void>
|
|
265
|
+
): Promise<void> => {
|
|
266
|
+
let isFinalized = false;
|
|
267
|
+
|
|
268
|
+
const execute = async (query: string, params?: Scalar[]) => {
|
|
269
|
+
if (isFinalized) {
|
|
270
|
+
throw Error(
|
|
271
|
+
`OP-Sqlite Error: Database: ${
|
|
272
|
+
options.name || options.url
|
|
273
|
+
}. Cannot execute query on finalized transaction`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
return await enhancedDb.execute(query, params);
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const commit = async (): Promise<QueryResult> => {
|
|
280
|
+
if (isFinalized) {
|
|
281
|
+
throw Error(
|
|
282
|
+
`OP-Sqlite Error: Database: ${
|
|
283
|
+
options.name || options.url
|
|
284
|
+
}. Cannot execute query on finalized transaction`
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
const result = enhancedDb.executeSync('COMMIT;');
|
|
288
|
+
|
|
289
|
+
await db.flushPendingReactiveQueries();
|
|
290
|
+
|
|
291
|
+
isFinalized = true;
|
|
292
|
+
return result;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const rollback = (): QueryResult => {
|
|
296
|
+
if (isFinalized) {
|
|
297
|
+
throw Error(
|
|
298
|
+
`OP-Sqlite Error: Database: ${
|
|
299
|
+
options.name || options.url
|
|
300
|
+
}. Cannot execute query on finalized transaction`
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
const result = enhancedDb.executeSync('ROLLBACK;');
|
|
304
|
+
isFinalized = true;
|
|
305
|
+
return result;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
async function run() {
|
|
309
|
+
try {
|
|
310
|
+
enhancedDb.executeSync('BEGIN TRANSACTION;');
|
|
311
|
+
|
|
312
|
+
await fn({
|
|
313
|
+
commit,
|
|
314
|
+
execute,
|
|
315
|
+
rollback,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
if (!isFinalized) {
|
|
319
|
+
commit();
|
|
320
|
+
}
|
|
321
|
+
} catch (executionError) {
|
|
322
|
+
if (!isFinalized) {
|
|
323
|
+
try {
|
|
324
|
+
rollback();
|
|
325
|
+
} catch (rollbackError) {
|
|
326
|
+
throw rollbackError;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
throw executionError;
|
|
331
|
+
} finally {
|
|
332
|
+
lock.inProgress = false;
|
|
333
|
+
isFinalized = false;
|
|
334
|
+
startNextTransaction();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return await new Promise((resolve, reject) => {
|
|
339
|
+
const tx: _PendingTransaction = {
|
|
340
|
+
start: () => {
|
|
341
|
+
run().then(resolve).catch(reject);
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
lock.queue.push(tx);
|
|
346
|
+
startNextTransaction();
|
|
347
|
+
});
|
|
348
|
+
},
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
return enhancedDb;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Open a replicating connection via libsql to a turso db
|
|
356
|
+
* libsql needs to be enabled on your package.json
|
|
357
|
+
*/
|
|
358
|
+
export const openSync = (params: {
|
|
359
|
+
url: string;
|
|
360
|
+
authToken: string;
|
|
361
|
+
name: string;
|
|
362
|
+
location?: string;
|
|
363
|
+
libsqlSyncInterval?: number;
|
|
364
|
+
libsqlOffline?: boolean;
|
|
365
|
+
encryptionKey?: string;
|
|
366
|
+
remoteEncryptionKey?: string;
|
|
367
|
+
}): DB => {
|
|
368
|
+
if (!isLibsql()) {
|
|
369
|
+
throw new Error('This function is only available for libsql');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const db = OPSQLite.openSync(params);
|
|
373
|
+
const enhancedDb = enhanceDB(db, params);
|
|
374
|
+
|
|
375
|
+
return enhancedDb;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Open a remote connection via libsql to a turso db
|
|
380
|
+
* libsql needs to be enabled on your package.json
|
|
381
|
+
*/
|
|
382
|
+
export const openRemote = (params: { url: string; authToken: string }): DB => {
|
|
383
|
+
if (!isLibsql()) {
|
|
384
|
+
throw new Error('This function is only available for libsql');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const db = OPSQLite.openRemote(params);
|
|
388
|
+
const enhancedDb = enhanceDB(db, params);
|
|
389
|
+
|
|
390
|
+
return enhancedDb;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Open a connection to a local sqlite or sqlcipher database
|
|
395
|
+
* If you want libsql remote or sync connections, use openSync or openRemote
|
|
396
|
+
*/
|
|
397
|
+
export const open = (params: {
|
|
398
|
+
name: string;
|
|
399
|
+
location?: string;
|
|
400
|
+
encryptionKey?: string;
|
|
401
|
+
}): DB => {
|
|
402
|
+
if (params.location?.startsWith('file://')) {
|
|
403
|
+
console.warn(
|
|
404
|
+
"[op-sqlite] You are passing a path with 'file://' prefix, it's automatically removed"
|
|
405
|
+
);
|
|
406
|
+
params.location = params.location.substring(7);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const db = OPSQLite.open(params);
|
|
410
|
+
const enhancedDb = enhanceDB(db, params);
|
|
411
|
+
|
|
412
|
+
return enhancedDb;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Moves the database from the assets folder to the default path (check the docs) or to a custom path
|
|
417
|
+
* It DOES NOT OVERWRITE the database if it already exists in the destination path
|
|
418
|
+
* if you want to overwrite the database, you need to pass the overwrite flag as true
|
|
419
|
+
* @param args object with the parameters for the operaiton
|
|
420
|
+
* @returns promise, rejects if failed to move the database, resolves if the operation was successful
|
|
421
|
+
*/
|
|
422
|
+
export const moveAssetsDatabase = async (args: {
|
|
423
|
+
filename: string;
|
|
424
|
+
path?: string;
|
|
425
|
+
overwrite?: boolean;
|
|
426
|
+
}): Promise<boolean> => {
|
|
427
|
+
return NativeModules.OPSQLite.moveAssetsDatabase(args);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Used to load a dylib file that contains a sqlite 3 extension/plugin
|
|
432
|
+
* It returns the raw path to the actual file which then needs to be passed to the loadExtension function
|
|
433
|
+
* Check the docs for more information
|
|
434
|
+
* @param bundle the iOS bundle identifier of the .framework
|
|
435
|
+
* @param name the file name of the dylib file
|
|
436
|
+
* @returns
|
|
437
|
+
*/
|
|
438
|
+
export const getDylibPath = (bundle: string, name: string): string => {
|
|
439
|
+
return NativeModules.OPSQLite.getDylibPath(bundle, name);
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
export const isSQLCipher = (): boolean => {
|
|
443
|
+
return OPSQLite.isSQLCipher();
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
export const isLibsql = (): boolean => {
|
|
447
|
+
return OPSQLite.isLibsql();
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
export const isIOSEmbeeded = (): boolean => {
|
|
451
|
+
if (Platform.OS !== 'ios') {
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return OPSQLite.isIOSEmbedded();
|
|
456
|
+
};
|