@op-engineering/op-sqlite 1.0.13 → 1.0.14

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/README.md CHANGED
@@ -43,9 +43,9 @@ const largeDb = open({
43
43
 
44
44
  Note that on iOS the file system is sand-boxed, so you cannot access files/directories outside your app bundle directories.
45
45
 
46
- ## In memory
46
+ ## In-memory
47
47
 
48
- Using SQLite in memory mode is supported:
48
+ Using SQLite in-memory mode is supported:
49
49
 
50
50
  ```ts
51
51
  import { open } from '@op-engineering/op-sqlite';
@@ -69,15 +69,22 @@ db = {
69
69
  delete: () => void,
70
70
  attach: (dbNameToAttach: string, alias: string, location?: string) => void,
71
71
  detach: (alias: string) => void,
72
- transaction: (fn: (tx: Transaction) => void) => Promise<void>,
72
+ transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>,
73
73
  execute: (query: string, params?: any[]) => QueryResult,
74
- executeAsync: (
75
- query: string,
76
- params?: any[]
77
- ) => Promise<QueryResult>,
78
- executeBatch: (commands: SQLBatchParams[]) => BatchQueryResult,
79
- executeBatchAsync: (commands: SQLBatchParams[]) => Promise<BatchQueryResult>,
80
- loadFile: (location: string) => Promise<FileLoadResult>
74
+ executeAsync: (query: string, params?: any[]) => Promise<QueryResult>,
75
+ executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult,
76
+ executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>,
77
+ loadFile: (location: string) => Promise<FileLoadResult>,
78
+ updateHook: (
79
+ callback: ((params: {
80
+ table: string;
81
+ operation: UpdateHookOperation;
82
+ row?: any;
83
+ rowId: number;
84
+ }) => void) | null
85
+ ) => void,
86
+ commitHook: (callback: (() => void) | null) => void,
87
+ rollbackHook: (callback: (() => void) | null) => void
81
88
  }
82
89
  ```
83
90
 
@@ -281,7 +288,7 @@ const { rowsAffected, commands } = db
281
288
  });
282
289
  ```
283
290
 
284
- ## Update hook
291
+ ## Hooks
285
292
 
286
293
  You can subscribe to changes in your database by using an update hook:
287
294
 
@@ -303,6 +310,46 @@ db.execute('INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)', [
303
310
  ]);
304
311
  ```
305
312
 
313
+ Same goes for commit and rollback hooks
314
+
315
+ ```ts
316
+ // will fire whenever a transaction commits
317
+ db.commitHook(() => {
318
+ console.log('Transaction commmitted!');
319
+ });
320
+
321
+ db.rollbackHook(() => {
322
+ console.log('Transaction rolled back!');
323
+ });
324
+
325
+ // will fire the commit hook
326
+ db.transaction(async (tx) => {
327
+ tx.execute(
328
+ 'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
329
+ [id, name, age, networth]
330
+ );
331
+ });
332
+
333
+ // will fire the rollback hook
334
+ try {
335
+ await db.transaction(async (tx) => {
336
+ throw new Error('Test Error');
337
+ });
338
+ } catch (e) {
339
+ // intentionally left blank
340
+ }
341
+ ```
342
+
343
+ You can pass `null`` to remove hooks at any moment:
344
+
345
+ ```ts
346
+ db.updateHook(null);
347
+
348
+ db.commitHook(null);
349
+
350
+ db.rollbackHook(null);
351
+ ```
352
+
306
353
  ## Use built-in SQLite
307
354
 
308
355
  On iOS you can use the embedded SQLite, when running `pod-install` add an environment flag:
@@ -1,8 +1,5 @@
1
1
  package com.op.sqlite;
2
2
 
3
- import android.util.Log;
4
-
5
- import com.facebook.react.bridge.ReactApplicationContext;
6
3
  import com.facebook.react.bridge.ReactContext;
7
4
  import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
8
5
 
package/cpp/bindings.cpp CHANGED
@@ -449,6 +449,13 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
449
449
 
450
450
  auto dbName = args[0].asString(rt).utf8(rt);
451
451
  auto callback = std::make_shared<jsi::Value>(rt, args[1]);
452
+
453
+ if (callback->isUndefined() || callback->isNull())
454
+ {
455
+ unregisterUpdateHook(dbName);
456
+ return {};
457
+ }
458
+
452
459
  updateHooks[dbName] = callback;
453
460
 
454
461
  auto hook = [&rt, callback](std::string dbName, std::string tableName, std::string operation, int rowId) {
@@ -491,6 +498,11 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
491
498
 
492
499
  auto dbName = args[0].asString(rt).utf8(rt);
493
500
  auto callback = std::make_shared<jsi::Value>(rt, args[1]);
501
+ if (callback->isUndefined() || callback->isNull())
502
+ {
503
+ unregisterCommitHook(dbName);
504
+ return {};
505
+ }
494
506
  commitHooks[dbName] = callback;
495
507
 
496
508
  auto hook = [&rt, callback](std::string dbName) {
@@ -504,7 +516,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
504
516
 
505
517
  return {};
506
518
  });
507
-
519
+
508
520
  auto rollbackHook = HOSTFN("rollbackHook", 2)
509
521
  {
510
522
  if (sizeof(args) < 2)
@@ -515,6 +527,12 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
515
527
 
516
528
  auto dbName = args[0].asString(rt).utf8(rt);
517
529
  auto callback = std::make_shared<jsi::Value>(rt, args[1]);
530
+
531
+ if (callback->isUndefined() || callback->isNull())
532
+ {
533
+ unregisterRollbackHook(dbName);
534
+ return {};
535
+ }
518
536
  rollbackHooks[dbName] = callback;
519
537
 
520
538
  auto hook = [&rt, callback](std::string dbName) {
@@ -529,7 +547,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
529
547
 
530
548
  return {};
531
549
  });
532
-
550
+
533
551
  jsi::Object module = jsi::Object(rt);
534
552
 
535
553
  module.setProperty(rt, "open", std::move(open));
package/cpp/bridge.cpp CHANGED
@@ -367,27 +367,30 @@ namespace opsqlite {
367
367
  }
368
368
  i++;
369
369
  }
370
- results->push_back(row);
370
+ if (results != nullptr) {
371
+ results->push_back(row);
372
+ }
371
373
  break;
372
374
  }
373
375
 
374
376
  case SQLITE_DONE:
375
- i = 0;
376
- count = sqlite3_column_count(statement);
377
-
378
- while (i < count)
379
- {
380
- column_name = sqlite3_column_name(statement, i);
381
- const char *type = sqlite3_column_decltype(statement, i);
382
- auto metadata = DynamicHostObject();
383
- metadata.fields.push_back(std::make_pair("name", column_name));
384
- metadata.fields.push_back(std::make_pair("index", i));
385
- metadata.fields.push_back(std::make_pair("type", type));
377
+ if (metadatas != nullptr) {
378
+ i = 0;
379
+ count = sqlite3_column_count(statement);
386
380
 
387
- metadatas->push_back(metadata);
388
- i++;
381
+ while (i < count)
382
+ {
383
+ column_name = sqlite3_column_name(statement, i);
384
+ const char *type = sqlite3_column_decltype(statement, i);
385
+ auto metadata = DynamicHostObject();
386
+ metadata.fields.push_back(std::make_pair("name", column_name));
387
+ metadata.fields.push_back(std::make_pair("index", i));
388
+ metadata.fields.push_back(std::make_pair("type", type == NULL ? "UNKNOWN" : type));
389
+
390
+ metadatas->push_back(metadata);
391
+ i++;
392
+ }
389
393
  }
390
-
391
394
  isConsuming = false;
392
395
  break;
393
396
 
@@ -557,6 +560,28 @@ namespace opsqlite {
557
560
  };
558
561
  }
559
562
 
563
+ BridgeResult unregisterUpdateHook(std::string const dbName) {
564
+ if (dbMap.count(dbName) == 0)
565
+ {
566
+ return {
567
+ SQLiteError,
568
+ "[op-sqlite] Database not opened: " + dbName
569
+ };
570
+ }
571
+
572
+ sqlite3 *db = dbMap[dbName];
573
+ updateCallbackMap.erase(dbName);
574
+
575
+ sqlite3_update_hook(
576
+ db,
577
+ NULL,
578
+ NULL);
579
+
580
+ return {
581
+ SQLiteOk
582
+ };
583
+ }
584
+
560
585
  int commit_callback(void *dbName) {
561
586
  std::string &strDbName = *(static_cast<std::string*>(dbName));
562
587
  auto callback = commitCallbackMap[strDbName];
@@ -596,6 +621,27 @@ namespace opsqlite {
596
621
  };
597
622
  }
598
623
 
624
+ BridgeResult unregisterCommitHook(std::string const dbName) {
625
+ if (dbMap.count(dbName) == 0)
626
+ {
627
+ return {
628
+ SQLiteError,
629
+ "[op-sqlite] Database not opened: " + dbName
630
+ };
631
+ }
632
+
633
+ sqlite3 *db = dbMap[dbName];
634
+ commitCallbackMap.erase(dbName);
635
+ sqlite3_commit_hook(
636
+ db,
637
+ NULL,
638
+ NULL);
639
+
640
+ return {
641
+ SQLiteOk
642
+ };
643
+ }
644
+
599
645
  void rollback_callback(void *dbName) {
600
646
  std::string &strDbName = *(static_cast<std::string*>(dbName));
601
647
  auto callback = rollbackCallbackMap[strDbName];
@@ -632,4 +678,26 @@ namespace opsqlite {
632
678
  SQLiteOk
633
679
  };
634
680
  }
681
+
682
+ BridgeResult unregisterRollbackHook(std::string const dbName) {
683
+ if (dbMap.count(dbName) == 0)
684
+ {
685
+ return {
686
+ SQLiteError,
687
+ "[op-sqlite] Database not opened: " + dbName
688
+ };
689
+ }
690
+
691
+ sqlite3 *db = dbMap[dbName];
692
+ rollbackCallbackMap.erase(dbName);
693
+
694
+ sqlite3_rollback_hook(
695
+ db,
696
+ NULL,
697
+ NULL);
698
+
699
+ return {
700
+ SQLiteOk
701
+ };
702
+ }
635
703
  }
package/cpp/bridge.h CHANGED
@@ -33,11 +33,13 @@ void sqliteCloseAll();
33
33
 
34
34
  BridgeResult registerUpdateHook(std::string const dbName,
35
35
  std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)> const callback);
36
+ BridgeResult unregisterUpdateHook(std::string const dbName);
36
37
  BridgeResult registerCommitHook(std::string const dbName,
37
38
  std::function<void (std::string dbName)> const callback);
39
+ BridgeResult unregisterCommitHook(std::string const dbName);
38
40
  BridgeResult registerRollbackHook(std::string const dbName,
39
41
  std::function<void (std::string dbName)> const callback);
40
-
42
+ BridgeResult unregisterRollbackHook(std::string const dbName);
41
43
  }
42
44
 
43
45
  #endif /* bridge_h */
package/ios/OPSQLite.mm CHANGED
@@ -1,11 +1,8 @@
1
1
  #import "OPSQLite.h"
2
-
3
2
  #import <React/RCTBridge+Private.h>
4
-
5
3
  #import <React/RCTUtils.h>
6
4
  #import <ReactCommon/RCTTurboModule.h>
7
5
  #import <jsi/jsi.h>
8
-
9
6
  #import "../cpp/bindings.h"
10
7
 
11
8
  @implementation OPSQLite
@@ -1 +1 @@
1
- {"version":3,"names":["global","__OPSQLiteProxy","OPSQLiteModule","NativeModules","OPSQLite","Error","nativeCallSyncHook","install","result","proxy","locks","enhanceQueryResult","rows","_array","length","item","idx","_open","open","dbName","location","inMemory","queue","inProgress","_close","close","_execute","execute","query","params","sanitizedParams","map","p","ArrayBuffer","isView","buffer","_executeAsync","executeAsync","res","transaction","fn","isFinalized","commit","rollback","run","executionError","rollbackError","startNextTransaction","Promise","resolve","reject","tx","start","then","catch","push","shift","setImmediate","options","name","delete","attach","dbNameToAttach","alias","detach","executeBatch","commands","executeBatchAsync","loadFile","updateHook","callback","commitHook","rollbackHook"],"sources":["index.ts"],"sourcesContent":["import { NativeModules } from 'react-native';\n\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __OPSQLiteProxy: object | undefined;\n}\n\nif (global.__OPSQLiteProxy == null) {\n const OPSQLiteModule = NativeModules.OPSQLite;\n\n if (OPSQLiteModule == null) {\n throw new Error('Base module not found. Maybe try rebuilding the app.');\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || OPSQLiteModule.install == null) {\n throw new Error(\n 'Failed to install op-sqlite: React Native is not running on-device. OPSQLite can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = OPSQLiteModule.install();\n if (result !== true) {\n throw new Error(\n `Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n }\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__OPSQLiteProxy == null) {\n throw new Error(\n 'Failed to install op-sqlite, the native initializer function does not exist. Are you trying to use OPSQLite from different JS Runtimes?'\n );\n }\n}\n\nconst proxy = global.__OPSQLiteProxy;\nexport const OPSQLite = proxy as ISQLite;\n\n/**\n * Object returned by SQL Query executions {\n * insertId: Represent the auto-generated row id if applicable\n * rowsAffected: Number of affected rows if result of a update query\n * message: if status === 1, here you will find error description\n * rows: if status is undefined or 0 this object will contain the query results\n * }\n *\n * @interface QueryResult\n */\nexport type QueryResult = {\n insertId?: number;\n rowsAffected: number;\n rows?: {\n /** Raw array with all dataset */\n _array: any[];\n /** The lengh of the dataset */\n length: number;\n /** A convenience function to acess the index based the row object\n * @param idx the row index\n * @returns the row structure identified by column names\n */\n item: (idx: number) => any;\n };\n /**\n * Query metadata, avaliable only for select query results\n */\n metadata?: ColumnMetadata[];\n};\n\n/**\n * Column metadata\n * Describes some information about columns fetched by the query\n */\nexport type ColumnMetadata = {\n /** The name used for this column for this resultset */\n name: string;\n /** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. \"UNKNOWN\" for dynamic values, like function returned ones. */\n type: string;\n /**\n * The index for this column for this resultset*/\n index: number;\n};\n\n/**\n * Allows the execution of bulk of sql commands\n * inside a transaction\n * If a single query must be executed many times with different arguments, its preferred\n * to declare it a single time, and use an array of array parameters.\n */\nexport type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];\n\nexport type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';\n\n/**\n * status: 0 or undefined for correct execution, 1 for error\n * message: if status === 1, here you will find error description\n * rowsAffected: Number of affected rows if status == 0\n */\nexport type BatchQueryResult = {\n rowsAffected?: number;\n};\n\n/**\n * Result of loading a file and executing every line as a SQL command\n * Similar to BatchQueryResult\n */\nexport interface FileLoadResult extends BatchQueryResult {\n commands?: number;\n}\n\nexport interface Transaction {\n commit: () => QueryResult;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ) => Promise<QueryResult>;\n rollback: () => QueryResult;\n}\n\nexport interface PendingTransaction {\n /*\n * The start function should not throw or return a promise because the\n * queue just calls it and does not monitor for failures or completions.\n *\n * It should catch any errors and call the resolve or reject of the wrapping\n * promise when complete.\n *\n * It should also automatically commit or rollback the transaction if needed\n */\n start: () => void;\n}\n\ninterface ISQLite {\n open: (dbName: string, location?: string, inMemory?: boolean) => void;\n close: (dbName: string) => void;\n delete: (dbName: string, location?: string) => void;\n attach: (\n mainDbName: string,\n dbNameToAttach: string,\n alias: string,\n location?: string\n ) => void;\n detach: (mainDbName: string, alias: string) => void;\n transaction: (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n ) => Promise<void>;\n execute: (dbName: string, query: string, params?: any[]) => QueryResult;\n executeAsync: (\n dbName: string,\n query: string,\n params?: any[]\n ) => Promise<QueryResult>;\n executeBatch: (dbName: string, commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (\n dbName: string,\n commands: SQLBatchTuple[]\n ) => Promise<BatchQueryResult>;\n loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;\n updateHook: (\n dbName: string,\n callback: (params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void\n ) => void;\n commitHook: (dbName: string, callback: () => void) => void;\n rollbackHook: (dbName: string, callback: () => void) => void;\n}\n\nconst locks: Record<\n string,\n { queue: PendingTransaction[]; inProgress: boolean }\n> = {};\n\n// Enhance some host functions\n\n// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\nfunction enhanceQueryResult(result: QueryResult): void {\n // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\n if (result.rows == null) {\n result.rows = {\n _array: [],\n length: 0,\n item: (idx: number) => result.rows?._array[idx],\n };\n } else {\n result.rows.item = (idx: number) => result.rows?._array[idx];\n }\n}\n\nconst _open = OPSQLite.open;\nOPSQLite.open = (dbName: string, location?: string, inMemory?: boolean) => {\n _open(dbName, location, !!inMemory);\n\n locks[dbName] = {\n queue: [],\n inProgress: false,\n };\n};\n\nconst _close = OPSQLite.close;\nOPSQLite.close = (dbName: string) => {\n _close(dbName);\n delete locks[dbName];\n};\n\nconst _execute = OPSQLite.execute;\nOPSQLite.execute = (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): QueryResult => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const result = _execute(dbName, query, sanitizedParams);\n enhanceQueryResult(result);\n return result;\n};\n\nconst _executeAsync = OPSQLite.executeAsync;\nOPSQLite.executeAsync = async (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): Promise<QueryResult> => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const res = await _executeAsync(dbName, query, sanitizedParams);\n enhanceQueryResult(res);\n return res;\n};\n\nOPSQLite.transaction = async (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n): Promise<void> => {\n if (!locks[dbName]) {\n throw Error(`SQLite Error: No lock found on db: ${dbName}`);\n }\n\n let isFinalized = false;\n\n // Local transaction context object implementation\n const execute = (query: string, params?: any[]): QueryResult => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.execute(dbName, query, params);\n };\n\n const executeAsync = (query: string, params?: any[] | undefined) => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.executeAsync(dbName, query, params);\n };\n\n const commit = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'COMMIT');\n isFinalized = true;\n return result;\n };\n\n const rollback = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'ROLLBACK');\n isFinalized = true;\n return result;\n };\n\n async function run() {\n try {\n await OPSQLite.executeAsync(dbName, 'BEGIN TRANSACTION');\n\n await fn({\n commit,\n execute,\n executeAsync,\n rollback,\n });\n\n if (!isFinalized) {\n commit();\n }\n } catch (executionError) {\n if (!isFinalized) {\n try {\n rollback();\n } catch (rollbackError) {\n throw rollbackError;\n }\n }\n\n throw executionError;\n } finally {\n locks[dbName].inProgress = false;\n isFinalized = false;\n startNextTransaction(dbName);\n }\n }\n\n return await new Promise((resolve, reject) => {\n const tx: PendingTransaction = {\n start: () => {\n run().then(resolve).catch(reject);\n },\n };\n\n locks[dbName].queue.push(tx);\n startNextTransaction(dbName);\n });\n};\n\nconst startNextTransaction = (dbName: string) => {\n if (!locks[dbName]) {\n throw Error(`Lock not found for db: ${dbName}`);\n }\n\n if (locks[dbName].inProgress) {\n // Transaction is already in process bail out\n return;\n }\n\n if (locks[dbName].queue.length) {\n locks[dbName].inProgress = true;\n const tx = locks[dbName].queue.shift();\n\n if (!tx) {\n throw new Error('Could not get a operation on datebase');\n }\n\n setImmediate(() => {\n tx.start();\n });\n }\n};\n\nexport type OPSQLiteConnection = {\n close: () => void;\n delete: () => void;\n attach: (dbNameToAttach: string, alias: string, location?: string) => void;\n detach: (alias: string) => void;\n transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (query: string, params?: any[]) => Promise<QueryResult>;\n executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;\n loadFile: (location: string) => Promise<FileLoadResult>;\n updateHook: (\n callback: (params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void\n ) => void;\n commitHook: (callback: () => void) => void;\n rollbackHook: (callback: () => void) => void;\n};\n\nexport const open = (options: {\n name: string;\n location?: string;\n inMemory?: boolean;\n}): OPSQLiteConnection => {\n OPSQLite.open(options.name, options.location, options.inMemory);\n\n return {\n close: () => OPSQLite.close(options.name),\n delete: () => OPSQLite.delete(options.name, options.location),\n attach: (dbNameToAttach: string, alias: string, location?: string) =>\n OPSQLite.attach(options.name, dbNameToAttach, alias, location),\n detach: (alias: string) => OPSQLite.detach(options.name, alias),\n transaction: (fn: (tx: Transaction) => Promise<void>) =>\n OPSQLite.transaction(options.name, fn),\n execute: (query: string, params?: any[] | undefined): QueryResult =>\n OPSQLite.execute(options.name, query, params),\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ): Promise<QueryResult> =>\n OPSQLite.executeAsync(options.name, query, params),\n executeBatch: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatch(options.name, commands),\n executeBatchAsync: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatchAsync(options.name, commands),\n loadFile: (location: string) => OPSQLite.loadFile(options.name, location),\n updateHook: (callback) => OPSQLite.updateHook(options.name, callback),\n commitHook: (callback) => OPSQLite.commitHook(options.name, callback),\n rollbackHook: (callback) => OPSQLite.rollbackHook(options.name, callback),\n };\n};\n"],"mappings":";;;;;;AAAA;AAOA,IAAIA,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;EAClC,MAAMC,cAAc,GAAGC,0BAAa,CAACC,QAAQ;EAE7C,IAAIF,cAAc,IAAI,IAAI,EAAE;IAC1B,MAAM,IAAIG,KAAK,CAAC,sDAAsD,CAAC;EACzE;;EAEA;EACA,IAAIL,MAAM,CAACM,kBAAkB,IAAI,IAAI,IAAIJ,cAAc,CAACK,OAAO,IAAI,IAAI,EAAE;IACvE,MAAM,IAAIF,KAAK,CACb,iQAAiQ,CAClQ;EACH;;EAEA;EACA,MAAMG,MAAM,GAAGN,cAAc,CAACK,OAAO,EAAE;EACvC,IAAIC,MAAM,KAAK,IAAI,EAAE;IACnB,MAAM,IAAIH,KAAK,CACZ,iJAAgJG,MAAO,EAAC,CAC1J;EACH;;EAEA;EACA,IAAIR,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;IAClC,MAAM,IAAII,KAAK,CACb,yIAAyI,CAC1I;EACH;AACF;AAEA,MAAMI,KAAK,GAAGT,MAAM,CAACC,eAAe;AAC7B,MAAMG,QAAQ,GAAGK,KAAgB;;AAExC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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AQ,IAAKlE,QAAQ,CAACmE,UAAU,CAACb,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEE,YAAY,EAAGF,QAAQ,IAAKlE,QAAQ,CAACoE,YAAY,CAACd,OAAO,CAACC,IAAI,EAAEW,QAAQ;EAC1E,CAAC;AACH,CAAC;AAAC"}
1
+ {"version":3,"names":["global","__OPSQLiteProxy","OPSQLiteModule","NativeModules","OPSQLite","Error","nativeCallSyncHook","install","result","proxy","locks","enhanceQueryResult","rows","_array","length","item","idx","_open","open","dbName","location","inMemory","queue","inProgress","_close","close","_execute","execute","query","params","sanitizedParams","map","p","ArrayBuffer","isView","buffer","_executeAsync","executeAsync","res","transaction","fn","isFinalized","commit","rollback","run","executionError","rollbackError","startNextTransaction","Promise","resolve","reject","tx","start","then","catch","push","shift","setImmediate","options","name","delete","attach","dbNameToAttach","alias","detach","executeBatch","commands","executeBatchAsync","loadFile","updateHook","callback","commitHook","rollbackHook"],"sources":["index.ts"],"sourcesContent":["import { NativeModules } from 'react-native';\n\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __OPSQLiteProxy: object | undefined;\n}\n\nif (global.__OPSQLiteProxy == null) {\n const OPSQLiteModule = NativeModules.OPSQLite;\n\n if (OPSQLiteModule == null) {\n throw new Error('Base module not found. Maybe try rebuilding the app.');\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || OPSQLiteModule.install == null) {\n throw new Error(\n 'Failed to install op-sqlite: React Native is not running on-device. OPSQLite can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = OPSQLiteModule.install();\n if (result !== true) {\n throw new Error(\n `Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n }\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__OPSQLiteProxy == null) {\n throw new Error(\n 'Failed to install op-sqlite, the native initializer function does not exist. Are you trying to use OPSQLite from different JS Runtimes?'\n );\n }\n}\n\nconst proxy = global.__OPSQLiteProxy;\nexport const OPSQLite = proxy as ISQLite;\n\n/**\n * Object returned by SQL Query executions {\n * insertId: Represent the auto-generated row id if applicable\n * rowsAffected: Number of affected rows if result of a update query\n * message: if status === 1, here you will find error description\n * rows: if status is undefined or 0 this object will contain the query results\n * }\n *\n * @interface QueryResult\n */\nexport type QueryResult = {\n insertId?: number;\n rowsAffected: number;\n rows?: {\n /** Raw array with all dataset */\n _array: any[];\n /** The lengh of the dataset */\n length: number;\n /** A convenience function to acess the index based the row object\n * @param idx the row index\n * @returns the row structure identified by column names\n */\n item: (idx: number) => any;\n };\n /**\n * Query metadata, avaliable only for select query results\n */\n metadata?: ColumnMetadata[];\n};\n\n/**\n * Column metadata\n * Describes some information about columns fetched by the query\n */\nexport type ColumnMetadata = {\n /** The name used for this column for this resultset */\n name: string;\n /** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. \"UNKNOWN\" for dynamic values, like function returned ones. */\n type: string;\n /**\n * The index for this column for this resultset*/\n index: number;\n};\n\n/**\n * Allows the execution of bulk of sql commands\n * inside a transaction\n * If a single query must be executed many times with different arguments, its preferred\n * to declare it a single time, and use an array of array parameters.\n */\nexport type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];\n\nexport type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';\n\n/**\n * status: 0 or undefined for correct execution, 1 for error\n * message: if status === 1, here you will find error description\n * rowsAffected: Number of affected rows if status == 0\n */\nexport type BatchQueryResult = {\n rowsAffected?: number;\n};\n\n/**\n * Result of loading a file and executing every line as a SQL command\n * Similar to BatchQueryResult\n */\nexport interface FileLoadResult extends BatchQueryResult {\n commands?: number;\n}\n\nexport interface Transaction {\n commit: () => QueryResult;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ) => Promise<QueryResult>;\n rollback: () => QueryResult;\n}\n\nexport interface PendingTransaction {\n /*\n * The start function should not throw or return a promise because the\n * queue just calls it and does not monitor for failures or completions.\n *\n * It should catch any errors and call the resolve or reject of the wrapping\n * promise when complete.\n *\n * It should also automatically commit or rollback the transaction if needed\n */\n start: () => void;\n}\n\ninterface ISQLite {\n open: (dbName: string, location?: string, inMemory?: boolean) => void;\n close: (dbName: string) => void;\n delete: (dbName: string, location?: string) => void;\n attach: (\n mainDbName: string,\n dbNameToAttach: string,\n alias: string,\n location?: string\n ) => void;\n detach: (mainDbName: string, alias: string) => void;\n transaction: (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n ) => Promise<void>;\n execute: (dbName: string, query: string, params?: any[]) => QueryResult;\n executeAsync: (\n dbName: string,\n query: string,\n params?: any[]\n ) => Promise<QueryResult>;\n executeBatch: (dbName: string, commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (\n dbName: string,\n commands: SQLBatchTuple[]\n ) => Promise<BatchQueryResult>;\n loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;\n updateHook: (\n dbName: string,\n callback?: ((params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void) | null\n ) => void;\n commitHook: (dbName: string, callback?: (() => void) | null) => void;\n rollbackHook: (dbName: string, callback?: (() => void) | null) => void;\n}\n\nconst locks: Record<\n string,\n { queue: PendingTransaction[]; inProgress: boolean }\n> = {};\n\n// Enhance some host functions\n\n// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\nfunction enhanceQueryResult(result: QueryResult): void {\n // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\n if (result.rows == null) {\n result.rows = {\n _array: [],\n length: 0,\n item: (idx: number) => result.rows?._array[idx],\n };\n } else {\n result.rows.item = (idx: number) => result.rows?._array[idx];\n }\n}\n\nconst _open = OPSQLite.open;\nOPSQLite.open = (dbName: string, location?: string, inMemory?: boolean) => {\n _open(dbName, location, !!inMemory);\n\n locks[dbName] = {\n queue: [],\n inProgress: false,\n };\n};\n\nconst _close = OPSQLite.close;\nOPSQLite.close = (dbName: string) => {\n _close(dbName);\n delete locks[dbName];\n};\n\nconst _execute = OPSQLite.execute;\nOPSQLite.execute = (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): QueryResult => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const result = _execute(dbName, query, sanitizedParams);\n enhanceQueryResult(result);\n return result;\n};\n\nconst _executeAsync = OPSQLite.executeAsync;\nOPSQLite.executeAsync = async (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): Promise<QueryResult> => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const res = await _executeAsync(dbName, query, sanitizedParams);\n enhanceQueryResult(res);\n return res;\n};\n\nOPSQLite.transaction = async (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n): Promise<void> => {\n if (!locks[dbName]) {\n throw Error(`SQLite Error: No lock found on db: ${dbName}`);\n }\n\n let isFinalized = false;\n\n // Local transaction context object implementation\n const execute = (query: string, params?: any[]): QueryResult => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.execute(dbName, query, params);\n };\n\n const executeAsync = (query: string, params?: any[] | undefined) => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.executeAsync(dbName, query, params);\n };\n\n const commit = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'COMMIT');\n isFinalized = true;\n return result;\n };\n\n const rollback = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'ROLLBACK');\n isFinalized = true;\n return result;\n };\n\n async function run() {\n try {\n await OPSQLite.executeAsync(dbName, 'BEGIN TRANSACTION');\n\n await fn({\n commit,\n execute,\n executeAsync,\n rollback,\n });\n\n if (!isFinalized) {\n commit();\n }\n } catch (executionError) {\n if (!isFinalized) {\n try {\n rollback();\n } catch (rollbackError) {\n throw rollbackError;\n }\n }\n\n throw executionError;\n } finally {\n locks[dbName].inProgress = false;\n isFinalized = false;\n startNextTransaction(dbName);\n }\n }\n\n return await new Promise((resolve, reject) => {\n const tx: PendingTransaction = {\n start: () => {\n run().then(resolve).catch(reject);\n },\n };\n\n locks[dbName].queue.push(tx);\n startNextTransaction(dbName);\n });\n};\n\nconst startNextTransaction = (dbName: string) => {\n if (!locks[dbName]) {\n throw Error(`Lock not found for db: ${dbName}`);\n }\n\n if (locks[dbName].inProgress) {\n // Transaction is already in process bail out\n return;\n }\n\n if (locks[dbName].queue.length) {\n locks[dbName].inProgress = true;\n const tx = locks[dbName].queue.shift();\n\n if (!tx) {\n throw new Error('Could not get a operation on datebase');\n }\n\n setImmediate(() => {\n tx.start();\n });\n }\n};\n\nexport type OPSQLiteConnection = {\n close: () => void;\n delete: () => void;\n attach: (dbNameToAttach: string, alias: string, location?: string) => void;\n detach: (alias: string) => void;\n transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (query: string, params?: any[]) => Promise<QueryResult>;\n executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;\n loadFile: (location: string) => Promise<FileLoadResult>;\n updateHook: (\n callback: ((params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void) | null\n ) => void;\n commitHook: (callback: (() => void) | null) => void;\n rollbackHook: (callback: (() => void) | null) => void;\n};\n\nexport const open = (options: {\n name: string;\n location?: string;\n inMemory?: boolean;\n}): OPSQLiteConnection => {\n OPSQLite.open(options.name, options.location, options.inMemory);\n\n return {\n close: () => OPSQLite.close(options.name),\n delete: () => OPSQLite.delete(options.name, options.location),\n attach: (dbNameToAttach: string, alias: string, location?: string) =>\n OPSQLite.attach(options.name, dbNameToAttach, alias, location),\n detach: (alias: string) => OPSQLite.detach(options.name, alias),\n transaction: (fn: (tx: Transaction) => Promise<void>) =>\n OPSQLite.transaction(options.name, fn),\n execute: (query: string, params?: any[] | undefined): QueryResult =>\n OPSQLite.execute(options.name, query, params),\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ): Promise<QueryResult> =>\n OPSQLite.executeAsync(options.name, query, params),\n executeBatch: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatch(options.name, commands),\n executeBatchAsync: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatchAsync(options.name, commands),\n loadFile: (location: string) => OPSQLite.loadFile(options.name, location),\n updateHook: (callback) => OPSQLite.updateHook(options.name, callback),\n commitHook: (callback) => OPSQLite.commitHook(options.name, callback),\n rollbackHook: (callback) => OPSQLite.rollbackHook(options.name, callback),\n };\n};\n"],"mappings":";;;;;;AAAA;AAOA,IAAIA,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;EAClC,MAAMC,cAAc,GAAGC,0BAAa,CAACC,QAAQ;EAE7C,IAAIF,cAAc,IAAI,IAAI,EAAE;IAC1B,MAAM,IAAIG,KAAK,CAAC,sDAAsD,CAAC;EACzE;;EAEA;EACA,IAAIL,MAAM,CAACM,kBAAkB,IAAI,IAAI,IAAIJ,cAAc,CAACK,OAAO,IAAI,IAAI,EAAE;IACvE,MAAM,IAAIF,KAAK,CACb,iQAAiQ,CAClQ;EACH;;EAEA;EACA,MAAMG,MAAM,GAAGN,cAAc,CAACK,OAAO,EAAE;EACvC,IAAIC,MAAM,KAAK,IAAI,EAAE;IACnB,MAAM,IAAIH,KAAK,CACZ,iJAAgJG,MAAO,EAAC,CAC1J;EACH;;EAEA;EACA,IAAIR,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;IAClC,MAAM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,CAACV,OAAO,CAACC,IAAI,EAAEvC,QAAQ,CAAC;IACzEiD,UAAU,EAAGC,QAAQ,IAAKlE,QAAQ,CAACiE,UAAU,CAACX,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEC,UAAU,EAAGD,QAAQ,IAAKlE,QAAQ,CAACmE,UAAU,CAACb,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEE,YAAY,EAAGF,QAAQ,IAAKlE,QAAQ,CAACoE,YAAY,CAACd,OAAO,CAACC,IAAI,EAAEW,QAAQ;EAC1E,CAAC;AACH,CAAC;AAAC"}
@@ -1 +1 @@
1
- {"version":3,"names":["NativeModules","global","__OPSQLiteProxy","OPSQLiteModule","OPSQLite","Error","nativeCallSyncHook","install","result","proxy","locks","enhanceQueryResult","rows","_array","length","item","idx","_open","open","dbName","location","inMemory","queue","inProgress","_close","close","_execute","execute","query","params","sanitizedParams","map","p","ArrayBuffer","isView","buffer","_executeAsync","executeAsync","res","transaction","fn","isFinalized","commit","rollback","run","executionError","rollbackError","startNextTransaction","Promise","resolve","reject","tx","start","then","catch","push","shift","setImmediate","options","name","delete","attach","dbNameToAttach","alias","detach","executeBatch","commands","executeBatchAsync","loadFile","updateHook","callback","commitHook","rollbackHook"],"sources":["index.ts"],"sourcesContent":["import { NativeModules } from 'react-native';\n\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __OPSQLiteProxy: object | undefined;\n}\n\nif (global.__OPSQLiteProxy == null) {\n const OPSQLiteModule = NativeModules.OPSQLite;\n\n if (OPSQLiteModule == null) {\n throw new Error('Base module not found. Maybe try rebuilding the app.');\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || OPSQLiteModule.install == null) {\n throw new Error(\n 'Failed to install op-sqlite: React Native is not running on-device. OPSQLite can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = OPSQLiteModule.install();\n if (result !== true) {\n throw new Error(\n `Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n }\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__OPSQLiteProxy == null) {\n throw new Error(\n 'Failed to install op-sqlite, the native initializer function does not exist. Are you trying to use OPSQLite from different JS Runtimes?'\n );\n }\n}\n\nconst proxy = global.__OPSQLiteProxy;\nexport const OPSQLite = proxy as ISQLite;\n\n/**\n * Object returned by SQL Query executions {\n * insertId: Represent the auto-generated row id if applicable\n * rowsAffected: Number of affected rows if result of a update query\n * message: if status === 1, here you will find error description\n * rows: if status is undefined or 0 this object will contain the query results\n * }\n *\n * @interface QueryResult\n */\nexport type QueryResult = {\n insertId?: number;\n rowsAffected: number;\n rows?: {\n /** Raw array with all dataset */\n _array: any[];\n /** The lengh of the dataset */\n length: number;\n /** A convenience function to acess the index based the row object\n * @param idx the row index\n * @returns the row structure identified by column names\n */\n item: (idx: number) => any;\n };\n /**\n * Query metadata, avaliable only for select query results\n */\n metadata?: ColumnMetadata[];\n};\n\n/**\n * Column metadata\n * Describes some information about columns fetched by the query\n */\nexport type ColumnMetadata = {\n /** The name used for this column for this resultset */\n name: string;\n /** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. \"UNKNOWN\" for dynamic values, like function returned ones. */\n type: string;\n /**\n * The index for this column for this resultset*/\n index: number;\n};\n\n/**\n * Allows the execution of bulk of sql commands\n * inside a transaction\n * If a single query must be executed many times with different arguments, its preferred\n * to declare it a single time, and use an array of array parameters.\n */\nexport type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];\n\nexport type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';\n\n/**\n * status: 0 or undefined for correct execution, 1 for error\n * message: if status === 1, here you will find error description\n * rowsAffected: Number of affected rows if status == 0\n */\nexport type BatchQueryResult = {\n rowsAffected?: number;\n};\n\n/**\n * Result of loading a file and executing every line as a SQL command\n * Similar to BatchQueryResult\n */\nexport interface FileLoadResult extends BatchQueryResult {\n commands?: number;\n}\n\nexport interface Transaction {\n commit: () => QueryResult;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ) => Promise<QueryResult>;\n rollback: () => QueryResult;\n}\n\nexport interface PendingTransaction {\n /*\n * The start function should not throw or return a promise because the\n * queue just calls it and does not monitor for failures or completions.\n *\n * It should catch any errors and call the resolve or reject of the wrapping\n * promise when complete.\n *\n * It should also automatically commit or rollback the transaction if needed\n */\n start: () => void;\n}\n\ninterface ISQLite {\n open: (dbName: string, location?: string, inMemory?: boolean) => void;\n close: (dbName: string) => void;\n delete: (dbName: string, location?: string) => void;\n attach: (\n mainDbName: string,\n dbNameToAttach: string,\n alias: string,\n location?: string\n ) => void;\n detach: (mainDbName: string, alias: string) => void;\n transaction: (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n ) => Promise<void>;\n execute: (dbName: string, query: string, params?: any[]) => QueryResult;\n executeAsync: (\n dbName: string,\n query: string,\n params?: any[]\n ) => Promise<QueryResult>;\n executeBatch: (dbName: string, commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (\n dbName: string,\n commands: SQLBatchTuple[]\n ) => Promise<BatchQueryResult>;\n loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;\n updateHook: (\n dbName: string,\n callback: (params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void\n ) => void;\n commitHook: (dbName: string, callback: () => void) => void;\n rollbackHook: (dbName: string, callback: () => void) => void;\n}\n\nconst locks: Record<\n string,\n { queue: PendingTransaction[]; inProgress: boolean }\n> = {};\n\n// Enhance some host functions\n\n// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\nfunction enhanceQueryResult(result: QueryResult): void {\n // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\n if (result.rows == null) {\n result.rows = {\n _array: [],\n length: 0,\n item: (idx: number) => result.rows?._array[idx],\n };\n } else {\n result.rows.item = (idx: number) => result.rows?._array[idx];\n }\n}\n\nconst _open = OPSQLite.open;\nOPSQLite.open = (dbName: string, location?: string, inMemory?: boolean) => {\n _open(dbName, location, !!inMemory);\n\n locks[dbName] = {\n queue: [],\n inProgress: false,\n };\n};\n\nconst _close = OPSQLite.close;\nOPSQLite.close = (dbName: string) => {\n _close(dbName);\n delete locks[dbName];\n};\n\nconst _execute = OPSQLite.execute;\nOPSQLite.execute = (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): QueryResult => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const result = _execute(dbName, query, sanitizedParams);\n enhanceQueryResult(result);\n return result;\n};\n\nconst _executeAsync = OPSQLite.executeAsync;\nOPSQLite.executeAsync = async (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): Promise<QueryResult> => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const res = await _executeAsync(dbName, query, sanitizedParams);\n enhanceQueryResult(res);\n return res;\n};\n\nOPSQLite.transaction = async (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n): Promise<void> => {\n if (!locks[dbName]) {\n throw Error(`SQLite Error: No lock found on db: ${dbName}`);\n }\n\n let isFinalized = false;\n\n // Local transaction context object implementation\n const execute = (query: string, params?: any[]): QueryResult => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.execute(dbName, query, params);\n };\n\n const executeAsync = (query: string, params?: any[] | undefined) => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.executeAsync(dbName, query, params);\n };\n\n const commit = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'COMMIT');\n isFinalized = true;\n return result;\n };\n\n const rollback = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'ROLLBACK');\n isFinalized = true;\n return result;\n };\n\n async function run() {\n try {\n await OPSQLite.executeAsync(dbName, 'BEGIN TRANSACTION');\n\n await fn({\n commit,\n execute,\n executeAsync,\n rollback,\n });\n\n if (!isFinalized) {\n commit();\n }\n } catch (executionError) {\n if (!isFinalized) {\n try {\n rollback();\n } catch (rollbackError) {\n throw rollbackError;\n }\n }\n\n throw executionError;\n } finally {\n locks[dbName].inProgress = false;\n isFinalized = false;\n startNextTransaction(dbName);\n }\n }\n\n return await new Promise((resolve, reject) => {\n const tx: PendingTransaction = {\n start: () => {\n run().then(resolve).catch(reject);\n },\n };\n\n locks[dbName].queue.push(tx);\n startNextTransaction(dbName);\n });\n};\n\nconst startNextTransaction = (dbName: string) => {\n if (!locks[dbName]) {\n throw Error(`Lock not found for db: ${dbName}`);\n }\n\n if (locks[dbName].inProgress) {\n // Transaction is already in process bail out\n return;\n }\n\n if (locks[dbName].queue.length) {\n locks[dbName].inProgress = true;\n const tx = locks[dbName].queue.shift();\n\n if (!tx) {\n throw new Error('Could not get a operation on datebase');\n }\n\n setImmediate(() => {\n tx.start();\n });\n }\n};\n\nexport type OPSQLiteConnection = {\n close: () => void;\n delete: () => void;\n attach: (dbNameToAttach: string, alias: string, location?: string) => void;\n detach: (alias: string) => void;\n transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (query: string, params?: any[]) => Promise<QueryResult>;\n executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;\n loadFile: (location: string) => Promise<FileLoadResult>;\n updateHook: (\n callback: (params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void\n ) => void;\n commitHook: (callback: () => void) => void;\n rollbackHook: (callback: () => void) => void;\n};\n\nexport const open = (options: {\n name: string;\n location?: string;\n inMemory?: boolean;\n}): OPSQLiteConnection => {\n OPSQLite.open(options.name, options.location, options.inMemory);\n\n return {\n close: () => OPSQLite.close(options.name),\n delete: () => OPSQLite.delete(options.name, options.location),\n attach: (dbNameToAttach: string, alias: string, location?: string) =>\n OPSQLite.attach(options.name, dbNameToAttach, alias, location),\n detach: (alias: string) => OPSQLite.detach(options.name, alias),\n transaction: (fn: (tx: Transaction) => Promise<void>) =>\n OPSQLite.transaction(options.name, fn),\n execute: (query: string, params?: any[] | undefined): QueryResult =>\n OPSQLite.execute(options.name, query, params),\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ): Promise<QueryResult> =>\n OPSQLite.executeAsync(options.name, query, params),\n executeBatch: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatch(options.name, commands),\n executeBatchAsync: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatchAsync(options.name, commands),\n loadFile: (location: string) => OPSQLite.loadFile(options.name, location),\n updateHook: (callback) => OPSQLite.updateHook(options.name, callback),\n commitHook: (callback) => OPSQLite.commitHook(options.name, callback),\n rollbackHook: (callback) => OPSQLite.rollbackHook(options.name, callback),\n };\n};\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ,cAAc;AAO5C,IAAIC,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;EAClC,MAAMC,cAAc,GAAGH,aAAa,CAACI,QAAQ;EAE7C,IAAID,cAAc,IAAI,IAAI,EAAE;IAC1B,MAAM,IAAIE,KAAK,CAAC,sDAAsD,CAAC;EACzE;;EAEA;EACA,IAAIJ,MAAM,CAACK,kBAAkB,IAAI,IAAI,IAAIH,cAAc,CAACI,OAAO,IAAI,IAAI,EAAE;IACvE,MAAM,IAAIF,KAAK,CACb,iQAAiQ,CAClQ;EACH;;EAEA;EACA,MAAMG,MAAM,GAAGL,cAAc,CAACI,OAAO,EAA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Q,CAAC;IAC/CC,iBAAiB,EAAGD,QAAyB,IAC3C9D,QAAQ,CAAC+D,iBAAiB,CAACT,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IACpDE,QAAQ,EAAGhD,QAAgB,IAAKhB,QAAQ,CAACgE,QAAQ,CAACV,OAAO,CAACC,IAAI,EAAEvC,QAAQ,CAAC;IACzEiD,UAAU,EAAGC,QAAQ,IAAKlE,QAAQ,CAACiE,UAAU,CAACX,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEC,UAAU,EAAGD,QAAQ,IAAKlE,QAAQ,CAACmE,UAAU,CAACb,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEE,YAAY,EAAGF,QAAQ,IAAKlE,QAAQ,CAACoE,YAAY,CAACd,OAAO,CAACC,IAAI,EAAEW,QAAQ;EAC1E,CAAC;AACH,CAAC"}
1
+ {"version":3,"names":["NativeModules","global","__OPSQLiteProxy","OPSQLiteModule","OPSQLite","Error","nativeCallSyncHook","install","result","proxy","locks","enhanceQueryResult","rows","_array","length","item","idx","_open","open","dbName","location","inMemory","queue","inProgress","_close","close","_execute","execute","query","params","sanitizedParams","map","p","ArrayBuffer","isView","buffer","_executeAsync","executeAsync","res","transaction","fn","isFinalized","commit","rollback","run","executionError","rollbackError","startNextTransaction","Promise","resolve","reject","tx","start","then","catch","push","shift","setImmediate","options","name","delete","attach","dbNameToAttach","alias","detach","executeBatch","commands","executeBatchAsync","loadFile","updateHook","callback","commitHook","rollbackHook"],"sources":["index.ts"],"sourcesContent":["import { NativeModules } from 'react-native';\n\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __OPSQLiteProxy: object | undefined;\n}\n\nif (global.__OPSQLiteProxy == null) {\n const OPSQLiteModule = NativeModules.OPSQLite;\n\n if (OPSQLiteModule == null) {\n throw new Error('Base module not found. Maybe try rebuilding the app.');\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || OPSQLiteModule.install == null) {\n throw new Error(\n 'Failed to install op-sqlite: React Native is not running on-device. OPSQLite can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = OPSQLiteModule.install();\n if (result !== true) {\n throw new Error(\n `Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n }\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__OPSQLiteProxy == null) {\n throw new Error(\n 'Failed to install op-sqlite, the native initializer function does not exist. Are you trying to use OPSQLite from different JS Runtimes?'\n );\n }\n}\n\nconst proxy = global.__OPSQLiteProxy;\nexport const OPSQLite = proxy as ISQLite;\n\n/**\n * Object returned by SQL Query executions {\n * insertId: Represent the auto-generated row id if applicable\n * rowsAffected: Number of affected rows if result of a update query\n * message: if status === 1, here you will find error description\n * rows: if status is undefined or 0 this object will contain the query results\n * }\n *\n * @interface QueryResult\n */\nexport type QueryResult = {\n insertId?: number;\n rowsAffected: number;\n rows?: {\n /** Raw array with all dataset */\n _array: any[];\n /** The lengh of the dataset */\n length: number;\n /** A convenience function to acess the index based the row object\n * @param idx the row index\n * @returns the row structure identified by column names\n */\n item: (idx: number) => any;\n };\n /**\n * Query metadata, avaliable only for select query results\n */\n metadata?: ColumnMetadata[];\n};\n\n/**\n * Column metadata\n * Describes some information about columns fetched by the query\n */\nexport type ColumnMetadata = {\n /** The name used for this column for this resultset */\n name: string;\n /** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. \"UNKNOWN\" for dynamic values, like function returned ones. */\n type: string;\n /**\n * The index for this column for this resultset*/\n index: number;\n};\n\n/**\n * Allows the execution of bulk of sql commands\n * inside a transaction\n * If a single query must be executed many times with different arguments, its preferred\n * to declare it a single time, and use an array of array parameters.\n */\nexport type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];\n\nexport type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';\n\n/**\n * status: 0 or undefined for correct execution, 1 for error\n * message: if status === 1, here you will find error description\n * rowsAffected: Number of affected rows if status == 0\n */\nexport type BatchQueryResult = {\n rowsAffected?: number;\n};\n\n/**\n * Result of loading a file and executing every line as a SQL command\n * Similar to BatchQueryResult\n */\nexport interface FileLoadResult extends BatchQueryResult {\n commands?: number;\n}\n\nexport interface Transaction {\n commit: () => QueryResult;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ) => Promise<QueryResult>;\n rollback: () => QueryResult;\n}\n\nexport interface PendingTransaction {\n /*\n * The start function should not throw or return a promise because the\n * queue just calls it and does not monitor for failures or completions.\n *\n * It should catch any errors and call the resolve or reject of the wrapping\n * promise when complete.\n *\n * It should also automatically commit or rollback the transaction if needed\n */\n start: () => void;\n}\n\ninterface ISQLite {\n open: (dbName: string, location?: string, inMemory?: boolean) => void;\n close: (dbName: string) => void;\n delete: (dbName: string, location?: string) => void;\n attach: (\n mainDbName: string,\n dbNameToAttach: string,\n alias: string,\n location?: string\n ) => void;\n detach: (mainDbName: string, alias: string) => void;\n transaction: (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n ) => Promise<void>;\n execute: (dbName: string, query: string, params?: any[]) => QueryResult;\n executeAsync: (\n dbName: string,\n query: string,\n params?: any[]\n ) => Promise<QueryResult>;\n executeBatch: (dbName: string, commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (\n dbName: string,\n commands: SQLBatchTuple[]\n ) => Promise<BatchQueryResult>;\n loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;\n updateHook: (\n dbName: string,\n callback?: ((params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void) | null\n ) => void;\n commitHook: (dbName: string, callback?: (() => void) | null) => void;\n rollbackHook: (dbName: string, callback?: (() => void) | null) => void;\n}\n\nconst locks: Record<\n string,\n { queue: PendingTransaction[]; inProgress: boolean }\n> = {};\n\n// Enhance some host functions\n\n// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\nfunction enhanceQueryResult(result: QueryResult): void {\n // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work\n if (result.rows == null) {\n result.rows = {\n _array: [],\n length: 0,\n item: (idx: number) => result.rows?._array[idx],\n };\n } else {\n result.rows.item = (idx: number) => result.rows?._array[idx];\n }\n}\n\nconst _open = OPSQLite.open;\nOPSQLite.open = (dbName: string, location?: string, inMemory?: boolean) => {\n _open(dbName, location, !!inMemory);\n\n locks[dbName] = {\n queue: [],\n inProgress: false,\n };\n};\n\nconst _close = OPSQLite.close;\nOPSQLite.close = (dbName: string) => {\n _close(dbName);\n delete locks[dbName];\n};\n\nconst _execute = OPSQLite.execute;\nOPSQLite.execute = (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): QueryResult => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const result = _execute(dbName, query, sanitizedParams);\n enhanceQueryResult(result);\n return result;\n};\n\nconst _executeAsync = OPSQLite.executeAsync;\nOPSQLite.executeAsync = async (\n dbName: string,\n query: string,\n params?: any[] | undefined\n): Promise<QueryResult> => {\n const sanitizedParams = params?.map((p) => {\n if (ArrayBuffer.isView(p)) {\n return p.buffer;\n }\n\n return p;\n });\n\n const res = await _executeAsync(dbName, query, sanitizedParams);\n enhanceQueryResult(res);\n return res;\n};\n\nOPSQLite.transaction = async (\n dbName: string,\n fn: (tx: Transaction) => Promise<void>\n): Promise<void> => {\n if (!locks[dbName]) {\n throw Error(`SQLite Error: No lock found on db: ${dbName}`);\n }\n\n let isFinalized = false;\n\n // Local transaction context object implementation\n const execute = (query: string, params?: any[]): QueryResult => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.execute(dbName, query, params);\n };\n\n const executeAsync = (query: string, params?: any[] | undefined) => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute query on finalized transaction: ${dbName}`\n );\n }\n return OPSQLite.executeAsync(dbName, query, params);\n };\n\n const commit = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'COMMIT');\n isFinalized = true;\n return result;\n };\n\n const rollback = () => {\n if (isFinalized) {\n throw Error(\n `SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`\n );\n }\n const result = OPSQLite.execute(dbName, 'ROLLBACK');\n isFinalized = true;\n return result;\n };\n\n async function run() {\n try {\n await OPSQLite.executeAsync(dbName, 'BEGIN TRANSACTION');\n\n await fn({\n commit,\n execute,\n executeAsync,\n rollback,\n });\n\n if (!isFinalized) {\n commit();\n }\n } catch (executionError) {\n if (!isFinalized) {\n try {\n rollback();\n } catch (rollbackError) {\n throw rollbackError;\n }\n }\n\n throw executionError;\n } finally {\n locks[dbName].inProgress = false;\n isFinalized = false;\n startNextTransaction(dbName);\n }\n }\n\n return await new Promise((resolve, reject) => {\n const tx: PendingTransaction = {\n start: () => {\n run().then(resolve).catch(reject);\n },\n };\n\n locks[dbName].queue.push(tx);\n startNextTransaction(dbName);\n });\n};\n\nconst startNextTransaction = (dbName: string) => {\n if (!locks[dbName]) {\n throw Error(`Lock not found for db: ${dbName}`);\n }\n\n if (locks[dbName].inProgress) {\n // Transaction is already in process bail out\n return;\n }\n\n if (locks[dbName].queue.length) {\n locks[dbName].inProgress = true;\n const tx = locks[dbName].queue.shift();\n\n if (!tx) {\n throw new Error('Could not get a operation on datebase');\n }\n\n setImmediate(() => {\n tx.start();\n });\n }\n};\n\nexport type OPSQLiteConnection = {\n close: () => void;\n delete: () => void;\n attach: (dbNameToAttach: string, alias: string, location?: string) => void;\n detach: (alias: string) => void;\n transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;\n execute: (query: string, params?: any[]) => QueryResult;\n executeAsync: (query: string, params?: any[]) => Promise<QueryResult>;\n executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult;\n executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;\n loadFile: (location: string) => Promise<FileLoadResult>;\n updateHook: (\n callback: ((params: {\n table: string;\n operation: UpdateHookOperation;\n row?: any;\n rowId: number;\n }) => void) | null\n ) => void;\n commitHook: (callback: (() => void) | null) => void;\n rollbackHook: (callback: (() => void) | null) => void;\n};\n\nexport const open = (options: {\n name: string;\n location?: string;\n inMemory?: boolean;\n}): OPSQLiteConnection => {\n OPSQLite.open(options.name, options.location, options.inMemory);\n\n return {\n close: () => OPSQLite.close(options.name),\n delete: () => OPSQLite.delete(options.name, options.location),\n attach: (dbNameToAttach: string, alias: string, location?: string) =>\n OPSQLite.attach(options.name, dbNameToAttach, alias, location),\n detach: (alias: string) => OPSQLite.detach(options.name, alias),\n transaction: (fn: (tx: Transaction) => Promise<void>) =>\n OPSQLite.transaction(options.name, fn),\n execute: (query: string, params?: any[] | undefined): QueryResult =>\n OPSQLite.execute(options.name, query, params),\n executeAsync: (\n query: string,\n params?: any[] | undefined\n ): Promise<QueryResult> =>\n OPSQLite.executeAsync(options.name, query, params),\n executeBatch: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatch(options.name, commands),\n executeBatchAsync: (commands: SQLBatchTuple[]) =>\n OPSQLite.executeBatchAsync(options.name, commands),\n loadFile: (location: string) => OPSQLite.loadFile(options.name, location),\n updateHook: (callback) => OPSQLite.updateHook(options.name, callback),\n commitHook: (callback) => OPSQLite.commitHook(options.name, callback),\n rollbackHook: (callback) => OPSQLite.rollbackHook(options.name, callback),\n };\n};\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ,cAAc;AAO5C,IAAIC,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;EAClC,MAAMC,cAAc,GAAGH,aAAa,CAACI,QAAQ;EAE7C,IAAID,cAAc,IAAI,IAAI,EAAE;IAC1B,MAAM,IAAIE,KAAK,CAAC,sDAAsD,CAAC;EACzE;;EAEA;EACA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,MAA0B,KAE1BzB,QAAQ,CAACiC,YAAY,CAACqB,OAAO,CAACC,IAAI,EAAE/B,KAAK,EAAEC,MAAM,CAAC;IACpDoC,YAAY,EAAGC,QAAyB,IACtC9D,QAAQ,CAAC6D,YAAY,CAACP,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IAC/CC,iBAAiB,EAAGD,QAAyB,IAC3C9D,QAAQ,CAAC+D,iBAAiB,CAACT,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IACpDE,QAAQ,EAAGhD,QAAgB,IAAKhB,QAAQ,CAACgE,QAAQ,CAACV,OAAO,CAACC,IAAI,EAAEvC,QAAQ,CAAC;IACzEiD,UAAU,EAAGC,QAAQ,IAAKlE,QAAQ,CAACiE,UAAU,CAACX,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEC,UAAU,EAAGD,QAAQ,IAAKlE,QAAQ,CAACmE,UAAU,CAACb,OAAO,CAACC,IAAI,EAAEW,QAAQ,CAAC;IACrEE,YAAY,EAAGF,QAAQ,IAAKlE,QAAQ,CAACoE,YAAY,CAACd,OAAO,CAACC,IAAI,EAAEW,QAAQ;EAC1E,CAAC;AACH,CAAC"}
@@ -89,14 +89,14 @@ interface ISQLite {
89
89
  executeBatch: (dbName: string, commands: SQLBatchTuple[]) => BatchQueryResult;
90
90
  executeBatchAsync: (dbName: string, commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
91
91
  loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;
92
- updateHook: (dbName: string, callback: (params: {
92
+ updateHook: (dbName: string, callback?: ((params: {
93
93
  table: string;
94
94
  operation: UpdateHookOperation;
95
95
  row?: any;
96
96
  rowId: number;
97
- }) => void) => void;
98
- commitHook: (dbName: string, callback: () => void) => void;
99
- rollbackHook: (dbName: string, callback: () => void) => void;
97
+ }) => void) | null) => void;
98
+ commitHook: (dbName: string, callback?: (() => void) | null) => void;
99
+ rollbackHook: (dbName: string, callback?: (() => void) | null) => void;
100
100
  }
101
101
  export type OPSQLiteConnection = {
102
102
  close: () => void;
@@ -109,14 +109,14 @@ export type OPSQLiteConnection = {
109
109
  executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult;
110
110
  executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
111
111
  loadFile: (location: string) => Promise<FileLoadResult>;
112
- updateHook: (callback: (params: {
112
+ updateHook: (callback: ((params: {
113
113
  table: string;
114
114
  operation: UpdateHookOperation;
115
115
  row?: any;
116
116
  rowId: number;
117
- }) => void) => void;
118
- commitHook: (callback: () => void) => void;
119
- rollbackHook: (callback: () => void) => void;
117
+ }) => void) | null) => void;
118
+ commitHook: (callback: (() => void) | null) => void;
119
+ rollbackHook: (callback: (() => void) | null) => void;
120
120
  };
121
121
  export declare const open: (options: {
122
122
  name: string;
package/op-sqlite.podspec CHANGED
@@ -1,6 +1,7 @@
1
1
  require "json"
2
2
 
3
3
  package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+ folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
4
5
 
5
6
  Pod::Spec.new do |s|
6
7
  s.name = "op-sqlite"
@@ -30,7 +31,6 @@ Pod::Spec.new do |s|
30
31
  'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
31
32
  }
32
33
 
33
-
34
34
  if ENV['OP_SQLITE_USE_PHONE_VERSION'] == '1' then
35
35
  s.exclude_files = "cpp/sqlite3.c", "cpp/sqlite3.h"
36
36
  s.library = "sqlite3"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@op-engineering/op-sqlite",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "Next generation SQLite for React Native",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
package/src/index.ts CHANGED
@@ -161,15 +161,15 @@ interface ISQLite {
161
161
  loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;
162
162
  updateHook: (
163
163
  dbName: string,
164
- callback: (params: {
164
+ callback?: ((params: {
165
165
  table: string;
166
166
  operation: UpdateHookOperation;
167
167
  row?: any;
168
168
  rowId: number;
169
- }) => void
169
+ }) => void) | null
170
170
  ) => void;
171
- commitHook: (dbName: string, callback: () => void) => void;
172
- rollbackHook: (dbName: string, callback: () => void) => void;
171
+ commitHook: (dbName: string, callback?: (() => void) | null) => void;
172
+ rollbackHook: (dbName: string, callback?: (() => void) | null) => void;
173
173
  }
174
174
 
175
175
  const locks: Record<
@@ -377,15 +377,15 @@ export type OPSQLiteConnection = {
377
377
  executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
378
378
  loadFile: (location: string) => Promise<FileLoadResult>;
379
379
  updateHook: (
380
- callback: (params: {
380
+ callback: ((params: {
381
381
  table: string;
382
382
  operation: UpdateHookOperation;
383
383
  row?: any;
384
384
  rowId: number;
385
- }) => void
385
+ }) => void) | null
386
386
  ) => void;
387
- commitHook: (callback: () => void) => void;
388
- rollbackHook: (callback: () => void) => void;
387
+ commitHook: (callback: (() => void) | null) => void;
388
+ rollbackHook: (callback: (() => void) | null) => void;
389
389
  };
390
390
 
391
391
  export const open = (options: {