expo-sqlite 11.8.0 → 12.1.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.
- package/CHANGELOG.md +34 -0
- package/android/CMakeLists.txt +2 -2
- package/android/build.gradle +52 -5
- package/android/src/main/cpp/NativeDatabaseBinding.cpp +142 -0
- package/android/src/main/cpp/NativeDatabaseBinding.h +75 -0
- package/android/src/main/cpp/NativeStatementBinding.cpp +157 -0
- package/android/src/main/cpp/NativeStatementBinding.h +83 -0
- package/android/src/main/cpp/SQLite3Main.cpp +7 -2
- package/android/src/main/cpp/SQLite3Wrapper.cpp +0 -30
- package/android/src/main/cpp/SQLite3Wrapper.h +0 -4
- package/android/src/main/java/expo/modules/sqlite/NativeDatabase.kt +11 -0
- package/android/src/main/java/expo/modules/sqlite/NativeDatabaseBinding.kt +79 -0
- package/android/src/main/java/expo/modules/sqlite/NativeStatement.kt +11 -0
- package/android/src/main/java/expo/modules/sqlite/NativeStatementBinding.kt +41 -0
- package/android/src/main/java/expo/modules/sqlite/SQLExceptions.kt +17 -6
- package/android/src/main/java/expo/modules/sqlite/SQLRecords.kt +16 -3
- package/android/src/main/java/expo/modules/sqlite/SQLite3Wrapper.kt +0 -30
- package/android/src/main/java/expo/modules/sqlite/SQLiteHelpers.kt +0 -79
- package/android/src/main/java/expo/modules/sqlite/SQLiteModule.kt +2 -55
- package/android/src/main/java/expo/modules/sqlite/SQLiteModuleNext.kt +454 -0
- package/android/src/main/java/expo/modules/sqlite/SQLiteOptions.kt +20 -0
- package/build/SQLite.d.ts +2 -2
- package/build/SQLite.d.ts.map +1 -1
- package/build/SQLite.js.map +1 -1
- package/build/SQLite.types.d.ts +3 -2
- package/build/SQLite.types.d.ts.map +1 -1
- package/build/SQLite.types.js.map +1 -1
- package/build/next/Database.d.ts +254 -0
- package/build/next/Database.d.ts.map +1 -0
- package/build/next/Database.js +320 -0
- package/build/next/Database.js.map +1 -0
- package/build/next/ExpoSQLiteNext.d.ts +12 -0
- package/build/next/ExpoSQLiteNext.d.ts.map +1 -0
- package/build/next/ExpoSQLiteNext.js +26 -0
- package/build/next/ExpoSQLiteNext.js.map +1 -0
- package/build/next/ExpoSQLiteNext.native.d.ts +3 -0
- package/build/next/ExpoSQLiteNext.native.d.ts.map +1 -0
- package/build/next/ExpoSQLiteNext.native.js +3 -0
- package/build/next/ExpoSQLiteNext.native.js.map +1 -0
- package/build/next/NativeDatabase.d.ts +44 -0
- package/build/next/NativeDatabase.d.ts.map +1 -0
- package/build/next/NativeDatabase.js +2 -0
- package/build/next/NativeDatabase.js.map +1 -0
- package/build/next/NativeStatement.d.ts +71 -0
- package/build/next/NativeStatement.d.ts.map +1 -0
- package/build/next/NativeStatement.js +2 -0
- package/build/next/NativeStatement.js.map +1 -0
- package/build/next/Statement.d.ts +138 -0
- package/build/next/Statement.d.ts.map +1 -0
- package/build/next/Statement.js +184 -0
- package/build/next/Statement.js.map +1 -0
- package/build/next/hooks.d.ts +35 -0
- package/build/next/hooks.d.ts.map +1 -0
- package/build/next/hooks.js +60 -0
- package/build/next/hooks.js.map +1 -0
- package/build/next/index.d.ts +4 -0
- package/build/next/index.d.ts.map +1 -0
- package/build/next/index.js +4 -0
- package/build/next/index.js.map +1 -0
- package/expo-module.config.json +2 -2
- package/ios/CRSQLiteLoader.h +3 -1
- package/ios/CRSQLiteLoader.m +11 -6
- package/ios/Exceptions.swift +13 -0
- package/ios/ExpoSQLite.podspec +2 -2
- package/ios/NativeDatabase.swift +26 -0
- package/ios/NativeStatement.swift +13 -0
- package/ios/SQLAction.swift +23 -0
- package/ios/SQLiteModule.swift +0 -53
- package/ios/SQLiteModuleNext.swift +555 -0
- package/ios/SQLiteOptions.swift +26 -0
- package/next.d.ts +1 -0
- package/next.js +1 -0
- package/package.json +4 -2
- package/src/SQLite.ts +2 -1
- package/src/SQLite.types.ts +5 -2
- package/src/next/Database.ts +442 -0
- package/src/next/ExpoSQLiteNext.native.ts +2 -0
- package/src/next/ExpoSQLiteNext.ts +34 -0
- package/src/next/NativeDatabase.ts +58 -0
- package/src/next/NativeStatement.ts +96 -0
- package/src/next/Statement.ts +306 -0
- package/src/next/hooks.tsx +111 -0
- package/src/next/index.ts +3 -0
- package/vendor/README.expo.md +0 -6
- package/vendor/shell.c +0 -28032
- package/vendor/sqlite3.c +0 -247629
- package/vendor/sqlite3.h +0 -13068
- package/vendor/sqlite3ext.h +0 -709
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoSQLiteNext.native.d.ts","sourceRoot":"","sources":["../../src/next/ExpoSQLiteNext.native.ts"],"names":[],"mappings":";AACA,wBAAqD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoSQLiteNext.native.js","sourceRoot":"","sources":["../../src/next/ExpoSQLiteNext.native.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,eAAe,mBAAmB,CAAC,gBAAgB,CAAC,CAAC","sourcesContent":["import { requireNativeModule } from 'expo-modules-core';\nexport default requireNativeModule('ExpoSQLiteNext');\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { NativeStatement } from './NativeStatement';
|
|
2
|
+
/**
|
|
3
|
+
* A class that represents an instance of the SQLite database.
|
|
4
|
+
*/
|
|
5
|
+
export declare class NativeDatabase {
|
|
6
|
+
constructor(dbName: string, options?: OpenOptions);
|
|
7
|
+
initAsync(): Promise<void>;
|
|
8
|
+
isInTransactionAsync(): Promise<boolean>;
|
|
9
|
+
closeAsync(): Promise<void>;
|
|
10
|
+
execAsync(source: string): Promise<void>;
|
|
11
|
+
prepareAsync(nativeStatement: NativeStatement, source: string): Promise<NativeStatement>;
|
|
12
|
+
initSync(): void;
|
|
13
|
+
isInTransactionSync(): boolean;
|
|
14
|
+
closeSync(): void;
|
|
15
|
+
execSync(source: string): void;
|
|
16
|
+
prepareSync(nativeStatement: NativeStatement, source: string): NativeStatement;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Options for opening a database.
|
|
20
|
+
*/
|
|
21
|
+
export interface OpenOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Whether to enable the CR-SQLite extension.
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
enableCRSQLite?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Whether to call the `sqlite3_update_hook` function and enable the `onDatabaseChange` events.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
enableChangeListener?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether to create new connection even if connection with the same database name exists in cache.
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
useNewConnection?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Finalized unclosed statements automatically when the database is closed.
|
|
39
|
+
* @default true
|
|
40
|
+
* @hidden
|
|
41
|
+
*/
|
|
42
|
+
finalizeUnusedStatementsBeforeClosing?: boolean;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=NativeDatabase.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeDatabase.d.ts","sourceRoot":"","sources":["../../src/next/NativeDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;gBACrB,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW;IAI1C,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAC1B,oBAAoB,IAAI,OAAO,CAAC,OAAO,CAAC;IACxC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAC3B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IACxC,YAAY,CAAC,eAAe,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAMxF,QAAQ,IAAI,IAAI;IAChB,mBAAmB,IAAI,OAAO;IAC9B,SAAS,IAAI,IAAI;IACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAC9B,WAAW,CAAC,eAAe,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe;CAGtF;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAE/B;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;;;OAIG;IACH,qCAAqC,CAAC,EAAE,OAAO,CAAC;CACjD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeDatabase.js","sourceRoot":"","sources":["../../src/next/NativeDatabase.ts"],"names":[],"mappings":"","sourcesContent":["import { NativeStatement } from './NativeStatement';\n\n/**\n * A class that represents an instance of the SQLite database.\n */\nexport declare class NativeDatabase {\n constructor(dbName: string, options?: OpenOptions);\n\n //#region Asynchronous API\n\n public initAsync(): Promise<void>;\n public isInTransactionAsync(): Promise<boolean>;\n public closeAsync(): Promise<void>;\n public execAsync(source: string): Promise<void>;\n public prepareAsync(nativeStatement: NativeStatement, source: string): Promise<NativeStatement>;\n\n //#endregion\n\n //#region Synchronous API\n\n public initSync(): void;\n public isInTransactionSync(): boolean;\n public closeSync(): void;\n public execSync(source: string): void;\n public prepareSync(nativeStatement: NativeStatement, source: string): NativeStatement;\n\n //#endregion\n}\n\n/**\n * Options for opening a database.\n */\nexport interface OpenOptions {\n /**\n * Whether to enable the CR-SQLite extension.\n * @default false\n */\n enableCRSQLite?: boolean;\n\n /**\n * Whether to call the `sqlite3_update_hook` function and enable the `onDatabaseChange` events.\n * @default false\n */\n enableChangeListener?: boolean;\n\n /**\n * Whether to create new connection even if connection with the same database name exists in cache.\n * @default false\n */\n useNewConnection?: boolean;\n\n /**\n * Finalized unclosed statements automatically when the database is closed.\n * @default true\n * @hidden\n */\n finalizeUnusedStatementsBeforeClosing?: boolean;\n}\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result of a `runAsync` call.
|
|
3
|
+
*/
|
|
4
|
+
export interface RunResult {
|
|
5
|
+
/**
|
|
6
|
+
* The last inserted row ID.
|
|
7
|
+
*/
|
|
8
|
+
lastInsertRowid: number;
|
|
9
|
+
/**
|
|
10
|
+
* The number of rows affected.
|
|
11
|
+
*/
|
|
12
|
+
changes: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Bind parameters to the prepared statement.
|
|
16
|
+
* You can either pass the parameters in the following forms:
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* - Variadic arguments for unnamed parameters.
|
|
20
|
+
* ```ts
|
|
21
|
+
* const statement = await db.prepareAsync('SELECT * FROM test WHERE value = ? AND intValue = ?');
|
|
22
|
+
* await statement.getAsync('test1', 789);
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* - A single array for unnamed parameters.
|
|
27
|
+
* ```ts
|
|
28
|
+
* const statement = await db.prepareAsync('SELECT * FROM test WHERE value = ? AND intValue = ?');
|
|
29
|
+
* await statement.getAsync(['test1', 789]);
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* - A single object for [named parameters](https://www.sqlite.org/lang_expr.html)
|
|
34
|
+
*
|
|
35
|
+
* Through we support multiple named parameter forms like `:VVV`, `@VVV`, and `$VVV`. We recommend using `$VVV` because JavaScript allows using `$` in identifiers without escaping.
|
|
36
|
+
* ```ts
|
|
37
|
+
* const statement = await db.prepareAsync('SELECT * FROM test WHERE value = $value AND intValue = $intValue');
|
|
38
|
+
* await statement.getAsync({ $value: 'test1', $intValue: 789 });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export type BindValue = string | number | null | boolean;
|
|
42
|
+
export type BindParams = Record<string, BindValue> | BindValue[];
|
|
43
|
+
export type VariadicBindParams = BindValue[];
|
|
44
|
+
export type ColumnNames = string[];
|
|
45
|
+
export type ColumnValues = any[];
|
|
46
|
+
type AnyDatabase = any;
|
|
47
|
+
/**
|
|
48
|
+
* A class that represents an instance of the SQLite statement.
|
|
49
|
+
*/
|
|
50
|
+
export declare class NativeStatement {
|
|
51
|
+
arrayRunAsync(database: AnyDatabase, params: BindParams): Promise<RunResult>;
|
|
52
|
+
objectRunAsync(database: AnyDatabase, params: BindParams): Promise<RunResult>;
|
|
53
|
+
arrayGetAsync(database: AnyDatabase, params: BindParams): Promise<ColumnValues | null | undefined>;
|
|
54
|
+
objectGetAsync(database: AnyDatabase, params: BindParams): Promise<ColumnValues | null | undefined>;
|
|
55
|
+
arrayGetAllAsync(database: AnyDatabase, params: BindParams): Promise<ColumnValues[]>;
|
|
56
|
+
objectGetAllAsync(database: AnyDatabase, params: BindParams): Promise<ColumnValues[]>;
|
|
57
|
+
getColumnNamesAsync(): Promise<ColumnNames>;
|
|
58
|
+
resetAsync(database: AnyDatabase): Promise<void>;
|
|
59
|
+
finalizeAsync(database: AnyDatabase): Promise<void>;
|
|
60
|
+
arrayRunSync(database: AnyDatabase, params: BindParams): RunResult;
|
|
61
|
+
objectRunSync(database: AnyDatabase, params: BindParams): RunResult;
|
|
62
|
+
arrayGetSync(database: AnyDatabase, params: BindParams): ColumnValues | null | undefined;
|
|
63
|
+
objectGetSync(database: AnyDatabase, params: BindParams): ColumnValues | null | undefined;
|
|
64
|
+
arrayGetAllSync(database: AnyDatabase, params: BindParams): ColumnValues[];
|
|
65
|
+
objectGetAllSync(database: AnyDatabase, params: BindParams): ColumnValues[];
|
|
66
|
+
getColumnNamesSync(): string[];
|
|
67
|
+
resetSync(database: AnyDatabase): void;
|
|
68
|
+
finalizeSync(database: AnyDatabase): void;
|
|
69
|
+
}
|
|
70
|
+
export {};
|
|
71
|
+
//# sourceMappingURL=N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AC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;IAErF,mBAAmB,IAAI,OAAO,CAAC,WAAW,CAAC;IAE3C,UAAU,CAAC,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAChD,aAAa,CAAC,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAMnD,YAAY,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,SAAS;IAClE,aAAa,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,SAAS;IAEnE,YAAY,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,GAAG,IAAI,GAAG,SAAS;IACxF,aAAa,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,GAAG,IAAI,GAAG,SAAS;IAEzF,eAAe,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,EAAE;IAC1E,gBAAgB,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,EAAE;IAE3E,kBAAkB,IAAI,MAAM,EAAE;IAE9B,SAAS,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI;IACtC,YAAY,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI;CAGjD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeStatement.js","sourceRoot":"","sources":["../../src/next/NativeStatement.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Result of a `runAsync` call.\n */\nexport interface RunResult {\n /**\n * The last inserted row ID.\n */\n lastInsertRowid: number;\n\n /**\n * The number of rows affected.\n */\n changes: number;\n}\n\n/**\n * Bind parameters to the prepared statement.\n * You can either pass the parameters in the following forms:\n *\n * @example\n * - Variadic arguments for unnamed parameters.\n * ```ts\n * const statement = await db.prepareAsync('SELECT * FROM test WHERE value = ? AND intValue = ?');\n * await statement.getAsync('test1', 789);\n * ```\n *\n * @example\n * - A single array for unnamed parameters.\n * ```ts\n * const statement = await db.prepareAsync('SELECT * FROM test WHERE value = ? AND intValue = ?');\n * await statement.getAsync(['test1', 789]);\n * ```\n *\n * @example\n * - A single object for [named parameters](https://www.sqlite.org/lang_expr.html)\n *\n * Through we support multiple named parameter forms like `:VVV`, `@VVV`, and `$VVV`. We recommend using `$VVV` because JavaScript allows using `$` in identifiers without escaping.\n * ```ts\n * const statement = await db.prepareAsync('SELECT * FROM test WHERE value = $value AND intValue = $intValue');\n * await statement.getAsync({ $value: 'test1', $intValue: 789 });\n * ```\n */\nexport type BindValue = string | number | null | boolean;\nexport type BindParams = Record<string, BindValue> | BindValue[];\nexport type VariadicBindParams = BindValue[];\n\nexport type ColumnNames = string[];\nexport type ColumnValues = any[];\ntype AnyDatabase = any;\n\n/**\n * A class that represents an instance of the SQLite statement.\n */\nexport declare class NativeStatement {\n //#region Asynchronous API\n\n public arrayRunAsync(database: AnyDatabase, params: BindParams): Promise<RunResult>;\n public objectRunAsync(database: AnyDatabase, params: BindParams): Promise<RunResult>;\n\n public arrayGetAsync(\n database: AnyDatabase,\n params: BindParams\n ): Promise<ColumnValues | null | undefined>;\n public objectGetAsync(\n database: AnyDatabase,\n params: BindParams\n ): Promise<ColumnValues | null | undefined>;\n\n public arrayGetAllAsync(database: AnyDatabase, params: BindParams): Promise<ColumnValues[]>;\n public objectGetAllAsync(database: AnyDatabase, params: BindParams): Promise<ColumnValues[]>;\n\n public getColumnNamesAsync(): Promise<ColumnNames>;\n\n public resetAsync(database: AnyDatabase): Promise<void>;\n public finalizeAsync(database: AnyDatabase): Promise<void>;\n\n //#endregion\n\n //#region Synchronous API\n\n public arrayRunSync(database: AnyDatabase, params: BindParams): RunResult;\n public objectRunSync(database: AnyDatabase, params: BindParams): RunResult;\n\n public arrayGetSync(database: AnyDatabase, params: BindParams): ColumnValues | null | undefined;\n public objectGetSync(database: AnyDatabase, params: BindParams): ColumnValues | null | undefined;\n\n public arrayGetAllSync(database: AnyDatabase, params: BindParams): ColumnValues[];\n public objectGetAllSync(database: AnyDatabase, params: BindParams): ColumnValues[];\n\n public getColumnNamesSync(): string[];\n\n public resetSync(database: AnyDatabase): void;\n public finalizeSync(database: AnyDatabase): void;\n\n //#endregion\n}\n"]}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { NativeDatabase } from './NativeDatabase';
|
|
2
|
+
import { BindParams, BindValue, NativeStatement, RunResult, VariadicBindParams, type ColumnNames, type ColumnValues } from './NativeStatement';
|
|
3
|
+
export { BindParams, BindValue, RunResult, VariadicBindParams };
|
|
4
|
+
/**
|
|
5
|
+
* A prepared statement returned by `Database.prepareAsync()` that can be binded with parameters and executed.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Statement {
|
|
8
|
+
private readonly nativeDatabase;
|
|
9
|
+
private readonly nativeStatement;
|
|
10
|
+
constructor(nativeDatabase: NativeDatabase, nativeStatement: NativeStatement);
|
|
11
|
+
/**
|
|
12
|
+
* Run the prepared statement and return the result.
|
|
13
|
+
*
|
|
14
|
+
* @param params @see `BindParams`
|
|
15
|
+
*/
|
|
16
|
+
runAsync(...params: VariadicBindParams): Promise<RunResult>;
|
|
17
|
+
runAsync(params: BindParams): Promise<RunResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Iterate the prepared statement and return results as an async iterable.
|
|
20
|
+
*
|
|
21
|
+
* @param params @see `BindParams`
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const statement = await db.prepareAsync('SELECT * FROM test');
|
|
26
|
+
* for await (const row of statement.eachAsync<any>()) {
|
|
27
|
+
* console.log(row);
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;
|
|
32
|
+
eachAsync<T>(params: BindParams): AsyncIterableIterator<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Get one row from the prepared statement.
|
|
35
|
+
*
|
|
36
|
+
* @param params @see `BindParams`
|
|
37
|
+
*/
|
|
38
|
+
getAsync<T>(...params: VariadicBindParams): Promise<T | null>;
|
|
39
|
+
getAsync<T>(params: BindParams): Promise<T | null>;
|
|
40
|
+
/**
|
|
41
|
+
* Get all rows from the prepared statement.
|
|
42
|
+
*
|
|
43
|
+
* @param params @see `BindParams`
|
|
44
|
+
*/
|
|
45
|
+
allAsync<T>(...params: VariadicBindParams): Promise<T[]>;
|
|
46
|
+
allAsync<T>(params: BindParams): Promise<T[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Get the column names of the prepared statement.
|
|
49
|
+
*/
|
|
50
|
+
getColumnNamesAsync(): Promise<string[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Reset the prepared statement cursor.
|
|
53
|
+
*/
|
|
54
|
+
resetAsync(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Finalize the prepared statement.
|
|
57
|
+
* > **Note:** Remember to finalize the prepared statement whenever you call `prepareAsync()` to avoid resource leaks.
|
|
58
|
+
*/
|
|
59
|
+
finalizeAsync(): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Run the prepared statement and return the result.
|
|
62
|
+
*
|
|
63
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
64
|
+
*
|
|
65
|
+
* @param params @see `BindParams`
|
|
66
|
+
*/
|
|
67
|
+
runSync(...params: VariadicBindParams): RunResult;
|
|
68
|
+
runSync(params: BindParams): RunResult;
|
|
69
|
+
/**
|
|
70
|
+
* Iterate the prepared statement and return results as an iterable.
|
|
71
|
+
*
|
|
72
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
73
|
+
*
|
|
74
|
+
* @param params @see `BindParams`
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const statement = await db.prepareSync('SELECT * FROM test');
|
|
79
|
+
* for (const row of statement.eachSync<any>()) {
|
|
80
|
+
* console.log(row);
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;
|
|
85
|
+
eachSync<T>(params: BindParams): IterableIterator<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Get one row from the prepared statement.
|
|
88
|
+
*
|
|
89
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
90
|
+
*
|
|
91
|
+
* @param params @see `BindParams`
|
|
92
|
+
*/
|
|
93
|
+
getSync<T>(...params: VariadicBindParams): T | null;
|
|
94
|
+
getSync<T>(params: BindParams): T | null;
|
|
95
|
+
/**
|
|
96
|
+
* Get all rows from the prepared statement.
|
|
97
|
+
*
|
|
98
|
+
* > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.
|
|
99
|
+
*
|
|
100
|
+
* @param params @see `BindParams`
|
|
101
|
+
*/
|
|
102
|
+
allSync<T>(...params: VariadicBindParams): T[];
|
|
103
|
+
allSync<T>(params: BindParams): T[];
|
|
104
|
+
/**
|
|
105
|
+
* Get the column names of the prepared statement.
|
|
106
|
+
*/
|
|
107
|
+
getColumnNamesSync(): string[];
|
|
108
|
+
/**
|
|
109
|
+
* Reset the prepared statement cursor.
|
|
110
|
+
*/
|
|
111
|
+
resetSync(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Finalize the prepared statement.
|
|
114
|
+
*
|
|
115
|
+
* > **Note:** Remember to finalize the prepared statement whenever you call `prepareSync()` to avoid resource leaks.
|
|
116
|
+
*
|
|
117
|
+
*/
|
|
118
|
+
finalizeSync(): void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Normalize the bind params to an array or object.
|
|
122
|
+
* @hidden
|
|
123
|
+
*/
|
|
124
|
+
export declare function normalizeParams(...params: any[]): {
|
|
125
|
+
params: BindParams;
|
|
126
|
+
shouldPassAsObject: boolean;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Compose `columnNames` and `columnValues` to an row object.
|
|
130
|
+
* @hidden
|
|
131
|
+
*/
|
|
132
|
+
export declare function composeRow<T>(columnNames: ColumnNames, columnValues: ColumnValues): T;
|
|
133
|
+
/**
|
|
134
|
+
* Compose `columnNames` and `columnValuesList` to an array of row objects.
|
|
135
|
+
* @hidden
|
|
136
|
+
*/
|
|
137
|
+
export declare function composeRows<T>(columnNames: ColumnNames, columnValuesList: ColumnValues[]): T[];
|
|
138
|
+
//# sourceMappingURL=Statement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Statement.d.ts","sourceRoot":"","sources":["../../src/next/Statement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EACL,UAAU,EACV,SAAS,EACT,eAAe,EACf,SAAS,EACT,kBAAkB,EAClB,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,kBAAkB,EAAE,CAAC;AAEhE;;GAEG;AACH,qBAAa,SAAS;IAElB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,eAAe;gBADf,cAAc,EAAE,cAAc,EAC9B,eAAe,EAAE,eAAe;IAKnD;;;;OAIG;IACI,QAAQ,CAAC,GAAG,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,SAAS,CAAC;IAC3D,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;IAUvD;;;;;;;;;;;;OAYG;IACI,SAAS,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,kBAAkB,GAAG,qBAAqB,CAAC,CAAC,CAAC;IACrE,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,UAA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AE,MAAM,EAAE,UAAU,GAAG,CAAC,EAAE;IAU1C;;OAEG;IACI,kBAAkB,IAAI,MAAM,EAAE;IAIrC;;OAEG;IACI,SAAS,IAAI,IAAI;IAIxB;;;;;OAKG;IACI,YAAY,IAAI,IAAI;CAK5B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,GAAG;IACjD,MAAM,EAAE,UAAU,CAAC;IACnB,kBAAkB,EAAE,OAAO,CAAC;CAC7B,CAaA;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,GAAG,CAAC,CAWrF;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,WAAW,EAAE,WAAW,EAAE,gBAAgB,EAAE,YAAY,EAAE,GAAG,CAAC,EAAE,CAmB9F"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A prepared statement returned by `Database.prepareAsync()` that can be binded with parameters and executed.
|
|
3
|
+
*/
|
|
4
|
+
export class Statement {
|
|
5
|
+
nativeDatabase;
|
|
6
|
+
nativeStatement;
|
|
7
|
+
constructor(nativeDatabase, nativeStatement) {
|
|
8
|
+
this.nativeDatabase = nativeDatabase;
|
|
9
|
+
this.nativeStatement = nativeStatement;
|
|
10
|
+
}
|
|
11
|
+
async runAsync(...params) {
|
|
12
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
13
|
+
if (shouldPassAsObject) {
|
|
14
|
+
return await this.nativeStatement.objectRunAsync(this.nativeDatabase, bindParams);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return await this.nativeStatement.arrayRunAsync(this.nativeDatabase, bindParams);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async *eachAsync(...params) {
|
|
21
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
22
|
+
const func = shouldPassAsObject
|
|
23
|
+
? this.nativeStatement.objectGetAsync.bind(this.nativeStatement)
|
|
24
|
+
: this.nativeStatement.arrayGetAsync.bind(this.nativeStatement);
|
|
25
|
+
const columnNames = await this.getColumnNamesAsync();
|
|
26
|
+
let result = null;
|
|
27
|
+
do {
|
|
28
|
+
result = await func(this.nativeDatabase, bindParams);
|
|
29
|
+
if (result != null) {
|
|
30
|
+
yield composeRow(columnNames, result);
|
|
31
|
+
}
|
|
32
|
+
} while (result != null);
|
|
33
|
+
}
|
|
34
|
+
async getAsync(...params) {
|
|
35
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
36
|
+
const columnNames = await this.getColumnNamesAsync();
|
|
37
|
+
const columnValues = shouldPassAsObject
|
|
38
|
+
? await this.nativeStatement.objectGetAsync(this.nativeDatabase, bindParams)
|
|
39
|
+
: await this.nativeStatement.arrayGetAsync(this.nativeDatabase, bindParams);
|
|
40
|
+
return columnValues != null ? composeRow(columnNames, columnValues) : null;
|
|
41
|
+
}
|
|
42
|
+
async allAsync(...params) {
|
|
43
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
44
|
+
const columnNames = await this.getColumnNamesAsync();
|
|
45
|
+
const columnValuesList = shouldPassAsObject
|
|
46
|
+
? await this.nativeStatement.objectGetAllAsync(this.nativeDatabase, bindParams)
|
|
47
|
+
: await this.nativeStatement.arrayGetAllAsync(this.nativeDatabase, bindParams);
|
|
48
|
+
return composeRows(columnNames, columnValuesList);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the column names of the prepared statement.
|
|
52
|
+
*/
|
|
53
|
+
getColumnNamesAsync() {
|
|
54
|
+
return this.nativeStatement.getColumnNamesAsync();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reset the prepared statement cursor.
|
|
58
|
+
*/
|
|
59
|
+
async resetAsync() {
|
|
60
|
+
await this.nativeStatement.resetAsync(this.nativeDatabase);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Finalize the prepared statement.
|
|
64
|
+
* > **Note:** Remember to finalize the prepared statement whenever you call `prepareAsync()` to avoid resource leaks.
|
|
65
|
+
*/
|
|
66
|
+
async finalizeAsync() {
|
|
67
|
+
await this.nativeStatement.finalizeAsync(this.nativeDatabase);
|
|
68
|
+
}
|
|
69
|
+
runSync(...params) {
|
|
70
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
71
|
+
if (shouldPassAsObject) {
|
|
72
|
+
return this.nativeStatement.objectRunSync(this.nativeDatabase, bindParams);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
return this.nativeStatement.arrayRunSync(this.nativeDatabase, bindParams);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
*eachSync(...params) {
|
|
79
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
80
|
+
const func = shouldPassAsObject
|
|
81
|
+
? this.nativeStatement.objectGetSync.bind(this.nativeStatement)
|
|
82
|
+
: this.nativeStatement.arrayGetSync.bind(this.nativeStatement);
|
|
83
|
+
const columnNames = this.getColumnNamesSync();
|
|
84
|
+
let result = null;
|
|
85
|
+
do {
|
|
86
|
+
result = func(this.nativeDatabase, bindParams);
|
|
87
|
+
if (result != null) {
|
|
88
|
+
yield composeRow(columnNames, result);
|
|
89
|
+
}
|
|
90
|
+
} while (result != null);
|
|
91
|
+
}
|
|
92
|
+
getSync(...params) {
|
|
93
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
94
|
+
const columnNames = this.getColumnNamesSync();
|
|
95
|
+
const columnValues = shouldPassAsObject
|
|
96
|
+
? this.nativeStatement.objectGetSync(this.nativeDatabase, bindParams)
|
|
97
|
+
: this.nativeStatement.arrayGetSync(this.nativeDatabase, bindParams);
|
|
98
|
+
return columnValues != null ? composeRow(columnNames, columnValues) : null;
|
|
99
|
+
}
|
|
100
|
+
allSync(...params) {
|
|
101
|
+
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
102
|
+
const columnNames = this.getColumnNamesSync();
|
|
103
|
+
const columnValuesList = shouldPassAsObject
|
|
104
|
+
? this.nativeStatement.objectGetAllSync(this.nativeDatabase, bindParams)
|
|
105
|
+
: this.nativeStatement.arrayGetAllSync(this.nativeDatabase, bindParams);
|
|
106
|
+
return composeRows(columnNames, columnValuesList);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Get the column names of the prepared statement.
|
|
110
|
+
*/
|
|
111
|
+
getColumnNamesSync() {
|
|
112
|
+
return this.nativeStatement.getColumnNamesSync();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Reset the prepared statement cursor.
|
|
116
|
+
*/
|
|
117
|
+
resetSync() {
|
|
118
|
+
this.nativeStatement.resetSync(this.nativeDatabase);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Finalize the prepared statement.
|
|
122
|
+
*
|
|
123
|
+
* > **Note:** Remember to finalize the prepared statement whenever you call `prepareSync()` to avoid resource leaks.
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
finalizeSync() {
|
|
127
|
+
this.nativeStatement.finalizeSync(this.nativeDatabase);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Normalize the bind params to an array or object.
|
|
132
|
+
* @hidden
|
|
133
|
+
*/
|
|
134
|
+
export function normalizeParams(...params) {
|
|
135
|
+
let bindParams = params.length > 1 ? params : params[0];
|
|
136
|
+
if (bindParams == null) {
|
|
137
|
+
bindParams = [];
|
|
138
|
+
}
|
|
139
|
+
if (typeof bindParams !== 'object') {
|
|
140
|
+
bindParams = [bindParams];
|
|
141
|
+
}
|
|
142
|
+
const shouldPassAsObject = !Array.isArray(bindParams);
|
|
143
|
+
return {
|
|
144
|
+
params: bindParams,
|
|
145
|
+
shouldPassAsObject,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Compose `columnNames` and `columnValues` to an row object.
|
|
150
|
+
* @hidden
|
|
151
|
+
*/
|
|
152
|
+
export function composeRow(columnNames, columnValues) {
|
|
153
|
+
const row = {};
|
|
154
|
+
if (columnNames.length !== columnValues.length) {
|
|
155
|
+
throw new Error(`Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValues.length}`);
|
|
156
|
+
}
|
|
157
|
+
for (let i = 0; i < columnNames.length; i++) {
|
|
158
|
+
row[columnNames[i]] = columnValues[i];
|
|
159
|
+
}
|
|
160
|
+
return row;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Compose `columnNames` and `columnValuesList` to an array of row objects.
|
|
164
|
+
* @hidden
|
|
165
|
+
*/
|
|
166
|
+
export function composeRows(columnNames, columnValuesList) {
|
|
167
|
+
if (columnValuesList.length === 0) {
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
if (columnNames.length !== columnValuesList[0].length) {
|
|
171
|
+
// We only check the first row because SQLite returns the same column count for all rows.
|
|
172
|
+
throw new Error(`Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValuesList[0].length}`);
|
|
173
|
+
}
|
|
174
|
+
const results = [];
|
|
175
|
+
for (const columnValues of columnValuesList) {
|
|
176
|
+
const row = {};
|
|
177
|
+
for (let i = 0; i < columnNames.length; i++) {
|
|
178
|
+
row[columnNames[i]] = columnValues[i];
|
|
179
|
+
}
|
|
180
|
+
results.push(row);
|
|
181
|
+
}
|
|
182
|
+
return results;
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=Statement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Statement.js","sourceRoot":"","sources":["../../src/next/Statement.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,MAAM,OAAO,SAAS;IAED;IACA;IAFnB,YACmB,cAA8B,EAC9B,eAAgC;QADhC,mBAAc,GAAd,cAAc,CAAgB;QAC9B,oBAAe,GAAf,eAAe,CAAiB;IAChD,CAAC;IAWG,KAA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AW,CAAC,MAAM,aAAa,gBAAgB,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAC9G,CAAC;KACH;IACD,MAAM,OAAO,GAAQ,EAAE,CAAC;IACxB,KAAK,MAAM,YAAY,IAAI,gBAAgB,EAAE;QAC3C,MAAM,GAAG,GAAG,EAAE,CAAC;QACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3C,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;SACvC;QACD,OAAO,CAAC,IAAI,CAAC,GAAQ,CAAC,CAAC;KACxB;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { NativeDatabase } from './NativeDatabase';\nimport {\n BindParams,\n BindValue,\n NativeStatement,\n RunResult,\n VariadicBindParams,\n type ColumnNames,\n type ColumnValues,\n} from './NativeStatement';\n\nexport { BindParams, BindValue, RunResult, VariadicBindParams };\n\n/**\n * A prepared statement returned by `Database.prepareAsync()` that can be binded with parameters and executed.\n */\nexport class Statement {\n constructor(\n private readonly nativeDatabase: NativeDatabase,\n private readonly nativeStatement: NativeStatement\n ) {}\n\n //#region Asynchronous API\n\n /**\n * Run the prepared statement and return the result.\n *\n * @param params @see `BindParams`\n */\n public runAsync(...params: VariadicBindParams): Promise<RunResult>;\n public runAsync(params: BindParams): Promise<RunResult>;\n public async runAsync(...params: unknown[]): Promise<RunResult> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n if (shouldPassAsObject) {\n return await this.nativeStatement.objectRunAsync(this.nativeDatabase, bindParams);\n } else {\n return await this.nativeStatement.arrayRunAsync(this.nativeDatabase, bindParams);\n }\n }\n\n /**\n * Iterate the prepared statement and return results as an async iterable.\n *\n * @param params @see `BindParams`\n *\n * @example\n * ```ts\n * const statement = await db.prepareAsync('SELECT * FROM test');\n * for await (const row of statement.eachAsync<any>()) {\n * console.log(row);\n * }\n * ```\n */\n public eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;\n public eachAsync<T>(params: BindParams): AsyncIterableIterator<T>;\n public async *eachAsync<T>(...params: unknown[]): AsyncIterableIterator<T> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const func = shouldPassAsObject\n ? this.nativeStatement.objectGetAsync.bind(this.nativeStatement)\n : this.nativeStatement.arrayGetAsync.bind(this.nativeStatement);\n\n const columnNames = await this.getColumnNamesAsync();\n let result = null;\n do {\n result = await func(this.nativeDatabase, bindParams);\n if (result != null) {\n yield composeRow<T>(columnNames, result);\n }\n } while (result != null);\n }\n\n /**\n * Get one row from the prepared statement.\n *\n * @param params @see `BindParams`\n */\n public getAsync<T>(...params: VariadicBindParams): Promise<T | null>;\n public getAsync<T>(params: BindParams): Promise<T | null>;\n public async getAsync<T>(...params: unknown[]): Promise<T | null> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = await this.getColumnNamesAsync();\n const columnValues = shouldPassAsObject\n ? await this.nativeStatement.objectGetAsync(this.nativeDatabase, bindParams)\n : await this.nativeStatement.arrayGetAsync(this.nativeDatabase, bindParams);\n return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;\n }\n\n /**\n * Get all rows from the prepared statement.\n *\n * @param params @see `BindParams`\n */\n public allAsync<T>(...params: VariadicBindParams): Promise<T[]>;\n public allAsync<T>(params: BindParams): Promise<T[]>;\n public async allAsync<T>(...params: unknown[]): Promise<T[]> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = await this.getColumnNamesAsync();\n const columnValuesList = shouldPassAsObject\n ? await this.nativeStatement.objectGetAllAsync(this.nativeDatabase, bindParams)\n : await this.nativeStatement.arrayGetAllAsync(this.nativeDatabase, bindParams);\n return composeRows<T>(columnNames, columnValuesList);\n }\n\n /**\n * Get the column names of the prepared statement.\n */\n public getColumnNamesAsync(): Promise<string[]> {\n return this.nativeStatement.getColumnNamesAsync();\n }\n\n /**\n * Reset the prepared statement cursor.\n */\n public async resetAsync(): Promise<void> {\n await this.nativeStatement.resetAsync(this.nativeDatabase);\n }\n\n /**\n * Finalize the prepared statement.\n * > **Note:** Remember to finalize the prepared statement whenever you call `prepareAsync()` to avoid resource leaks.\n */\n public async finalizeAsync(): Promise<void> {\n await this.nativeStatement.finalizeAsync(this.nativeDatabase);\n }\n\n //#endregion\n\n //#region Synchronous API\n\n /**\n * Run the prepared statement and return the result.\n *\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.\n *\n * @param params @see `BindParams`\n */\n public runSync(...params: VariadicBindParams): RunResult;\n public runSync(params: BindParams): RunResult;\n public runSync(...params: unknown[]): RunResult {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n if (shouldPassAsObject) {\n return this.nativeStatement.objectRunSync(this.nativeDatabase, bindParams);\n } else {\n return this.nativeStatement.arrayRunSync(this.nativeDatabase, bindParams);\n }\n }\n\n /**\n * Iterate the prepared statement and return results as an iterable.\n *\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.\n *\n * @param params @see `BindParams`\n *\n * @example\n * ```ts\n * const statement = await db.prepareSync('SELECT * FROM test');\n * for (const row of statement.eachSync<any>()) {\n * console.log(row);\n * }\n * ```\n */\n public eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;\n public eachSync<T>(params: BindParams): IterableIterator<T>;\n public *eachSync<T>(...params: unknown[]): IterableIterator<T> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const func = shouldPassAsObject\n ? this.nativeStatement.objectGetSync.bind(this.nativeStatement)\n : this.nativeStatement.arrayGetSync.bind(this.nativeStatement);\n\n const columnNames = this.getColumnNamesSync();\n let result = null;\n do {\n result = func(this.nativeDatabase, bindParams);\n if (result != null) {\n yield composeRow<T>(columnNames, result);\n }\n } while (result != null);\n }\n\n /**\n * Get one row from the prepared statement.\n *\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.\n *\n * @param params @see `BindParams`\n */\n public getSync<T>(...params: VariadicBindParams): T | null;\n public getSync<T>(params: BindParams): T | null;\n public getSync<T>(...params: unknown[]): T | null {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = this.getColumnNamesSync();\n const columnValues = shouldPassAsObject\n ? this.nativeStatement.objectGetSync(this.nativeDatabase, bindParams)\n : this.nativeStatement.arrayGetSync(this.nativeDatabase, bindParams);\n return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;\n }\n\n /**\n * Get all rows from the prepared statement.\n *\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread, affecting performance.\n *\n * @param params @see `BindParams`\n */\n public allSync<T>(...params: VariadicBindParams): T[];\n public allSync<T>(params: BindParams): T[];\n public allSync<T>(...params: unknown[]): T[] {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = this.getColumnNamesSync();\n const columnValuesList = shouldPassAsObject\n ? this.nativeStatement.objectGetAllSync(this.nativeDatabase, bindParams)\n : this.nativeStatement.arrayGetAllSync(this.nativeDatabase, bindParams);\n return composeRows<T>(columnNames, columnValuesList);\n }\n\n /**\n * Get the column names of the prepared statement.\n */\n public getColumnNamesSync(): string[] {\n return this.nativeStatement.getColumnNamesSync();\n }\n\n /**\n * Reset the prepared statement cursor.\n */\n public resetSync(): void {\n this.nativeStatement.resetSync(this.nativeDatabase);\n }\n\n /**\n * Finalize the prepared statement.\n *\n * > **Note:** Remember to finalize the prepared statement whenever you call `prepareSync()` to avoid resource leaks.\n *\n */\n public finalizeSync(): void {\n this.nativeStatement.finalizeSync(this.nativeDatabase);\n }\n\n //#endregion\n}\n\n/**\n * Normalize the bind params to an array or object.\n * @hidden\n */\nexport function normalizeParams(...params: any[]): {\n params: BindParams;\n shouldPassAsObject: boolean;\n} {\n let bindParams = params.length > 1 ? params : (params[0] as BindParams);\n if (bindParams == null) {\n bindParams = [];\n }\n if (typeof bindParams !== 'object') {\n bindParams = [bindParams];\n }\n const shouldPassAsObject = !Array.isArray(bindParams);\n return {\n params: bindParams,\n shouldPassAsObject,\n };\n}\n\n/**\n * Compose `columnNames` and `columnValues` to an row object.\n * @hidden\n */\nexport function composeRow<T>(columnNames: ColumnNames, columnValues: ColumnValues): T {\n const row = {};\n if (columnNames.length !== columnValues.length) {\n throw new Error(\n `Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValues.length}`\n );\n }\n for (let i = 0; i < columnNames.length; i++) {\n row[columnNames[i]] = columnValues[i];\n }\n return row as T;\n}\n\n/**\n * Compose `columnNames` and `columnValuesList` to an array of row objects.\n * @hidden\n */\nexport function composeRows<T>(columnNames: ColumnNames, columnValuesList: ColumnValues[]): T[] {\n if (columnValuesList.length === 0) {\n return [];\n }\n if (columnNames.length !== columnValuesList[0].length) {\n // We only check the first row because SQLite returns the same column count for all rows.\n throw new Error(\n `Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValuesList[0].length}`\n );\n }\n const results: T[] = [];\n for (const columnValues of columnValuesList) {\n const row = {};\n for (let i = 0; i < columnNames.length; i++) {\n row[columnNames[i]] = columnValues[i];\n }\n results.push(row as T);\n }\n return results;\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type Database } from './Database';
|
|
3
|
+
import type { OpenOptions } from './NativeDatabase';
|
|
4
|
+
export interface SQLiteProviderProps {
|
|
5
|
+
/**
|
|
6
|
+
* The name of the database file to open.
|
|
7
|
+
*/
|
|
8
|
+
dbName: string;
|
|
9
|
+
/**
|
|
10
|
+
* Open options.
|
|
11
|
+
*/
|
|
12
|
+
options?: OpenOptions;
|
|
13
|
+
/**
|
|
14
|
+
* The children to render.
|
|
15
|
+
*/
|
|
16
|
+
children: React.ReactNode;
|
|
17
|
+
/**
|
|
18
|
+
* A custom initialization handler to run before rendering the children.
|
|
19
|
+
* You can use this to run database migrations or other setup tasks.
|
|
20
|
+
*/
|
|
21
|
+
initHandler?: (db: Database) => Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* A custom loading fallback to render before the database is ready.
|
|
24
|
+
* @default null
|
|
25
|
+
*/
|
|
26
|
+
loadingFallback?: React.ReactNode;
|
|
27
|
+
/**
|
|
28
|
+
* Handle errors from SQLiteProvider.
|
|
29
|
+
* @default rethrow the error
|
|
30
|
+
*/
|
|
31
|
+
errorHandler?: (error: Error) => void;
|
|
32
|
+
}
|
|
33
|
+
export declare function SQLiteProvider({ dbName, options, children, initHandler, loadingFallback, errorHandler, }: SQLiteProviderProps): JSX.Element | null;
|
|
34
|
+
export declare function useSQLiteContext(): Database;
|
|
35
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/next/hooks.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAiE,MAAM,OAAO,CAAC;AAEtF,OAAO,EAAqB,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,OAAO,CAAC,EAAE,WAAW,CAAC;IAEtB;;OAEG;IACH,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAE1B;;;OAGG;IACH,WAAW,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAE9C;;;OAGG;IACH,eAAe,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAElC;;;OAGG;IACH,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACvC;AAMD,wBAAgB,cAAc,CAAC,EAC7B,MAAM,EACN,OAAO,EACP,QAAQ,EACR,WAAW,EACX,eAAe,EACf,YAAY,GACb,EAAE,mBAAmB,sBAkDrB;AAGD,wBAAgB,gBAAgB,aAM/B"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { openDatabaseAsync } from './Database';
|
|
3
|
+
// Create a context for the SQLite database
|
|
4
|
+
const SQLiteContext = createContext(null);
|
|
5
|
+
// Create a provider component
|
|
6
|
+
export function SQLiteProvider({ dbName, options, children, initHandler, loadingFallback, errorHandler, }) {
|
|
7
|
+
const databaseRef = useRef(null);
|
|
8
|
+
const [loading, setLoading] = useState(true);
|
|
9
|
+
const [error, setError] = useState(null);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
async function setup() {
|
|
12
|
+
try {
|
|
13
|
+
const db = await openDatabaseAsync(dbName, options);
|
|
14
|
+
if (initHandler != null) {
|
|
15
|
+
await initHandler(db);
|
|
16
|
+
}
|
|
17
|
+
databaseRef.current = db;
|
|
18
|
+
setLoading(false);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
setError(e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function teardown(db) {
|
|
25
|
+
try {
|
|
26
|
+
await db?.closeAsync();
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
setError(e);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
setup();
|
|
33
|
+
return () => {
|
|
34
|
+
const db = databaseRef.current;
|
|
35
|
+
teardown(db);
|
|
36
|
+
databaseRef.current = null;
|
|
37
|
+
setLoading(true);
|
|
38
|
+
};
|
|
39
|
+
}, [dbName, options, initHandler]);
|
|
40
|
+
if (error != null) {
|
|
41
|
+
const handler = errorHandler ??
|
|
42
|
+
((e) => {
|
|
43
|
+
throw e;
|
|
44
|
+
});
|
|
45
|
+
handler(error);
|
|
46
|
+
}
|
|
47
|
+
if (loading || !databaseRef.current) {
|
|
48
|
+
return loadingFallback != null ? <>{loadingFallback}</> : null;
|
|
49
|
+
}
|
|
50
|
+
return <SQLiteContext.Provider value={databaseRef.current}>{children}</SQLiteContext.Provider>;
|
|
51
|
+
}
|
|
52
|
+
// Create a hook for accessing the SQLite database context
|
|
53
|
+
export function useSQLiteContext() {
|
|
54
|
+
const context = useContext(SQLiteContext);
|
|
55
|
+
if (context == null) {
|
|
56
|
+
throw new Error('useSQLiteContext must be used within a <SQLiteProvider>');
|
|
57
|
+
}
|
|
58
|
+
return context;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/next/hooks.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEtF,OAAO,EAAE,iBAAiB,EAAiB,MAAM,YAAY,CAAC;AAsC9D,2CAA2C;AAC3C,MAAM,aAAa,GAAG,aAAa,CAAkB,IAAI,CAAC,CAAC;AAE3D,8BAA8B;AAC9B,MAAM,UAAU,cAAc,CAAC,EAC7B,MAAM,EACN,OAAO,EACP,QAAQ,EACR,WAAW,EACX,eAAe,EACf,YAAY,GACQ;IACpB,MAAM,WAAW,GAAG,MAAM,CAAkB,IAAI,CAAC,CAAC;IAClD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC7C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IAEvD,SAAS,CAAC,GAAG,EAAE;QACb,KAAK,UAAU,KAAK;YAClB,IAAI;gBACF,MAAM,EAAE,GAAG,MAAM,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBACpD,IAAI,WAAW,IAAI,IAAI,EAAE;oBACvB,MAAM,WAAW,CAAC,EAAE,CAAC,CAAC;iBACvB;gBACD,WAAW,CAAC,OAAO,GAAG,EAAE,CAAC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,IAAI,EAAE;QACnB,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;KAC5E;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import React, { createContext, useContext, useEffect, useRef, useState } from 'react';\n\nimport { openDatabaseAsync, type Database } from './Database';\nimport type { OpenOptions } from './NativeDatabase';\n\nexport interface SQLiteProviderProps {\n /**\n * The name of the database file to open.\n */\n dbName: string;\n\n /**\n * Open options.\n */\n options?: OpenOptions;\n\n /**\n * The children to render.\n */\n children: React.ReactNode;\n\n /**\n * A custom initialization handler to run before rendering the children.\n * You can use this to run database migrations or other setup tasks.\n */\n initHandler?: (db: Database) => Promise<void>;\n\n /**\n * A custom loading fallback to render before the database is ready.\n * @default null\n */\n loadingFallback?: React.ReactNode;\n\n /**\n * Handle errors from SQLiteProvider.\n * @default rethrow the error\n */\n errorHandler?: (error: Error) => void;\n}\n\n// Create a context for the SQLite database\nconst SQLiteContext = createContext<Database | null>(null);\n\n// Create a provider component\nexport function SQLiteProvider({\n dbName,\n options,\n children,\n initHandler,\n loadingFallback,\n errorHandler,\n}: SQLiteProviderProps) {\n const databaseRef = useRef<Database | null>(null);\n const [loading, setLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n useEffect(() => {\n async function setup() {\n try {\n const db = await openDatabaseAsync(dbName, options);\n if (initHandler != null) {\n await initHandler(db);\n }\n databaseRef.current = db;\n setLoading(false);\n } catch (e) {\n setError(e);\n }\n }\n\n async function teardown(db: Database | null) {\n try {\n await db?.closeAsync();\n } catch (e) {\n setError(e);\n }\n }\n\n setup();\n\n return () => {\n const db = databaseRef.current;\n teardown(db);\n databaseRef.current = null;\n setLoading(true);\n };\n }, [dbName, options, initHandler]);\n\n if (error != null) {\n const handler =\n errorHandler ??\n ((e) => {\n throw e;\n });\n handler(error);\n }\n\n if (loading || !databaseRef.current) {\n return loadingFallback != null ? <>{loadingFallback}</> : null;\n }\n return <SQLiteContext.Provider value={databaseRef.current}>{children}</SQLiteContext.Provider>;\n}\n\n// Create a hook for accessing the SQLite database context\nexport function useSQLiteContext() {\n const context = useContext(SQLiteContext);\n if (context == null) {\n throw new Error('useSQLiteContext must be used within a <SQLiteProvider>');\n }\n return context;\n}\n"]}
|