expo-sqlite 12.1.0 → 12.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/android/build.gradle +2 -2
- package/android/src/main/jniLibs/arm64-v8a/libcrsqlite.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libcrsqlite.so +0 -0
- package/android/src/main/jniLibs/x86/libcrsqlite.so +0 -0
- package/android/src/main/jniLibs/x86_64/libcrsqlite.so +0 -0
- package/build/next/Database.d.ts +91 -73
- package/build/next/Database.d.ts.map +1 -1
- package/build/next/Database.js +22 -19
- package/build/next/Database.js.map +1 -1
- package/build/next/NativeDatabase.d.ts +1 -1
- package/build/next/NativeDatabase.js.map +1 -1
- package/build/next/NativeStatement.d.ts +5 -5
- package/build/next/NativeStatement.js.map +1 -1
- package/build/next/Statement.d.ts +46 -42
- package/build/next/Statement.d.ts.map +1 -1
- package/build/next/Statement.js +1 -1
- package/build/next/Statement.js.map +1 -1
- package/build/next/hooks.d.ts +25 -0
- package/build/next/hooks.d.ts.map +1 -1
- package/build/next/hooks.js +28 -3
- package/build/next/hooks.js.map +1 -1
- package/ios/SQLiteModuleNext.swift +3 -3
- package/package.json +2 -2
- package/src/next/Database.ts +109 -86
- package/src/next/NativeDatabase.ts +1 -1
- package/src/next/NativeStatement.ts +5 -5
- package/src/next/Statement.ts +46 -42
- package/src/next/hooks.tsx +29 -4
- package/android/src/main/jniLibs/arm64/libcrsqlite.so +0 -0
package/src/next/Database.ts
CHANGED
|
@@ -14,6 +14,7 @@ const emitter = new EventEmitter(ExpoSQLite);
|
|
|
14
14
|
export class Database {
|
|
15
15
|
constructor(
|
|
16
16
|
public readonly dbName: string,
|
|
17
|
+
public readonly options: OpenOptions,
|
|
17
18
|
private readonly nativeDatabase: NativeDatabase
|
|
18
19
|
) {}
|
|
19
20
|
|
|
@@ -45,7 +46,6 @@ export class Database {
|
|
|
45
46
|
* Prepare a SQL statement.
|
|
46
47
|
*
|
|
47
48
|
* @param source A string containing the SQL query.
|
|
48
|
-
* @returns A `Statement` object.
|
|
49
49
|
*/
|
|
50
50
|
public async prepareAsync(source: string): Promise<Statement> {
|
|
51
51
|
const nativeStatement = new ExpoSQLite.NativeStatement();
|
|
@@ -140,7 +140,8 @@ export class Database {
|
|
|
140
140
|
* Execute all SQL queries in the supplied string.
|
|
141
141
|
*
|
|
142
142
|
* > **Note:** The queries are not escaped for you! Be careful when constructing your queries.
|
|
143
|
-
*
|
|
143
|
+
*
|
|
144
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
144
145
|
*
|
|
145
146
|
* @param source A string containing all the SQL queries.
|
|
146
147
|
*/
|
|
@@ -151,10 +152,9 @@ export class Database {
|
|
|
151
152
|
/**
|
|
152
153
|
* Prepare a SQL statement.
|
|
153
154
|
*
|
|
154
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread
|
|
155
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
155
156
|
*
|
|
156
157
|
* @param source A string containing the SQL query.
|
|
157
|
-
* @returns A `Statement` object.
|
|
158
158
|
*/
|
|
159
159
|
public prepareSync(source: string): Statement {
|
|
160
160
|
const nativeStatement = new ExpoSQLite.NativeStatement();
|
|
@@ -165,7 +165,7 @@ export class Database {
|
|
|
165
165
|
/**
|
|
166
166
|
* Execute a transaction and automatically commit/rollback based on the `task` result.
|
|
167
167
|
*
|
|
168
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread
|
|
168
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
169
169
|
*
|
|
170
170
|
* @param task An async function to execute within a transaction.
|
|
171
171
|
*/
|
|
@@ -183,16 +183,17 @@ export class Database {
|
|
|
183
183
|
//#region Statement API shorthands
|
|
184
184
|
|
|
185
185
|
/**
|
|
186
|
-
* Shorthand for `prepareAsync` and `Statement.runAsync
|
|
187
|
-
* Unlike `Statement.runAsync
|
|
188
|
-
*
|
|
189
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
190
|
-
*
|
|
186
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.runAsync()`](#runasyncparams).
|
|
187
|
+
* Unlike [`Statement.runAsync()`](#runasyncparams), this method finalizes the statement after execution.
|
|
191
188
|
* @param source A string containing the SQL query.
|
|
192
|
-
* @param params
|
|
189
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
193
190
|
*/
|
|
194
|
-
public runAsync(source: string, ...params: VariadicBindParams): Promise<RunResult>;
|
|
195
191
|
public runAsync(source: string, params: BindParams): Promise<RunResult>;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @hidden
|
|
195
|
+
*/
|
|
196
|
+
public runAsync(source: string, ...params: VariadicBindParams): Promise<RunResult>;
|
|
196
197
|
public async runAsync(source: string, ...params: any[]): Promise<RunResult> {
|
|
197
198
|
const statement = await this.prepareAsync(source);
|
|
198
199
|
let result;
|
|
@@ -205,16 +206,16 @@ export class Database {
|
|
|
205
206
|
}
|
|
206
207
|
|
|
207
208
|
/**
|
|
208
|
-
* Shorthand for `prepareAsync` and `Statement.getAsync
|
|
209
|
-
* Unlike `Statement.getAsync
|
|
210
|
-
*
|
|
211
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
212
|
-
*
|
|
209
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.getAsync()`](#getasyncparams).
|
|
210
|
+
* Unlike [`Statement.getAsync()`](#getasyncparams), this method finalizes the statement after execution.
|
|
213
211
|
* @param source A string containing the SQL query.
|
|
214
|
-
* @param params
|
|
212
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
215
213
|
*/
|
|
216
|
-
public getAsync<T>(source: string, ...params: VariadicBindParams): Promise<T | null>;
|
|
217
214
|
public getAsync<T>(source: string, params: BindParams): Promise<T | null>;
|
|
215
|
+
/**
|
|
216
|
+
* @hidden
|
|
217
|
+
*/
|
|
218
|
+
public getAsync<T>(source: string, ...params: VariadicBindParams): Promise<T | null>;
|
|
218
219
|
public async getAsync<T>(source: string, ...params: any[]): Promise<T | null> {
|
|
219
220
|
const statement = await this.prepareAsync(source);
|
|
220
221
|
let result;
|
|
@@ -227,16 +228,16 @@ export class Database {
|
|
|
227
228
|
}
|
|
228
229
|
|
|
229
230
|
/**
|
|
230
|
-
* Shorthand for `prepareAsync` and `Statement.eachAsync
|
|
231
|
-
* Unlike `Statement.eachAsync
|
|
232
|
-
*
|
|
233
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
234
|
-
*
|
|
231
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.eachAsync()`](#eachasyncparams).
|
|
232
|
+
* Unlike [`Statement.eachAsync()`](#eachasyncparams), this method finalizes the statement after execution.
|
|
235
233
|
* @param source A string containing the SQL query.
|
|
236
|
-
* @param params
|
|
234
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
237
235
|
*/
|
|
238
|
-
public eachAsync<T>(source: string, ...params: VariadicBindParams): AsyncIterableIterator<T>;
|
|
239
236
|
public eachAsync<T>(source: string, params: BindParams): AsyncIterableIterator<T>;
|
|
237
|
+
/**
|
|
238
|
+
* @hidden
|
|
239
|
+
*/
|
|
240
|
+
public eachAsync<T>(source: string, ...params: VariadicBindParams): AsyncIterableIterator<T>;
|
|
240
241
|
public async *eachAsync<T>(source: string, ...params: any[]): AsyncIterableIterator<T> {
|
|
241
242
|
const statement = await this.prepareAsync(source);
|
|
242
243
|
try {
|
|
@@ -247,16 +248,27 @@ export class Database {
|
|
|
247
248
|
}
|
|
248
249
|
|
|
249
250
|
/**
|
|
250
|
-
* Shorthand for `prepareAsync` and `Statement.allAsync
|
|
251
|
-
* Unlike `Statement.allAsync
|
|
251
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.allAsync()`](#allasyncparams).
|
|
252
|
+
* Unlike [`Statement.allAsync()`](#allasyncparams), this method finalizes the statement after execution.
|
|
253
|
+
* @param source A string containing the SQL query.
|
|
254
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* // For unnamed parameters, you pass values in an array.
|
|
258
|
+
* db.allAsync('SELECT * FROM test WHERE intValue = ? AND name = ?', [1, 'Hello']);
|
|
252
259
|
*
|
|
253
|
-
*
|
|
260
|
+
* // For unnamed parameters, you pass values in variadic arguments.
|
|
261
|
+
* db.allAsync('SELECT * FROM test WHERE intValue = ? AND name = ?', 1, 'Hello');
|
|
254
262
|
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
263
|
+
* // For named parameters, you should pass values in object.
|
|
264
|
+
* db.allAsync('SELECT * FROM test WHERE intValue = $intValue AND name = $name', { $intValue: 1, $name: 'Hello' });
|
|
265
|
+
* ```
|
|
257
266
|
*/
|
|
258
|
-
public allAsync<T>(source: string, ...params: VariadicBindParams): Promise<T[]>;
|
|
259
267
|
public allAsync<T>(source: string, params: BindParams): Promise<T[]>;
|
|
268
|
+
/**
|
|
269
|
+
* @hidden
|
|
270
|
+
*/
|
|
271
|
+
public allAsync<T>(source: string, ...params: VariadicBindParams): Promise<T[]>;
|
|
260
272
|
public async allAsync<T>(source: string, ...params: any[]): Promise<T[]> {
|
|
261
273
|
const statement = await this.prepareAsync(source);
|
|
262
274
|
let result;
|
|
@@ -269,16 +281,17 @@ export class Database {
|
|
|
269
281
|
}
|
|
270
282
|
|
|
271
283
|
/**
|
|
272
|
-
* Shorthand for `
|
|
273
|
-
* Unlike `Statement.runSync
|
|
274
|
-
*
|
|
275
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
276
|
-
*
|
|
284
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.runSync()`](#runsyncparams).
|
|
285
|
+
* Unlike [`Statement.runSync()`](#runsyncparams), this method finalizes the statement after execution.
|
|
286
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
277
287
|
* @param source A string containing the SQL query.
|
|
278
|
-
* @param params
|
|
288
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
279
289
|
*/
|
|
280
|
-
public runSync(source: string, ...params: VariadicBindParams): RunResult;
|
|
281
290
|
public runSync(source: string, params: BindParams): RunResult;
|
|
291
|
+
/**
|
|
292
|
+
* @hidden
|
|
293
|
+
*/
|
|
294
|
+
public runSync(source: string, ...params: VariadicBindParams): RunResult;
|
|
282
295
|
public runSync(source: string, ...params: any[]): RunResult {
|
|
283
296
|
const statement = this.prepareSync(source);
|
|
284
297
|
let result;
|
|
@@ -291,16 +304,17 @@ export class Database {
|
|
|
291
304
|
}
|
|
292
305
|
|
|
293
306
|
/**
|
|
294
|
-
* Shorthand for `
|
|
295
|
-
* Unlike `Statement.getSync
|
|
296
|
-
*
|
|
297
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
298
|
-
*
|
|
307
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.getSync()`](#getsyncparams).
|
|
308
|
+
* Unlike [`Statement.getSync()`](#getsyncparams), this method finalizes the statement after execution.
|
|
309
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
299
310
|
* @param source A string containing the SQL query.
|
|
300
|
-
* @param params
|
|
311
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
301
312
|
*/
|
|
302
|
-
public getSync<T>(source: string, ...params: VariadicBindParams): T | null;
|
|
303
313
|
public getSync<T>(source: string, params: BindParams): T | null;
|
|
314
|
+
/**
|
|
315
|
+
* @hidden
|
|
316
|
+
*/
|
|
317
|
+
public getSync<T>(source: string, ...params: VariadicBindParams): T | null;
|
|
304
318
|
public getSync<T>(source: string, ...params: any[]): T | null {
|
|
305
319
|
const statement = this.prepareSync(source);
|
|
306
320
|
let result;
|
|
@@ -313,16 +327,17 @@ export class Database {
|
|
|
313
327
|
}
|
|
314
328
|
|
|
315
329
|
/**
|
|
316
|
-
* Shorthand for `
|
|
317
|
-
* Unlike `Statement.eachSync
|
|
318
|
-
*
|
|
319
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
320
|
-
*
|
|
330
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.eachSync()`](#eachsyncparams).
|
|
331
|
+
* Unlike [`Statement.eachSync()`](#eachsyncparams), this method finalizes the statement after execution.
|
|
332
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
321
333
|
* @param source A string containing the SQL query.
|
|
322
|
-
* @param params
|
|
334
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
323
335
|
*/
|
|
324
|
-
public eachSync<T>(source: string, ...params: VariadicBindParams): IterableIterator<T>;
|
|
325
336
|
public eachSync<T>(source: string, params: BindParams): IterableIterator<T>;
|
|
337
|
+
/**
|
|
338
|
+
* @hidden
|
|
339
|
+
*/
|
|
340
|
+
public eachSync<T>(source: string, ...params: VariadicBindParams): IterableIterator<T>;
|
|
326
341
|
public *eachSync<T>(source: string, ...params: any[]): IterableIterator<T> {
|
|
327
342
|
const statement = this.prepareSync(source);
|
|
328
343
|
try {
|
|
@@ -333,16 +348,17 @@ export class Database {
|
|
|
333
348
|
}
|
|
334
349
|
|
|
335
350
|
/**
|
|
336
|
-
* Shorthand for `
|
|
337
|
-
* Unlike `Statement.allSync
|
|
338
|
-
*
|
|
339
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
340
|
-
*
|
|
351
|
+
* Shorthand for [`prepareAsync()`](#prepareasyncsource) and [`Statement.allSync()`](#allsyncparams).
|
|
352
|
+
* Unlike [`Statement.allSync()`](#allsyncparams), this method finalizes the statement after execution.
|
|
353
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
341
354
|
* @param source A string containing the SQL query.
|
|
342
|
-
* @param params
|
|
355
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
343
356
|
*/
|
|
344
|
-
public allSync<T>(source: string, ...params: VariadicBindParams): T[];
|
|
345
357
|
public allSync<T>(source: string, params: BindParams): T[];
|
|
358
|
+
/**
|
|
359
|
+
* @hidden
|
|
360
|
+
*/
|
|
361
|
+
public allSync<T>(source: string, ...params: VariadicBindParams): T[];
|
|
346
362
|
public allSync<T>(source: string, ...params: any[]): T[] {
|
|
347
363
|
const statement = this.prepareSync(source);
|
|
348
364
|
let result;
|
|
@@ -362,27 +378,27 @@ export class Database {
|
|
|
362
378
|
*
|
|
363
379
|
* @param dbName The name of the database file to open.
|
|
364
380
|
* @param options Open options.
|
|
365
|
-
* @returns Database object.
|
|
366
381
|
*/
|
|
367
382
|
export async function openDatabaseAsync(dbName: string, options?: OpenOptions): Promise<Database> {
|
|
368
|
-
const
|
|
383
|
+
const openOptions = options ?? {};
|
|
384
|
+
const nativeDatabase = new ExpoSQLite.NativeDatabase(dbName, openOptions);
|
|
369
385
|
await nativeDatabase.initAsync();
|
|
370
|
-
return new Database(dbName, nativeDatabase);
|
|
386
|
+
return new Database(dbName, openOptions, nativeDatabase);
|
|
371
387
|
}
|
|
372
388
|
|
|
373
389
|
/**
|
|
374
390
|
* Open a database.
|
|
375
391
|
*
|
|
376
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread
|
|
392
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
377
393
|
*
|
|
378
394
|
* @param dbName The name of the database file to open.
|
|
379
395
|
* @param options Open options.
|
|
380
|
-
* @returns Database object.
|
|
381
396
|
*/
|
|
382
397
|
export function openDatabaseSync(dbName: string, options?: OpenOptions): Database {
|
|
383
|
-
const
|
|
398
|
+
const openOptions = options ?? {};
|
|
399
|
+
const nativeDatabase = new ExpoSQLite.NativeDatabase(dbName, openOptions);
|
|
384
400
|
nativeDatabase.initSync();
|
|
385
|
-
return new Database(dbName, nativeDatabase);
|
|
401
|
+
return new Database(dbName, openOptions, nativeDatabase);
|
|
386
402
|
}
|
|
387
403
|
|
|
388
404
|
/**
|
|
@@ -397,7 +413,7 @@ export async function deleteDatabaseAsync(dbName: string): Promise<void> {
|
|
|
397
413
|
/**
|
|
398
414
|
* Delete a database file.
|
|
399
415
|
*
|
|
400
|
-
* > **Note:** Running heavy tasks with this function can block the JavaScript thread
|
|
416
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
401
417
|
*
|
|
402
418
|
* @param dbName The name of the database file to delete.
|
|
403
419
|
*/
|
|
@@ -405,38 +421,45 @@ export function deleteDatabaseSync(dbName: string): void {
|
|
|
405
421
|
return ExpoSQLite.deleteDatabaseSync(dbName);
|
|
406
422
|
}
|
|
407
423
|
|
|
424
|
+
/**
|
|
425
|
+
* The event payload for the listener of [`addDatabaseChangeListener`](#sqliteadddatabasechangelistenerlistener)
|
|
426
|
+
*/
|
|
427
|
+
export type DatabaseChangeEvent = {
|
|
428
|
+
/** The database name. The value would be `main` by default and other database names if you use `ATTACH DATABASE` statement. */
|
|
429
|
+
dbName: string;
|
|
430
|
+
|
|
431
|
+
/** The absolute file path to the database. */
|
|
432
|
+
dbFilePath: string;
|
|
433
|
+
|
|
434
|
+
/** The table name. */
|
|
435
|
+
tableName: string;
|
|
436
|
+
|
|
437
|
+
/** The changed row ID. */
|
|
438
|
+
rowId: number;
|
|
439
|
+
};
|
|
440
|
+
|
|
408
441
|
/**
|
|
409
442
|
* Add a listener for database changes.
|
|
410
|
-
* > Note: to enable this feature, you must set `enableChangeListener` to `true` when opening the database.
|
|
443
|
+
* > Note: to enable this feature, you must set [`enableChangeListener` to `true`](#openoptions) when opening the database.
|
|
411
444
|
*
|
|
412
|
-
* @param listener A function that receives the `dbName`, `tableName` and `rowId` of the modified data.
|
|
445
|
+
* @param listener A function that receives the `dbFilePath`, `dbName`, `tableName` and `rowId` of the modified data.
|
|
413
446
|
* @returns A `Subscription` object that you can call `remove()` on when you would like to unsubscribe the listener.
|
|
414
447
|
*/
|
|
415
448
|
export function addDatabaseChangeListener(
|
|
416
|
-
listener: (event:
|
|
417
|
-
/** The database name. The value would be `main` by default and other database names if you use `ATTACH DATABASE` statement. */
|
|
418
|
-
dbName: string;
|
|
419
|
-
|
|
420
|
-
/** The absolute file path to the database. */
|
|
421
|
-
dbFilePath: string;
|
|
422
|
-
|
|
423
|
-
/** The table name. */
|
|
424
|
-
tableName: string;
|
|
425
|
-
|
|
426
|
-
/** The changed row ID. */
|
|
427
|
-
rowId: number;
|
|
428
|
-
}) => void
|
|
449
|
+
listener: (event: DatabaseChangeEvent) => void
|
|
429
450
|
): Subscription {
|
|
430
451
|
return emitter.addListener('onDatabaseChange', listener);
|
|
431
452
|
}
|
|
432
453
|
|
|
433
454
|
/**
|
|
434
|
-
* A new connection specific for `transactionExclusiveAsync
|
|
455
|
+
* A new connection specific used for [`transactionExclusiveAsync`](#transactionexclusiveasynctask).
|
|
456
|
+
* @hidden not going to pull all the database methods to the document.
|
|
435
457
|
*/
|
|
436
458
|
class Transaction extends Database {
|
|
437
459
|
public static async createAsync(db: Database): Promise<Transaction> {
|
|
438
|
-
const
|
|
460
|
+
const options = { ...db.options, useNewConnection: true };
|
|
461
|
+
const nativeDatabase = new ExpoSQLite.NativeDatabase(db.dbName, options);
|
|
439
462
|
await nativeDatabase.initAsync();
|
|
440
|
-
return new Transaction(db.dbName, nativeDatabase);
|
|
463
|
+
return new Transaction(db.dbName, options, nativeDatabase);
|
|
441
464
|
}
|
|
442
465
|
}
|
|
@@ -38,7 +38,7 @@ export interface OpenOptions {
|
|
|
38
38
|
enableCRSQLite?: boolean;
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* Whether to call the `sqlite3_update_hook` function and enable the `onDatabaseChange` events.
|
|
41
|
+
* Whether to call the [`sqlite3_update_hook`](https://www.sqlite.org/c3ref/update_hook.html) function and enable the `onDatabaseChange` events. You can later subscribe to the change events by [`addDatabaseChangeListener`](#sqliteadddatabasechangelistenerlistener).
|
|
42
42
|
* @default false
|
|
43
43
|
*/
|
|
44
44
|
enableChangeListener?: boolean;
|
|
@@ -18,23 +18,23 @@ export interface RunResult {
|
|
|
18
18
|
* You can either pass the parameters in the following forms:
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
21
|
-
* -
|
|
21
|
+
* - A single array for unnamed parameters.
|
|
22
22
|
* ```ts
|
|
23
23
|
* const statement = await db.prepareAsync('SELECT * FROM test WHERE value = ? AND intValue = ?');
|
|
24
|
-
* await statement.getAsync('test1', 789);
|
|
24
|
+
* await statement.getAsync(['test1', 789]);
|
|
25
25
|
* ```
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
|
-
* -
|
|
28
|
+
* - Variadic arguments for unnamed parameters.
|
|
29
29
|
* ```ts
|
|
30
30
|
* const statement = await db.prepareAsync('SELECT * FROM test WHERE value = ? AND intValue = ?');
|
|
31
|
-
* await statement.getAsync(
|
|
31
|
+
* await statement.getAsync('test1', 789);
|
|
32
32
|
* ```
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* - A single object for [named parameters](https://www.sqlite.org/lang_expr.html)
|
|
36
36
|
*
|
|
37
|
-
*
|
|
37
|
+
* We support multiple named parameter forms such as `:VVV`, `@VVV`, and `$VVV`. We recommend using `$VVV` because JavaScript allows using `$` in identifiers without escaping.
|
|
38
38
|
* ```ts
|
|
39
39
|
* const statement = await db.prepareAsync('SELECT * FROM test WHERE value = $value AND intValue = $intValue');
|
|
40
40
|
* await statement.getAsync({ $value: 'test1', $intValue: 789 });
|
package/src/next/Statement.ts
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
export { BindParams, BindValue, RunResult, VariadicBindParams };
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* A prepared statement returned by `Database.prepareAsync()` that can be binded with parameters and executed.
|
|
15
|
+
* A prepared statement returned by [`Database.prepareAsync()`](#prepareasyncsource) or [`Database.prepareSync()`](#preparesyncsource) that can be binded with parameters and executed.
|
|
16
16
|
*/
|
|
17
17
|
export class Statement {
|
|
18
18
|
constructor(
|
|
@@ -24,11 +24,13 @@ export class Statement {
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Run the prepared statement and return the result.
|
|
27
|
-
*
|
|
28
|
-
* @param params @see `BindParams`
|
|
27
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
29
28
|
*/
|
|
30
|
-
public runAsync(...params: VariadicBindParams): Promise<RunResult>;
|
|
31
29
|
public runAsync(params: BindParams): Promise<RunResult>;
|
|
30
|
+
/**
|
|
31
|
+
* @hidden
|
|
32
|
+
*/
|
|
33
|
+
public runAsync(...params: VariadicBindParams): Promise<RunResult>;
|
|
32
34
|
public async runAsync(...params: unknown[]): Promise<RunResult> {
|
|
33
35
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
34
36
|
if (shouldPassAsObject) {
|
|
@@ -40,19 +42,21 @@ export class Statement {
|
|
|
40
42
|
|
|
41
43
|
/**
|
|
42
44
|
* Iterate the prepared statement and return results as an async iterable.
|
|
43
|
-
*
|
|
44
|
-
* @param params @see `BindParams`
|
|
45
|
-
*
|
|
45
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
46
46
|
* @example
|
|
47
47
|
* ```ts
|
|
48
48
|
* const statement = await db.prepareAsync('SELECT * FROM test');
|
|
49
49
|
* for await (const row of statement.eachAsync<any>()) {
|
|
50
50
|
* console.log(row);
|
|
51
51
|
* }
|
|
52
|
+
* await statement.finalizeAsync();
|
|
52
53
|
* ```
|
|
53
54
|
*/
|
|
54
|
-
public eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;
|
|
55
55
|
public eachAsync<T>(params: BindParams): AsyncIterableIterator<T>;
|
|
56
|
+
/**
|
|
57
|
+
* @hidden
|
|
58
|
+
*/
|
|
59
|
+
public eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;
|
|
56
60
|
public async *eachAsync<T>(...params: unknown[]): AsyncIterableIterator<T> {
|
|
57
61
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
58
62
|
const func = shouldPassAsObject
|
|
@@ -71,11 +75,13 @@ export class Statement {
|
|
|
71
75
|
|
|
72
76
|
/**
|
|
73
77
|
* Get one row from the prepared statement.
|
|
74
|
-
*
|
|
75
|
-
* @param params @see `BindParams`
|
|
78
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
76
79
|
*/
|
|
77
|
-
public getAsync<T>(...params: VariadicBindParams): Promise<T | null>;
|
|
78
80
|
public getAsync<T>(params: BindParams): Promise<T | null>;
|
|
81
|
+
/**
|
|
82
|
+
* @hidden
|
|
83
|
+
*/
|
|
84
|
+
public getAsync<T>(...params: VariadicBindParams): Promise<T | null>;
|
|
79
85
|
public async getAsync<T>(...params: unknown[]): Promise<T | null> {
|
|
80
86
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
81
87
|
const columnNames = await this.getColumnNamesAsync();
|
|
@@ -87,11 +93,13 @@ export class Statement {
|
|
|
87
93
|
|
|
88
94
|
/**
|
|
89
95
|
* Get all rows from the prepared statement.
|
|
90
|
-
*
|
|
91
|
-
* @param params @see `BindParams`
|
|
96
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
92
97
|
*/
|
|
93
|
-
public allAsync<T>(...params: VariadicBindParams): Promise<T[]>;
|
|
94
98
|
public allAsync<T>(params: BindParams): Promise<T[]>;
|
|
99
|
+
/**
|
|
100
|
+
* @hidden
|
|
101
|
+
*/
|
|
102
|
+
public allAsync<T>(...params: VariadicBindParams): Promise<T[]>;
|
|
95
103
|
public async allAsync<T>(...params: unknown[]): Promise<T[]> {
|
|
96
104
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
97
105
|
const columnNames = await this.getColumnNamesAsync();
|
|
@@ -129,13 +137,14 @@ export class Statement {
|
|
|
129
137
|
|
|
130
138
|
/**
|
|
131
139
|
* Run the prepared statement and return the result.
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* @param params @see `BindParams`
|
|
140
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
141
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
136
142
|
*/
|
|
137
|
-
public runSync(...params: VariadicBindParams): RunResult;
|
|
138
143
|
public runSync(params: BindParams): RunResult;
|
|
144
|
+
/**
|
|
145
|
+
* @hidden
|
|
146
|
+
*/
|
|
147
|
+
public runSync(...params: VariadicBindParams): RunResult;
|
|
139
148
|
public runSync(...params: unknown[]): RunResult {
|
|
140
149
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
141
150
|
if (shouldPassAsObject) {
|
|
@@ -147,21 +156,14 @@ export class Statement {
|
|
|
147
156
|
|
|
148
157
|
/**
|
|
149
158
|
* Iterate the prepared statement and return results as an iterable.
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* @param params @see `BindParams`
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```ts
|
|
157
|
-
* const statement = await db.prepareSync('SELECT * FROM test');
|
|
158
|
-
* for (const row of statement.eachSync<any>()) {
|
|
159
|
-
* console.log(row);
|
|
160
|
-
* }
|
|
161
|
-
* ```
|
|
159
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
160
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
162
161
|
*/
|
|
163
|
-
public eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;
|
|
164
162
|
public eachSync<T>(params: BindParams): IterableIterator<T>;
|
|
163
|
+
/**
|
|
164
|
+
* @hidden
|
|
165
|
+
*/
|
|
166
|
+
public eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;
|
|
165
167
|
public *eachSync<T>(...params: unknown[]): IterableIterator<T> {
|
|
166
168
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
167
169
|
const func = shouldPassAsObject
|
|
@@ -180,13 +182,14 @@ export class Statement {
|
|
|
180
182
|
|
|
181
183
|
/**
|
|
182
184
|
* Get one row from the prepared statement.
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
* @param params @see `BindParams`
|
|
185
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
186
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
187
187
|
*/
|
|
188
|
-
public getSync<T>(...params: VariadicBindParams): T | null;
|
|
189
188
|
public getSync<T>(params: BindParams): T | null;
|
|
189
|
+
/**
|
|
190
|
+
* @hidden
|
|
191
|
+
*/
|
|
192
|
+
public getSync<T>(...params: VariadicBindParams): T | null;
|
|
190
193
|
public getSync<T>(...params: unknown[]): T | null {
|
|
191
194
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
192
195
|
const columnNames = this.getColumnNamesSync();
|
|
@@ -198,13 +201,14 @@ export class Statement {
|
|
|
198
201
|
|
|
199
202
|
/**
|
|
200
203
|
* Get all rows from the prepared statement.
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
* @param params @see `BindParams`
|
|
204
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.
|
|
205
|
+
* @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.
|
|
205
206
|
*/
|
|
206
|
-
public allSync<T>(...params: VariadicBindParams): T[];
|
|
207
207
|
public allSync<T>(params: BindParams): T[];
|
|
208
|
+
/**
|
|
209
|
+
* @hidden
|
|
210
|
+
*/
|
|
211
|
+
public allSync<T>(...params: VariadicBindParams): T[];
|
|
208
212
|
public allSync<T>(...params: unknown[]): T[] {
|
|
209
213
|
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
210
214
|
const columnNames = this.getColumnNamesSync();
|
package/src/next/hooks.tsx
CHANGED
|
@@ -38,10 +38,15 @@ export interface SQLiteProviderProps {
|
|
|
38
38
|
errorHandler?: (error: Error) => void;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Create a context for the SQLite database
|
|
43
|
+
*/
|
|
42
44
|
const SQLiteContext = createContext<Database | null>(null);
|
|
43
45
|
|
|
44
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Context.Provider component that provides a SQLite database to all children.
|
|
48
|
+
* All descendants of this component will be able to access the database using the [`useSQLiteContext`](#usesqlitecontext) hook.
|
|
49
|
+
*/
|
|
45
50
|
export function SQLiteProvider({
|
|
46
51
|
dbName,
|
|
47
52
|
options,
|
|
@@ -101,8 +106,28 @@ export function SQLiteProvider({
|
|
|
101
106
|
return <SQLiteContext.Provider value={databaseRef.current}>{children}</SQLiteContext.Provider>;
|
|
102
107
|
}
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
|
|
109
|
+
/**
|
|
110
|
+
* A global hook for accessing the SQLite database across components.
|
|
111
|
+
* This hook should only be used within a [`<SQLiteProvider>`](#sqliteprovider) component.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```tsx
|
|
115
|
+
* export default function App() {
|
|
116
|
+
* return (
|
|
117
|
+
* <SQLiteProvider dbName="test.db">
|
|
118
|
+
* <Main />
|
|
119
|
+
* </SQLiteProvider>
|
|
120
|
+
* );
|
|
121
|
+
* }
|
|
122
|
+
*
|
|
123
|
+
* export function Main() {
|
|
124
|
+
* const db = useSQLiteContext();
|
|
125
|
+
* console.log('sqlite version', db.getSync('SELECT sqlite_version()'));
|
|
126
|
+
* return <View />
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function useSQLiteContext(): Database {
|
|
106
131
|
const context = useContext(SQLiteContext);
|
|
107
132
|
if (context == null) {
|
|
108
133
|
throw new Error('useSQLiteContext must be used within a <SQLiteProvider>');
|
|
Binary file
|