@op-engineering/op-sqlite 15.0.1 → 15.0.3

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.
@@ -0,0 +1,272 @@
1
+ export type Scalar = string | number | boolean | null | ArrayBuffer | ArrayBufferView;
2
+ /**
3
+ * Object returned by SQL Query executions {
4
+ * insertId: Represent the auto-generated row id if applicable
5
+ * rowsAffected: Number of affected rows if result of a update query
6
+ * message: if status === 1, here you will find error description
7
+ * rows: if status is undefined or 0 this object will contain the query results
8
+ * }
9
+ *
10
+ * @interface QueryResult
11
+ */
12
+ export type QueryResult = {
13
+ insertId?: number;
14
+ rowsAffected: number;
15
+ res?: any[];
16
+ rows: Array<Record<string, Scalar>>;
17
+ rawRows?: Scalar[][];
18
+ columnNames?: string[];
19
+ /**
20
+ * Query metadata, available only for select query results
21
+ */
22
+ metadata?: ColumnMetadata[];
23
+ };
24
+ /**
25
+ * Column metadata
26
+ * Describes some information about columns fetched by the query
27
+ */
28
+ export type ColumnMetadata = {
29
+ /** The name used for this column for this result set */
30
+ name: string;
31
+ /** 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. */
32
+ type: string;
33
+ /**
34
+ * The index for this column for this result set*/
35
+ index: number;
36
+ };
37
+ /**
38
+ * Allows the execution of bulk of sql commands
39
+ * inside a transaction
40
+ * If a single query must be executed many times with different arguments, its preferred
41
+ * to declare it a single time, and use an array of array parameters.
42
+ */
43
+ export type SQLBatchTuple = [string] | [string, Scalar[]] | [string, Scalar[][]];
44
+ export type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';
45
+ /**
46
+ * status: 0 or undefined for correct execution, 1 for error
47
+ * message: if status === 1, here you will find error description
48
+ * rowsAffected: Number of affected rows if status == 0
49
+ */
50
+ export type BatchQueryResult = {
51
+ rowsAffected?: number;
52
+ };
53
+ /**
54
+ * Result of loading a file and executing every line as a SQL command
55
+ * Similar to BatchQueryResult
56
+ */
57
+ export type FileLoadResult = BatchQueryResult & {
58
+ commands?: number;
59
+ };
60
+ export type Transaction = {
61
+ commit: () => Promise<QueryResult>;
62
+ execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
63
+ rollback: () => QueryResult;
64
+ };
65
+ export type _PendingTransaction = {
66
+ start: () => void;
67
+ };
68
+ export type PreparedStatement = {
69
+ bind: (params: any[]) => Promise<void>;
70
+ bindSync: (params: any[]) => void;
71
+ execute: () => Promise<QueryResult>;
72
+ };
73
+ export type _InternalDB = {
74
+ close: () => void;
75
+ delete: (location?: string) => void;
76
+ attach: (params: {
77
+ secondaryDbFileName: string;
78
+ alias: string;
79
+ location?: string;
80
+ }) => void;
81
+ detach: (alias: string) => void;
82
+ transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
83
+ executeSync: (query: string, params?: Scalar[]) => QueryResult;
84
+ execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
85
+ executeWithHostObjects: (query: string, params?: Scalar[]) => Promise<QueryResult>;
86
+ executeBatch: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
87
+ loadFile: (location: string) => Promise<FileLoadResult>;
88
+ updateHook: (callback?: ((params: {
89
+ table: string;
90
+ operation: UpdateHookOperation;
91
+ row?: any;
92
+ rowId: number;
93
+ }) => void) | null) => void;
94
+ commitHook: (callback?: (() => void) | null) => void;
95
+ rollbackHook: (callback?: (() => void) | null) => void;
96
+ prepareStatement: (query: string) => PreparedStatement;
97
+ loadExtension: (path: string, entryPoint?: string) => void;
98
+ executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
99
+ executeRawSync: (query: string, params?: Scalar[]) => any[];
100
+ getDbPath: (location?: string) => string;
101
+ reactiveExecute: (params: {
102
+ query: string;
103
+ arguments: any[];
104
+ fireOn: {
105
+ table: string;
106
+ ids?: number[];
107
+ }[];
108
+ callback: (response: any) => void;
109
+ }) => () => void;
110
+ sync: () => void;
111
+ flushPendingReactiveQueries: () => Promise<void>;
112
+ };
113
+ export type DB = {
114
+ close: () => void;
115
+ delete: (location?: string) => void;
116
+ attach: (params: {
117
+ secondaryDbFileName: string;
118
+ alias: string;
119
+ location?: string;
120
+ }) => void;
121
+ detach: (alias: string) => void;
122
+ /**
123
+ * Wraps all the executions into a transaction. If an error is thrown it will rollback all of the changes
124
+ *
125
+ * You need to use this if you are using reactive queries for the queries to fire after the transaction is done
126
+ */
127
+ transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
128
+ /**
129
+ * Sync version of the execute function
130
+ * It will block the JS thread and therefore your UI and should be used with caution
131
+ *
132
+ * When writing your queries, you can use the ? character as a placeholder for parameters
133
+ * The parameters will be automatically escaped and sanitized
134
+ *
135
+ * Example:
136
+ * db.executeSync('SELECT * FROM table WHERE id = ?', [1]);
137
+ *
138
+ * If you are writing a query that doesn't require parameters, you can omit the second argument
139
+ *
140
+ * If you are writing to the database YOU SHOULD BE USING TRANSACTIONS!
141
+ * Transactions protect you from partial writes and ensure that your data is always in a consistent state
142
+ *
143
+ * @param query
144
+ * @param params
145
+ * @returns QueryResult
146
+ */
147
+ executeSync: (query: string, params?: Scalar[]) => QueryResult;
148
+ /**
149
+ * Basic query execution function, it is async don't forget to await it
150
+ *
151
+ * When writing your queries, you can use the ? character as a placeholder for parameters
152
+ * The parameters will be automatically escaped and sanitized
153
+ *
154
+ * Example:
155
+ * await db.execute('SELECT * FROM table WHERE id = ?', [1]);
156
+ *
157
+ * If you are writing a query that doesn't require parameters, you can omit the second argument
158
+ *
159
+ * If you are writing to the database YOU SHOULD BE USING TRANSACTIONS!
160
+ * Transactions protect you from partial writes and ensure that your data is always in a consistent state
161
+ *
162
+ * If you need a large amount of queries ran as fast as possible you should be using `executeBatch`, `executeRaw`, `loadFile` or `executeWithHostObjects`
163
+ *
164
+ * @param query string of your SQL query
165
+ * @param params a list of parameters to bind to the query, if any
166
+ * @returns Promise<QueryResult> with the result of the query
167
+ */
168
+ execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
169
+ /**
170
+ * Similar to the execute function but returns the response in HostObjects
171
+ * Read more about HostObjects in the documentation and their pitfalls
172
+ *
173
+ * Will be a lot faster than the normal execute functions when returning data but you will pay when accessing the fields
174
+ * as the conversion is done the moment you access any field
175
+ * @param query
176
+ * @param params
177
+ * @returns
178
+ */
179
+ executeWithHostObjects: (query: string, params?: Scalar[]) => Promise<QueryResult>;
180
+ /**
181
+ * Executes all the queries in the params inside a single transaction
182
+ *
183
+ * It's faster than executing single queries as data is sent to the native side only once
184
+ * @param commands
185
+ * @returns Promise<BatchQueryResult>
186
+ */
187
+ executeBatch: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
188
+ /**
189
+ * Loads a SQLite Dump from disk. It will be the fastest way to execute a large set of queries as no JS is involved
190
+ */
191
+ loadFile: (location: string) => Promise<FileLoadResult>;
192
+ updateHook: (callback?: ((params: {
193
+ table: string;
194
+ operation: UpdateHookOperation;
195
+ row?: any;
196
+ rowId: number;
197
+ }) => void) | null) => void;
198
+ commitHook: (callback?: (() => void) | null) => void;
199
+ rollbackHook: (callback?: (() => void) | null) => void;
200
+ /**
201
+ * Constructs a prepared statement from the query string
202
+ * The statement can be re-bound with parameters and executed
203
+ * The performance gain is significant when the same query is executed multiple times, NOT when the query is executed (once)
204
+ * The cost lies in the preparation of the statement as it is compiled and optimized by the sqlite engine, the params can then rebound
205
+ * but the query itself is already optimized
206
+ *
207
+ * @param query string of your SQL query
208
+ * @returns Prepared statement object
209
+ */
210
+ prepareStatement: (query: string) => PreparedStatement;
211
+ /**
212
+ * Loads a runtime loadable sqlite extension. Libsql and iOS embedded version do not support loading extensions
213
+ */
214
+ loadExtension: (path: string, entryPoint?: string) => void;
215
+ /**
216
+ * Same as `execute` except the results are not returned in objects but rather in arrays with just the values and not the keys
217
+ * It will be faster since a lot of repeated work is skipped and only the values you care about are returned
218
+ */
219
+ executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
220
+ /**
221
+ * Same as `executeRaw` but it will block the JS thread and therefore your UI and should be used with caution
222
+ * It will return an array of arrays with just the values and not the keys
223
+ */
224
+ executeRawSync: (query: string, params?: Scalar[]) => any[];
225
+ /**
226
+ * Get's the absolute path to the db file. Useful for debugging on local builds and for attaching the DB from users devices
227
+ */
228
+ getDbPath: (location?: string) => string;
229
+ /**
230
+ * Reactive execution of queries when data is written to the database. Check the docs for how to use them.
231
+ */
232
+ reactiveExecute: (params: {
233
+ query: string;
234
+ arguments: any[];
235
+ fireOn: {
236
+ table: string;
237
+ ids?: number[];
238
+ }[];
239
+ callback: (response: any) => void;
240
+ }) => () => void;
241
+ /** This function is only available for libsql.
242
+ * Allows to trigger a sync the database with it's remote replica
243
+ * In order for this function to work you need to use openSync or openRemote functions
244
+ * with libsql: true in the package.json
245
+ *
246
+ * The database is hosted in turso
247
+ **/
248
+ sync: () => void;
249
+ };
250
+ export type DBParams = {
251
+ url?: string;
252
+ authToken?: string;
253
+ name?: string;
254
+ location?: string;
255
+ syncInterval?: number;
256
+ };
257
+ export type OPSQLiteProxy = {
258
+ open: (options: {
259
+ name: string;
260
+ location?: string;
261
+ encryptionKey?: string;
262
+ }) => _InternalDB;
263
+ openRemote: (options: {
264
+ url: string;
265
+ authToken: string;
266
+ }) => _InternalDB;
267
+ openSync: (options: DBParams) => _InternalDB;
268
+ isSQLCipher: () => boolean;
269
+ isLibsql: () => boolean;
270
+ isIOSEmbedded: () => boolean;
271
+ };
272
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GACd,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,WAAW,GACX,eAAe,CAAC;AAEpB;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,GAAG,CAAC,EAAE,GAAG,EAAE,C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AAQ,GAAG;IACrB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,CAAC,OAAO,EAAE;QACd,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,KAAK,WAAW,CAAC;IAClB,UAAU,EAAE,CAAC,OAAO,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,KAAK,WAAW,CAAC;IACzE,QAAQ,EAAE,CAAC,OAAO,EAAE,QAAQ,KAAK,WAAW,CAAC;IAC7C,WAAW,EAAE,MAAM,OAAO,CAAC;IAC3B,QAAQ,EAAE,MAAM,OAAO,CAAC;IACxB,aAAa,EAAE,MAAM,OAAO,CAAC;CAC9B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@op-engineering/op-sqlite",
3
- "version": "15.0.1",
3
+ "version": "15.0.3",
4
4
  "description": "Next generation SQLite for React Native",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
package/src/Storage.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { type DB } from './index';
2
1
  import { open } from './functions';
2
+ import { type DB } from './types';
3
3
 
4
4
  type StorageOptions = {
5
5
  location?: string;
package/src/functions.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { NativeModules, Platform } from 'react-native';
2
2
  import {
3
3
  type _InternalDB,
4
- type DBParams,
5
- type DB,
6
4
  type _PendingTransaction,
7
- type SQLBatchTuple,
8
5
  type BatchQueryResult,
9
- type Scalar,
6
+ type DB,
7
+ type DBParams,
8
+ type OPSQLiteProxy,
10
9
  type QueryResult,
10
+ type Scalar,
11
+ type SQLBatchTuple,
11
12
  type Transaction,
12
- type OPSQLiteProxy,
13
- } from './index';
13
+ } from './types';
14
14
 
15
15
  declare global {
16
16
  var __OPSQLiteProxy: object | undefined;
@@ -97,22 +97,24 @@ function enhanceDB(db: _InternalDB, options: DBParams): DB {
97
97
  executeBatch: async (
98
98
  commands: SQLBatchTuple[]
99
99
  ): Promise<BatchQueryResult> => {
100
- const sanitizedCommands = commands.map(([query, params]) => {
101
- if (params) {
102
- return [query, sanitizeArrayBuffersInArray(params)];
100
+ // Do normal for loop and replace in place for performance
101
+ for (let i = 0; i < commands.length; i++) {
102
+ // [1] is the params arg
103
+ if (commands[i]![1]) {
104
+ commands[i]![1] = sanitizeArrayBuffersInArray(commands[i]![1]) as any;
103
105
  }
104
-
105
- return [query];
106
- });
106
+ }
107
107
 
108
108
  async function run() {
109
109
  try {
110
110
  enhancedDb.executeSync('BEGIN TRANSACTION;');
111
111
 
112
- let res = await db.executeBatch(sanitizedCommands as any[]);
112
+ let res = await db.executeBatch(commands as any[]);
113
113
 
114
114
  enhancedDb.executeSync('COMMIT;');
115
115
 
116
+ await db.flushPendingReactiveQueries();
117
+
116
118
  return res;
117
119
  } catch (executionError) {
118
120
  try {
package/src/index.ts CHANGED
@@ -1,321 +1,8 @@
1
1
  import { NativeModules } from 'react-native';
2
2
 
3
- export { Storage } from './Storage';
4
3
  export * from './functions';
5
-
6
- export type Scalar =
7
- | string
8
- | number
9
- | boolean
10
- | null
11
- | ArrayBuffer
12
- | ArrayBufferView;
13
-
14
- /**
15
- * Object returned by SQL Query executions {
16
- * insertId: Represent the auto-generated row id if applicable
17
- * rowsAffected: Number of affected rows if result of a update query
18
- * message: if status === 1, here you will find error description
19
- * rows: if status is undefined or 0 this object will contain the query results
20
- * }
21
- *
22
- * @interface QueryResult
23
- */
24
- export type QueryResult = {
25
- insertId?: number;
26
- rowsAffected: number;
27
- res?: any[];
28
- rows: Array<Record<string, Scalar>>;
29
- // An array of intermediate results, just values without column names
30
- rawRows?: Scalar[][];
31
- columnNames?: string[];
32
- /**
33
- * Query metadata, available only for select query results
34
- */
35
- metadata?: ColumnMetadata[];
36
- };
37
-
38
- /**
39
- * Column metadata
40
- * Describes some information about columns fetched by the query
41
- */
42
- export type ColumnMetadata = {
43
- /** The name used for this column for this result set */
44
- name: string;
45
- /** 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. */
46
- type: string;
47
- /**
48
- * The index for this column for this result set*/
49
- index: number;
50
- };
51
-
52
- /**
53
- * Allows the execution of bulk of sql commands
54
- * inside a transaction
55
- * If a single query must be executed many times with different arguments, its preferred
56
- * to declare it a single time, and use an array of array parameters.
57
- */
58
- export type SQLBatchTuple =
59
- | [string]
60
- | [string, Array<Scalar> | Array<Array<Scalar>>];
61
-
62
- export type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';
63
-
64
- /**
65
- * status: 0 or undefined for correct execution, 1 for error
66
- * message: if status === 1, here you will find error description
67
- * rowsAffected: Number of affected rows if status == 0
68
- */
69
- export type BatchQueryResult = {
70
- rowsAffected?: number;
71
- };
72
-
73
- /**
74
- * Result of loading a file and executing every line as a SQL command
75
- * Similar to BatchQueryResult
76
- */
77
- export type FileLoadResult = BatchQueryResult & {
78
- commands?: number;
79
- };
80
-
81
- export type Transaction = {
82
- commit: () => Promise<QueryResult>;
83
- execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
84
- rollback: () => QueryResult;
85
- };
86
-
87
- export type _PendingTransaction = {
88
- /*
89
- * The start function should not throw or return a promise because the
90
- * queue just calls it and does not monitor for failures or completions.
91
- *
92
- * It should catch any errors and call the resolve or reject of the wrapping
93
- * promise when complete.
94
- *
95
- * It should also automatically commit or rollback the transaction if needed
96
- */
97
- start: () => void;
98
- };
99
-
100
- export type PreparedStatement = {
101
- bind: (params: any[]) => Promise<void>;
102
- bindSync: (params: any[]) => void;
103
- execute: () => Promise<QueryResult>;
104
- };
105
-
106
- export type _InternalDB = {
107
- close: () => void;
108
- delete: (location?: string) => void;
109
- attach: (params: {
110
- secondaryDbFileName: string;
111
- alias: string;
112
- location?: string;
113
- }) => void;
114
- detach: (alias: string) => void;
115
- transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
116
- executeSync: (query: string, params?: Scalar[]) => QueryResult;
117
- execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
118
- executeWithHostObjects: (
119
- query: string,
120
- params?: Scalar[]
121
- ) => Promise<QueryResult>;
122
- executeBatch: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
123
- loadFile: (location: string) => Promise<FileLoadResult>;
124
- updateHook: (
125
- callback?:
126
- | ((params: {
127
- table: string;
128
- operation: UpdateHookOperation;
129
- row?: any;
130
- rowId: number;
131
- }) => void)
132
- | null
133
- ) => void;
134
- commitHook: (callback?: (() => void) | null) => void;
135
- rollbackHook: (callback?: (() => void) | null) => void;
136
- prepareStatement: (query: string) => PreparedStatement;
137
- loadExtension: (path: string, entryPoint?: string) => void;
138
- executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
139
- executeRawSync: (query: string, params?: Scalar[]) => any[];
140
- getDbPath: (location?: string) => string;
141
- reactiveExecute: (params: {
142
- query: string;
143
- arguments: any[];
144
- fireOn: {
145
- table: string;
146
- ids?: number[];
147
- }[];
148
- callback: (response: any) => void;
149
- }) => () => void;
150
- sync: () => void;
151
- flushPendingReactiveQueries: () => Promise<void>;
152
- };
153
-
154
- export type DB = {
155
- close: () => void;
156
- delete: (location?: string) => void;
157
- attach: (params: {
158
- secondaryDbFileName: string;
159
- alias: string;
160
- location?: string;
161
- }) => void;
162
- detach: (alias: string) => void;
163
- /**
164
- * Wraps all the executions into a transaction. If an error is thrown it will rollback all of the changes
165
- *
166
- * You need to use this if you are using reactive queries for the queries to fire after the transaction is done
167
- */
168
- transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
169
- /**
170
- * Sync version of the execute function
171
- * It will block the JS thread and therefore your UI and should be used with caution
172
- *
173
- * When writing your queries, you can use the ? character as a placeholder for parameters
174
- * The parameters will be automatically escaped and sanitized
175
- *
176
- * Example:
177
- * db.executeSync('SELECT * FROM table WHERE id = ?', [1]);
178
- *
179
- * If you are writing a query that doesn't require parameters, you can omit the second argument
180
- *
181
- * If you are writing to the database YOU SHOULD BE USING TRANSACTIONS!
182
- * Transactions protect you from partial writes and ensure that your data is always in a consistent state
183
- *
184
- * @param query
185
- * @param params
186
- * @returns QueryResult
187
- */
188
- executeSync: (query: string, params?: Scalar[]) => QueryResult;
189
- /**
190
- * Basic query execution function, it is async don't forget to await it
191
- *
192
- * When writing your queries, you can use the ? character as a placeholder for parameters
193
- * The parameters will be automatically escaped and sanitized
194
- *
195
- * Example:
196
- * await db.execute('SELECT * FROM table WHERE id = ?', [1]);
197
- *
198
- * If you are writing a query that doesn't require parameters, you can omit the second argument
199
- *
200
- * If you are writing to the database YOU SHOULD BE USING TRANSACTIONS!
201
- * Transactions protect you from partial writes and ensure that your data is always in a consistent state
202
- *
203
- * If you need a large amount of queries ran as fast as possible you should be using `executeBatch`, `executeRaw`, `loadFile` or `executeWithHostObjects`
204
- *
205
- * @param query string of your SQL query
206
- * @param params a list of parameters to bind to the query, if any
207
- * @returns Promise<QueryResult> with the result of the query
208
- */
209
- execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
210
- /**
211
- * Similar to the execute function but returns the response in HostObjects
212
- * Read more about HostObjects in the documentation and their pitfalls
213
- *
214
- * Will be a lot faster than the normal execute functions when returning data but you will pay when accessing the fields
215
- * as the conversion is done the moment you access any field
216
- * @param query
217
- * @param params
218
- * @returns
219
- */
220
- executeWithHostObjects: (
221
- query: string,
222
- params?: Scalar[]
223
- ) => Promise<QueryResult>;
224
- /**
225
- * Executes all the queries in the params inside a single transaction
226
- *
227
- * It's faster than executing single queries as data is sent to the native side only once
228
- * @param commands
229
- * @returns Promise<BatchQueryResult>
230
- */
231
- executeBatch: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
232
- /**
233
- * Loads a SQLite Dump from disk. It will be the fastest way to execute a large set of queries as no JS is involved
234
- */
235
- loadFile: (location: string) => Promise<FileLoadResult>;
236
- updateHook: (
237
- callback?:
238
- | ((params: {
239
- table: string;
240
- operation: UpdateHookOperation;
241
- row?: any;
242
- rowId: number;
243
- }) => void)
244
- | null
245
- ) => void;
246
- commitHook: (callback?: (() => void) | null) => void;
247
- rollbackHook: (callback?: (() => void) | null) => void;
248
- /**
249
- * Constructs a prepared statement from the query string
250
- * The statement can be re-bound with parameters and executed
251
- * The performance gain is significant when the same query is executed multiple times, NOT when the query is executed (once)
252
- * The cost lies in the preparation of the statement as it is compiled and optimized by the sqlite engine, the params can then rebound
253
- * but the query itself is already optimized
254
- *
255
- * @param query string of your SQL query
256
- * @returns Prepared statement object
257
- */
258
- prepareStatement: (query: string) => PreparedStatement;
259
- /**
260
- * Loads a runtime loadable sqlite extension. Libsql and iOS embedded version do not support loading extensions
261
- */
262
- loadExtension: (path: string, entryPoint?: string) => void;
263
- /**
264
- * Same as `execute` except the results are not returned in objects but rather in arrays with just the values and not the keys
265
- * It will be faster since a lot of repeated work is skipped and only the values you care about are returned
266
- */
267
- executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
268
- /**
269
- * Same as `executeRaw` but it will block the JS thread and therefore your UI and should be used with caution
270
- * It will return an array of arrays with just the values and not the keys
271
- */
272
- executeRawSync: (query: string, params?: Scalar[]) => any[];
273
- /**
274
- * Get's the absolute path to the db file. Useful for debugging on local builds and for attaching the DB from users devices
275
- */
276
- getDbPath: (location?: string) => string;
277
- /**
278
- * Reactive execution of queries when data is written to the database. Check the docs for how to use them.
279
- */
280
- reactiveExecute: (params: {
281
- query: string;
282
- arguments: any[];
283
- fireOn: {
284
- table: string;
285
- ids?: number[];
286
- }[];
287
- callback: (response: any) => void;
288
- }) => () => void;
289
- /** This function is only available for libsql.
290
- * Allows to trigger a sync the database with it's remote replica
291
- * In order for this function to work you need to use openSync or openRemote functions
292
- * with libsql: true in the package.json
293
- *
294
- * The database is hosted in turso
295
- **/
296
- sync: () => void;
297
- };
298
-
299
- export type DBParams = {
300
- url?: string;
301
- authToken?: string;
302
- name?: string;
303
- location?: string;
304
- syncInterval?: number;
305
- };
306
-
307
- export type OPSQLiteProxy = {
308
- open: (options: {
309
- name: string;
310
- location?: string;
311
- encryptionKey?: string;
312
- }) => _InternalDB;
313
- openRemote: (options: { url: string; authToken: string }) => _InternalDB;
314
- openSync: (options: DBParams) => _InternalDB;
315
- isSQLCipher: () => boolean;
316
- isLibsql: () => boolean;
317
- isIOSEmbedded: () => boolean;
318
- };
4
+ export { Storage } from './Storage';
5
+ export * from './types';
319
6
 
320
7
  export const {
321
8
  IOS_DOCUMENT_PATH,