@op-engineering/op-sqlite 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +274 -0
  3. package/android/.project +17 -0
  4. package/android/.settings/org.eclipse.buildship.core.prefs +13 -0
  5. package/android/CMakeLists.txt +57 -0
  6. package/android/build.gradle +127 -0
  7. package/android/cpp-adapter.cpp +42 -0
  8. package/android/gradle.properties +4 -0
  9. package/android/src/main/AndroidManifest.xml +4 -0
  10. package/android/src/main/java/com/op/sqlite/OPSQLiteBridge.java +29 -0
  11. package/android/src/main/java/com/op/sqlite/OPSQLiteModule.java +46 -0
  12. package/android/src/main/java/com/op/sqlite/OPSQLitePackage.java +26 -0
  13. package/cpp/DynamicHostObject.cpp +30 -0
  14. package/cpp/DynamicHostObject.h +30 -0
  15. package/cpp/ThreadPool.cpp +95 -0
  16. package/cpp/ThreadPool.h +46 -0
  17. package/cpp/bindings.cpp +430 -0
  18. package/cpp/bindings.h +13 -0
  19. package/cpp/bridge.cpp +502 -0
  20. package/cpp/bridge.h +34 -0
  21. package/cpp/logs.h +38 -0
  22. package/cpp/macros.h +16 -0
  23. package/cpp/sqlbatchexecutor.cpp +94 -0
  24. package/cpp/sqlbatchexecutor.h +28 -0
  25. package/cpp/sqlite3.c +252611 -0
  26. package/cpp/sqlite3.h +13257 -0
  27. package/cpp/utils.cpp +218 -0
  28. package/cpp/utils.h +55 -0
  29. package/ios/OPSQLite.h +8 -0
  30. package/ios/OPSQLite.mm +63 -0
  31. package/ios/OPSQLite.xcodeproj/project.pbxproj +275 -0
  32. package/lib/commonjs/index.js +190 -0
  33. package/lib/commonjs/index.js.map +1 -0
  34. package/lib/module/index.js +183 -0
  35. package/lib/module/index.js.map +1 -0
  36. package/lib/typescript/index.d.ts +108 -0
  37. package/op-sqlite.podspec +39 -0
  38. package/package.json +79 -0
  39. package/src/index.ts +374 -0
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.open = exports.OPSQLite = void 0;
7
+ var _reactNative = require("react-native");
8
+ if (global.__OPSQLiteProxy == null) {
9
+ const OPSQLiteModule = _reactNative.NativeModules.OPSQLite;
10
+ if (OPSQLiteModule == null) {
11
+ throw new Error('Base module not found. Maybe try rebuilding the app.');
12
+ }
13
+
14
+ // Check if we are running on-device (JSI)
15
+ if (global.nativeCallSyncHook == null || OPSQLiteModule.install == null) {
16
+ throw new Error('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.');
17
+ }
18
+
19
+ // Call the synchronous blocking install() function
20
+ const result = OPSQLiteModule.install();
21
+ if (result !== true) {
22
+ throw new Error(`Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`);
23
+ }
24
+
25
+ // Check again if the constructor now exists. If not, throw an error.
26
+ if (global.__OPSQLiteProxy == null) {
27
+ throw new Error('Failed to install op-sqlite, the native initializer function does not exist. Are you trying to use OPSQLite from different JS Runtimes?');
28
+ }
29
+ }
30
+ const proxy = global.__OPSQLiteProxy;
31
+ const OPSQLite = proxy;
32
+
33
+ /**
34
+ * Object returned by SQL Query executions {
35
+ * insertId: Represent the auto-generated row id if applicable
36
+ * rowsAffected: Number of affected rows if result of a update query
37
+ * message: if status === 1, here you will find error description
38
+ * rows: if status is undefined or 0 this object will contain the query results
39
+ * }
40
+ *
41
+ * @interface QueryResult
42
+ */
43
+ exports.OPSQLite = OPSQLite;
44
+ const locks = {};
45
+
46
+ // Enhance some host functions
47
+
48
+ // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
49
+ const enhanceQueryResult = result => {
50
+ // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
51
+ if (result.rows == null) {
52
+ result.rows = {
53
+ _array: [],
54
+ length: 0,
55
+ item: idx => result.rows._array[idx]
56
+ };
57
+ } else {
58
+ result.rows.item = idx => result.rows._array[idx];
59
+ }
60
+ };
61
+ const _open = OPSQLite.open;
62
+ OPSQLite.open = (dbName, location) => {
63
+ _open(dbName, location);
64
+ locks[dbName] = {
65
+ queue: [],
66
+ inProgress: false
67
+ };
68
+ };
69
+ const _close = OPSQLite.close;
70
+ OPSQLite.close = dbName => {
71
+ _close(dbName);
72
+ delete locks[dbName];
73
+ };
74
+ const _execute = OPSQLite.execute;
75
+ OPSQLite.execute = (dbName, query, params) => {
76
+ const result = _execute(dbName, query, params);
77
+ enhanceQueryResult(result);
78
+ return result;
79
+ };
80
+ const _executeAsync = OPSQLite.executeAsync;
81
+ OPSQLite.executeAsync = async (dbName, query, params) => {
82
+ const res = await _executeAsync(dbName, query, params);
83
+ enhanceQueryResult(res);
84
+ return res;
85
+ };
86
+ OPSQLite.transaction = async (dbName, fn) => {
87
+ if (!locks[dbName]) {
88
+ throw Error(`SQLite Error: No lock found on db: ${dbName}`);
89
+ }
90
+ let isFinalized = false;
91
+
92
+ // Local transaction context object implementation
93
+ const execute = (query, params) => {
94
+ if (isFinalized) {
95
+ throw Error(`SQLite Error: Cannot execute query on finalized transaction: ${dbName}`);
96
+ }
97
+ return OPSQLite.execute(dbName, query, params);
98
+ };
99
+ const executeAsync = (query, params) => {
100
+ if (isFinalized) {
101
+ throw Error(`SQLite Error: Cannot execute query on finalized transaction: ${dbName}`);
102
+ }
103
+ return OPSQLite.executeAsync(dbName, query, params);
104
+ };
105
+ const commit = () => {
106
+ if (isFinalized) {
107
+ throw Error(`SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`);
108
+ }
109
+ const result = OPSQLite.execute(dbName, 'COMMIT');
110
+ isFinalized = true;
111
+ return result;
112
+ };
113
+ const rollback = () => {
114
+ if (isFinalized) {
115
+ throw Error(`SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`);
116
+ }
117
+ const result = OPSQLite.execute(dbName, 'ROLLBACK');
118
+ isFinalized = true;
119
+ return result;
120
+ };
121
+ async function run() {
122
+ try {
123
+ await OPSQLite.executeAsync(dbName, 'BEGIN TRANSACTION');
124
+ await fn({
125
+ commit,
126
+ execute,
127
+ executeAsync,
128
+ rollback
129
+ });
130
+ if (!isFinalized) {
131
+ commit();
132
+ }
133
+ } catch (executionError) {
134
+ if (!isFinalized) {
135
+ try {
136
+ rollback();
137
+ } catch (rollbackError) {
138
+ throw rollbackError;
139
+ }
140
+ }
141
+ throw executionError;
142
+ } finally {
143
+ locks[dbName].inProgress = false;
144
+ isFinalized = false;
145
+ startNextTransaction(dbName);
146
+ }
147
+ }
148
+ return await new Promise((resolve, reject) => {
149
+ const tx = {
150
+ start: () => {
151
+ run().then(resolve).catch(reject);
152
+ }
153
+ };
154
+ locks[dbName].queue.push(tx);
155
+ startNextTransaction(dbName);
156
+ });
157
+ };
158
+ const startNextTransaction = dbName => {
159
+ if (!locks[dbName]) {
160
+ throw Error(`Lock not found for db: ${dbName}`);
161
+ }
162
+ if (locks[dbName].inProgress) {
163
+ // Transaction is already in process bail out
164
+ return;
165
+ }
166
+ if (locks[dbName].queue.length) {
167
+ locks[dbName].inProgress = true;
168
+ const tx = locks[dbName].queue.shift();
169
+ setImmediate(() => {
170
+ tx.start();
171
+ });
172
+ }
173
+ };
174
+ const open = options => {
175
+ OPSQLite.open(options.name, options.location);
176
+ return {
177
+ close: () => OPSQLite.close(options.name),
178
+ delete: () => OPSQLite.delete(options.name, options.location),
179
+ attach: (dbNameToAttach, alias, location) => OPSQLite.attach(options.name, dbNameToAttach, alias, location),
180
+ detach: alias => OPSQLite.detach(options.name, alias),
181
+ transaction: fn => OPSQLite.transaction(options.name, fn),
182
+ execute: (query, params) => OPSQLite.execute(options.name, query, params),
183
+ executeAsync: (query, params) => OPSQLite.executeAsync(options.name, query, params),
184
+ executeBatch: commands => OPSQLite.executeBatch(options.name, commands),
185
+ executeBatchAsync: commands => OPSQLite.executeBatchAsync(options.name, commands),
186
+ loadFile: location => OPSQLite.loadFile(options.name, location)
187
+ };
188
+ };
189
+ exports.open = open;
190
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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","queue","inProgress","_close","close","_execute","execute","query","params","_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"],"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\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) => 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> | 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}\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\nconst 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) => {\n _open(dbName, location);\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 result = _execute(dbName, query, params);\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 res = await _executeAsync(dbName, query, params);\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 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> | 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};\n\nexport const open = (options: {\n name: string;\n location?: string;\n}): OPSQLiteConnection => {\n OPSQLite.open(options.name, options.location);\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> | 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 };\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,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,CAAC;IAC7DkC,MAAM,EAAE,CAACC,cAAsB,EAAEC,KAAa,EAAEpC,QAAiB,KAC/DhB,QAAQ,CAACkD,MAAM,CAACH,OAAO,CAACC,IAAI,EAAEG,cAAc,EAAEC,KAAK,EAAEpC,QAAQ,CAAC;IAChEqC,MAAM,EAAGD,KAAa,IAAKpD,QAAQ,CAACqD,MAAM,CAACN,OAAO,CAACC,IAAI,EAAEI,KAAK,CAAC;IAC/DxB,WAAW,EAAGC,EAA6C,IACzD7B,QAAQ,CAAC4B,WAAW,CAACmB,OAAO,CAACC,IAAI,EAAEnB,EAAE,CAAC;IACxCP,OAAO,EAAE,CAACC,KAAa,EAAEC,MAA0B,KACjDxB,QAAQ,CAACsB,OAAO,CAACyB,OAAO,CAACC,IAAI,EAAEzB,KAAK,EAAEC,MAAM,CAAC;IAC/CE,YAAY,EAAE,CACZH,KAAa,EACbC,MAA0B,KAE1BxB,QAAQ,CAAC0B,YAAY,CAACqB,OAAO,CAACC,IAAI,EAAEzB,KAAK,EAAEC,MAAM,CAAC;IACpD8B,YAAY,EAAGC,QAAyB,IACtCvD,QAAQ,CAACsD,YAAY,CAACP,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IAC/CC,iBAAiB,EAAGD,QAAyB,IAC3CvD,QAAQ,CAACwD,iBAAiB,CAACT,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IACpDE,QAAQ,EAAGzC,QAAgB,IAAKhB,QAAQ,CAACyD,QAAQ,CAACV,OAAO,CAACC,IAAI,EAAEhC,QAAQ;EAC1E,CAAC;AACH,CAAC;AAAC"}
@@ -0,0 +1,183 @@
1
+ import { NativeModules } from 'react-native';
2
+ if (global.__OPSQLiteProxy == null) {
3
+ const OPSQLiteModule = NativeModules.OPSQLite;
4
+ if (OPSQLiteModule == null) {
5
+ throw new Error('Base module not found. Maybe try rebuilding the app.');
6
+ }
7
+
8
+ // Check if we are running on-device (JSI)
9
+ if (global.nativeCallSyncHook == null || OPSQLiteModule.install == null) {
10
+ throw new Error('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.');
11
+ }
12
+
13
+ // Call the synchronous blocking install() function
14
+ const result = OPSQLiteModule.install();
15
+ if (result !== true) {
16
+ throw new Error(`Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`);
17
+ }
18
+
19
+ // Check again if the constructor now exists. If not, throw an error.
20
+ if (global.__OPSQLiteProxy == null) {
21
+ throw new Error('Failed to install op-sqlite, the native initializer function does not exist. Are you trying to use OPSQLite from different JS Runtimes?');
22
+ }
23
+ }
24
+ const proxy = global.__OPSQLiteProxy;
25
+ export const OPSQLite = proxy;
26
+
27
+ /**
28
+ * Object returned by SQL Query executions {
29
+ * insertId: Represent the auto-generated row id if applicable
30
+ * rowsAffected: Number of affected rows if result of a update query
31
+ * message: if status === 1, here you will find error description
32
+ * rows: if status is undefined or 0 this object will contain the query results
33
+ * }
34
+ *
35
+ * @interface QueryResult
36
+ */
37
+
38
+ const locks = {};
39
+
40
+ // Enhance some host functions
41
+
42
+ // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
43
+ const enhanceQueryResult = result => {
44
+ // Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
45
+ if (result.rows == null) {
46
+ result.rows = {
47
+ _array: [],
48
+ length: 0,
49
+ item: idx => result.rows._array[idx]
50
+ };
51
+ } else {
52
+ result.rows.item = idx => result.rows._array[idx];
53
+ }
54
+ };
55
+ const _open = OPSQLite.open;
56
+ OPSQLite.open = (dbName, location) => {
57
+ _open(dbName, location);
58
+ locks[dbName] = {
59
+ queue: [],
60
+ inProgress: false
61
+ };
62
+ };
63
+ const _close = OPSQLite.close;
64
+ OPSQLite.close = dbName => {
65
+ _close(dbName);
66
+ delete locks[dbName];
67
+ };
68
+ const _execute = OPSQLite.execute;
69
+ OPSQLite.execute = (dbName, query, params) => {
70
+ const result = _execute(dbName, query, params);
71
+ enhanceQueryResult(result);
72
+ return result;
73
+ };
74
+ const _executeAsync = OPSQLite.executeAsync;
75
+ OPSQLite.executeAsync = async (dbName, query, params) => {
76
+ const res = await _executeAsync(dbName, query, params);
77
+ enhanceQueryResult(res);
78
+ return res;
79
+ };
80
+ OPSQLite.transaction = async (dbName, fn) => {
81
+ if (!locks[dbName]) {
82
+ throw Error(`SQLite Error: No lock found on db: ${dbName}`);
83
+ }
84
+ let isFinalized = false;
85
+
86
+ // Local transaction context object implementation
87
+ const execute = (query, params) => {
88
+ if (isFinalized) {
89
+ throw Error(`SQLite Error: Cannot execute query on finalized transaction: ${dbName}`);
90
+ }
91
+ return OPSQLite.execute(dbName, query, params);
92
+ };
93
+ const executeAsync = (query, params) => {
94
+ if (isFinalized) {
95
+ throw Error(`SQLite Error: Cannot execute query on finalized transaction: ${dbName}`);
96
+ }
97
+ return OPSQLite.executeAsync(dbName, query, params);
98
+ };
99
+ const commit = () => {
100
+ if (isFinalized) {
101
+ throw Error(`SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`);
102
+ }
103
+ const result = OPSQLite.execute(dbName, 'COMMIT');
104
+ isFinalized = true;
105
+ return result;
106
+ };
107
+ const rollback = () => {
108
+ if (isFinalized) {
109
+ throw Error(`SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`);
110
+ }
111
+ const result = OPSQLite.execute(dbName, 'ROLLBACK');
112
+ isFinalized = true;
113
+ return result;
114
+ };
115
+ async function run() {
116
+ try {
117
+ await OPSQLite.executeAsync(dbName, 'BEGIN TRANSACTION');
118
+ await fn({
119
+ commit,
120
+ execute,
121
+ executeAsync,
122
+ rollback
123
+ });
124
+ if (!isFinalized) {
125
+ commit();
126
+ }
127
+ } catch (executionError) {
128
+ if (!isFinalized) {
129
+ try {
130
+ rollback();
131
+ } catch (rollbackError) {
132
+ throw rollbackError;
133
+ }
134
+ }
135
+ throw executionError;
136
+ } finally {
137
+ locks[dbName].inProgress = false;
138
+ isFinalized = false;
139
+ startNextTransaction(dbName);
140
+ }
141
+ }
142
+ return await new Promise((resolve, reject) => {
143
+ const tx = {
144
+ start: () => {
145
+ run().then(resolve).catch(reject);
146
+ }
147
+ };
148
+ locks[dbName].queue.push(tx);
149
+ startNextTransaction(dbName);
150
+ });
151
+ };
152
+ const startNextTransaction = dbName => {
153
+ if (!locks[dbName]) {
154
+ throw Error(`Lock not found for db: ${dbName}`);
155
+ }
156
+ if (locks[dbName].inProgress) {
157
+ // Transaction is already in process bail out
158
+ return;
159
+ }
160
+ if (locks[dbName].queue.length) {
161
+ locks[dbName].inProgress = true;
162
+ const tx = locks[dbName].queue.shift();
163
+ setImmediate(() => {
164
+ tx.start();
165
+ });
166
+ }
167
+ };
168
+ export const open = options => {
169
+ OPSQLite.open(options.name, options.location);
170
+ return {
171
+ close: () => OPSQLite.close(options.name),
172
+ delete: () => OPSQLite.delete(options.name, options.location),
173
+ attach: (dbNameToAttach, alias, location) => OPSQLite.attach(options.name, dbNameToAttach, alias, location),
174
+ detach: alias => OPSQLite.detach(options.name, alias),
175
+ transaction: fn => OPSQLite.transaction(options.name, fn),
176
+ execute: (query, params) => OPSQLite.execute(options.name, query, params),
177
+ executeAsync: (query, params) => OPSQLite.executeAsync(options.name, query, params),
178
+ executeBatch: commands => OPSQLite.executeBatch(options.name, commands),
179
+ executeBatchAsync: commands => OPSQLite.executeBatchAsync(options.name, commands),
180
+ loadFile: location => OPSQLite.loadFile(options.name, location)
181
+ };
182
+ };
183
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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","queue","inProgress","_close","close","_execute","execute","query","params","_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"],"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\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) => 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> | 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}\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\nconst 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) => {\n _open(dbName, location);\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 result = _execute(dbName, query, params);\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 res = await _executeAsync(dbName, query, params);\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 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> | 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};\n\nexport const open = (options: {\n name: string;\n location?: string;\n}): OPSQLiteConnection => {\n OPSQLite.open(options.name, options.location);\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> | 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 };\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,EAAE;EACvC,IAAIC,MAAM,KAAK,IAAI,EAAE;IACnB,MAAM,IAAIH,KAAK,CACZ,iJAAgJG,MAAO,EAAC,CAC1J;EACH;;EAEA;EACA,IAAIP,MAAM,CAACC,eAAe,IAAI,IAAI,EAAE;IAClC,MAAM,IAA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W,CAACmB,OAAO,CAACC,IAAI,EAAEnB,EAAE,CAAC;IACxCP,OAAO,EAAE,CAACC,KAAa,EAAEC,MAA0B,KACjDxB,QAAQ,CAACsB,OAAO,CAACyB,OAAO,CAACC,IAAI,EAAEzB,KAAK,EAAEC,MAAM,CAAC;IAC/CE,YAAY,EAAE,CACZH,KAAa,EACbC,MAA0B,KAE1BxB,QAAQ,CAAC0B,YAAY,CAACqB,OAAO,CAACC,IAAI,EAAEzB,KAAK,EAAEC,MAAM,CAAC;IACpD8B,YAAY,EAAGC,QAAyB,IACtCvD,QAAQ,CAACsD,YAAY,CAACP,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IAC/CC,iBAAiB,EAAGD,QAAyB,IAC3CvD,QAAQ,CAACwD,iBAAiB,CAACT,OAAO,CAACC,IAAI,EAAEO,QAAQ,CAAC;IACpDE,QAAQ,EAAGzC,QAAgB,IAAKhB,QAAQ,CAACyD,QAAQ,CAACV,OAAO,CAACC,IAAI,EAAEhC,QAAQ;EAC1E,CAAC;AACH,CAAC"}
@@ -0,0 +1,108 @@
1
+ declare global {
2
+ function nativeCallSyncHook(): unknown;
3
+ var __OPSQLiteProxy: object | undefined;
4
+ }
5
+ export declare const OPSQLite: ISQLite;
6
+ /**
7
+ * Object returned by SQL Query executions {
8
+ * insertId: Represent the auto-generated row id if applicable
9
+ * rowsAffected: Number of affected rows if result of a update query
10
+ * message: if status === 1, here you will find error description
11
+ * rows: if status is undefined or 0 this object will contain the query results
12
+ * }
13
+ *
14
+ * @interface QueryResult
15
+ */
16
+ export type QueryResult = {
17
+ insertId?: number;
18
+ rowsAffected: number;
19
+ rows?: {
20
+ /** Raw array with all dataset */
21
+ _array: any[];
22
+ /** The lengh of the dataset */
23
+ length: number;
24
+ /** A convenience function to acess the index based the row object
25
+ * @param idx the row index
26
+ * @returns the row structure identified by column names
27
+ */
28
+ item: (idx: number) => any;
29
+ };
30
+ /**
31
+ * Query metadata, avaliable only for select query results
32
+ */
33
+ metadata?: ColumnMetadata[];
34
+ };
35
+ /**
36
+ * Column metadata
37
+ * Describes some information about columns fetched by the query
38
+ */
39
+ export type ColumnMetadata = {
40
+ /** The name used for this column for this resultset */
41
+ name: string;
42
+ /** 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. */
43
+ type: string;
44
+ /**
45
+ * The index for this column for this resultset*/
46
+ index: number;
47
+ };
48
+ /**
49
+ * Allows the execution of bulk of sql commands
50
+ * inside a transaction
51
+ * If a single query must be executed many times with different arguments, its preferred
52
+ * to declare it a single time, and use an array of array parameters.
53
+ */
54
+ export type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];
55
+ /**
56
+ * status: 0 or undefined for correct execution, 1 for error
57
+ * message: if status === 1, here you will find error description
58
+ * rowsAffected: Number of affected rows if status == 0
59
+ */
60
+ export type BatchQueryResult = {
61
+ rowsAffected?: number;
62
+ };
63
+ /**
64
+ * Result of loading a file and executing every line as a SQL command
65
+ * Similar to BatchQueryResult
66
+ */
67
+ export interface FileLoadResult extends BatchQueryResult {
68
+ commands?: number;
69
+ }
70
+ export interface Transaction {
71
+ commit: () => QueryResult;
72
+ execute: (query: string, params?: any[]) => QueryResult;
73
+ executeAsync: (query: string, params?: any[] | undefined) => Promise<QueryResult>;
74
+ rollback: () => QueryResult;
75
+ }
76
+ export interface PendingTransaction {
77
+ start: () => void;
78
+ }
79
+ interface ISQLite {
80
+ open: (dbName: string, location?: string) => void;
81
+ close: (dbName: string) => void;
82
+ delete: (dbName: string, location?: string) => void;
83
+ attach: (mainDbName: string, dbNameToAttach: string, alias: string, location?: string) => void;
84
+ detach: (mainDbName: string, alias: string) => void;
85
+ transaction: (dbName: string, fn: (tx: Transaction) => Promise<void> | void) => Promise<void>;
86
+ execute: (dbName: string, query: string, params?: any[]) => QueryResult;
87
+ executeAsync: (dbName: string, query: string, params?: any[]) => Promise<QueryResult>;
88
+ executeBatch: (dbName: string, commands: SQLBatchTuple[]) => BatchQueryResult;
89
+ executeBatchAsync: (dbName: string, commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
90
+ loadFile: (dbName: string, location: string) => Promise<FileLoadResult>;
91
+ }
92
+ export type OPSQLiteConnection = {
93
+ close: () => void;
94
+ delete: () => void;
95
+ attach: (dbNameToAttach: string, alias: string, location?: string) => void;
96
+ detach: (alias: string) => void;
97
+ transaction: (fn: (tx: Transaction) => Promise<void> | void) => Promise<void>;
98
+ execute: (query: string, params?: any[]) => QueryResult;
99
+ executeAsync: (query: string, params?: any[]) => Promise<QueryResult>;
100
+ executeBatch: (commands: SQLBatchTuple[]) => BatchQueryResult;
101
+ executeBatchAsync: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
102
+ loadFile: (location: string) => Promise<FileLoadResult>;
103
+ };
104
+ export declare const open: (options: {
105
+ name: string;
106
+ location?: string;
107
+ }) => OPSQLiteConnection;
108
+ export {};
@@ -0,0 +1,39 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "op-sqlite"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => "12.0", :osx => "10.7" }
14
+ s.source = { :git => "https://github.com/ospfranco/op-sqlite.git", :tag => "#{s.version}" }
15
+
16
+ s.pod_target_xcconfig = {
17
+ :GCC_PREPROCESSOR_DEFINITIONS => "HAVE_FULLFSYNC=1",
18
+ :WARNING_CFLAGS => "-Wno-shorten-64-to-32 -Wno-comma -Wno-unreachable-code -Wno-conditional-uninitialized -Wno-deprecated-declarations",
19
+ :USE_HEADERMAP => "No"
20
+ }
21
+
22
+ s.header_mappings_dir = "cpp"
23
+ s.source_files = "ios/**/*.{h,hpp,m,mm}", "cpp/**/*.{h,hpp,cpp,c}"
24
+
25
+ s.dependency "React-callinvoker"
26
+ s.dependency "React"
27
+ s.dependency "React-Core"
28
+
29
+ s.xcconfig = {
30
+ 'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
31
+ }
32
+
33
+
34
+ if ENV['OP_SQLITE_USE_PHONE_VERSION'] == '1' then
35
+ s.exclude_files = "cpp/sqlite3.c", "cpp/sqlite3.h"
36
+ s.library = "sqlite3"
37
+ end
38
+
39
+ end