@photostructure/sqlite 0.4.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -1
- package/README.md +4 -8
- package/binding.gyp +2 -0
- package/dist/index.cjs +247 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +145 -24
- package/dist/index.d.mts +145 -24
- package/dist/index.d.ts +145 -24
- package/dist/index.mjs +247 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -11
- package/prebuilds/darwin-arm64/@photostructure+sqlite.glibc.node +0 -0
- package/prebuilds/darwin-x64/@photostructure+sqlite.glibc.node +0 -0
- package/prebuilds/linux-arm64/@photostructure+sqlite.glibc.node +0 -0
- package/prebuilds/linux-arm64/@photostructure+sqlite.musl.node +0 -0
- package/prebuilds/linux-x64/@photostructure+sqlite.glibc.node +0 -0
- package/prebuilds/linux-x64/@photostructure+sqlite.musl.node +0 -0
- package/prebuilds/test_extension.so +0 -0
- package/prebuilds/win32-arm64/@photostructure+sqlite.glibc.node +0 -0
- package/prebuilds/win32-x64/@photostructure+sqlite.glibc.node +0 -0
- package/src/enhance.ts +382 -55
- package/src/index.ts +85 -2
- package/src/sqlite_impl.cpp +133 -10
- package/src/sqlite_impl.h +19 -0
- package/src/types/database-sync-instance.ts +43 -0
- package/src/types/database-sync-options.ts +19 -0
- package/src/upstream/node_sqlite.cc +312 -17
- package/src/upstream/node_sqlite.h +80 -0
- package/src/upstream/sqlite.js +0 -3
- package/src/upstream/sqlite3.c +5027 -3518
- package/src/upstream/sqlite3.h +195 -58
- package/src/upstream/sqlite3ext.h +10 -1
package/dist/index.d.mts
CHANGED
|
@@ -349,9 +349,49 @@ interface DatabaseSyncInstance {
|
|
|
349
349
|
* @see https://sqlite.org/c3ref/set_authorizer.html
|
|
350
350
|
*/
|
|
351
351
|
setAuthorizer(callback: ((actionCode: number, param1: string | null, param2: string | null, param3: string | null, param4: string | null) => number) | null): void;
|
|
352
|
+
/**
|
|
353
|
+
* An object with getters and setters for each SQLite limit.
|
|
354
|
+
* Setting a property changes the limit immediately.
|
|
355
|
+
* Setting a property to `Infinity` resets the limit to its compile-time maximum.
|
|
356
|
+
* @see https://sqlite.org/c3ref/limit.html
|
|
357
|
+
*/
|
|
358
|
+
readonly limits: DatabaseSyncLimits;
|
|
359
|
+
/** @internal Native method to get a SQLite limit by ID. */
|
|
360
|
+
getLimit(limitId: number): number;
|
|
361
|
+
/** @internal Native method to set a SQLite limit by ID. Returns old value. */
|
|
362
|
+
setLimit(limitId: number, value: number): number;
|
|
352
363
|
/** Dispose of the database resources using the explicit resource management protocol. */
|
|
353
364
|
[Symbol.dispose](): void;
|
|
354
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Represents the configurable SQLite limits for a database connection.
|
|
368
|
+
* Each property corresponds to a SQLite limit constant.
|
|
369
|
+
* @see https://sqlite.org/c3ref/limit.html
|
|
370
|
+
*/
|
|
371
|
+
interface DatabaseSyncLimits {
|
|
372
|
+
/** Maximum length of any string or BLOB or table row, in bytes. */
|
|
373
|
+
length: number;
|
|
374
|
+
/** Maximum length of an SQL statement, in bytes. */
|
|
375
|
+
sqlLength: number;
|
|
376
|
+
/** Maximum number of columns in a table, result set, or index. */
|
|
377
|
+
column: number;
|
|
378
|
+
/** Maximum depth of the parse tree on any expression. */
|
|
379
|
+
exprDepth: number;
|
|
380
|
+
/** Maximum number of terms in a compound SELECT statement. */
|
|
381
|
+
compoundSelect: number;
|
|
382
|
+
/** Maximum number of instructions in a virtual machine program. */
|
|
383
|
+
vdbeOp: number;
|
|
384
|
+
/** Maximum number of arguments on a function. */
|
|
385
|
+
functionArg: number;
|
|
386
|
+
/** Maximum number of attached databases. */
|
|
387
|
+
attach: number;
|
|
388
|
+
/** Maximum length of the pattern argument to the LIKE or GLOB operators. */
|
|
389
|
+
likePatternLength: number;
|
|
390
|
+
/** Maximum index number of any parameter in an SQL statement. */
|
|
391
|
+
variableNumber: number;
|
|
392
|
+
/** Maximum depth of recursion for triggers. */
|
|
393
|
+
triggerDepth: number;
|
|
394
|
+
}
|
|
355
395
|
|
|
356
396
|
/**
|
|
357
397
|
* SQLTagStore provides cached prepared statements via tagged template syntax.
|
|
@@ -482,6 +522,25 @@ interface DatabaseSyncOptions {
|
|
|
482
522
|
* @default true
|
|
483
523
|
*/
|
|
484
524
|
readonly open?: boolean;
|
|
525
|
+
/**
|
|
526
|
+
* An object specifying initial SQLite limits to set when opening the database.
|
|
527
|
+
* Each property corresponds to a SQLite limit constant. Only integer values are
|
|
528
|
+
* accepted (no Infinity). Omitted properties retain their default values.
|
|
529
|
+
* @see https://sqlite.org/c3ref/limit.html
|
|
530
|
+
*/
|
|
531
|
+
readonly limits?: {
|
|
532
|
+
readonly length?: number;
|
|
533
|
+
readonly sqlLength?: number;
|
|
534
|
+
readonly column?: number;
|
|
535
|
+
readonly exprDepth?: number;
|
|
536
|
+
readonly compoundSelect?: number;
|
|
537
|
+
readonly vdbeOp?: number;
|
|
538
|
+
readonly functionArg?: number;
|
|
539
|
+
readonly attach?: number;
|
|
540
|
+
readonly likePatternLength?: number;
|
|
541
|
+
readonly variableNumber?: number;
|
|
542
|
+
readonly triggerDepth?: number;
|
|
543
|
+
};
|
|
485
544
|
}
|
|
486
545
|
|
|
487
546
|
/**
|
|
@@ -760,9 +819,9 @@ interface TransactionFunction<F extends (...args: any[]) => any> {
|
|
|
760
819
|
* compatible database, including `node:sqlite` DatabaseSync and this package's
|
|
761
820
|
* DatabaseSync.
|
|
762
821
|
*
|
|
763
|
-
* This module provides the `enhance()` function which adds `.pragma()
|
|
764
|
-
* `.transaction()
|
|
765
|
-
* node:sqlite DatabaseSync).
|
|
822
|
+
* This module provides the `enhance()` function which adds `.pragma()`,
|
|
823
|
+
* `.transaction()`, and statement modes (`.pluck()`, `.raw()`, `.expand()`)
|
|
824
|
+
* to database instances that don't have them (e.g., node:sqlite DatabaseSync).
|
|
766
825
|
*/
|
|
767
826
|
|
|
768
827
|
/**
|
|
@@ -776,9 +835,74 @@ interface EnhanceableDatabaseSync {
|
|
|
776
835
|
prepare(sql: string): {
|
|
777
836
|
all(): unknown[];
|
|
778
837
|
};
|
|
838
|
+
/** Whether the database connection is open */
|
|
839
|
+
readonly isOpen?: boolean;
|
|
779
840
|
/** Whether a transaction is currently active */
|
|
780
841
|
readonly isTransaction: boolean;
|
|
781
842
|
}
|
|
843
|
+
/**
|
|
844
|
+
* A statement enhanced with better-sqlite3-style `.pluck()`, `.raw()`, and
|
|
845
|
+
* `.expand()` methods. These are mutually exclusive — enabling one disables
|
|
846
|
+
* the others.
|
|
847
|
+
*/
|
|
848
|
+
interface EnhancedStatementMethods {
|
|
849
|
+
/**
|
|
850
|
+
* Causes the statement to return only the first column value of each row.
|
|
851
|
+
*
|
|
852
|
+
* When plucking is turned on, raw and expand modes are turned off.
|
|
853
|
+
*
|
|
854
|
+
* @param toggle Enable (true) or disable (false) pluck mode. Defaults to true.
|
|
855
|
+
* @returns The same statement for chaining.
|
|
856
|
+
*
|
|
857
|
+
* @example
|
|
858
|
+
* ```typescript
|
|
859
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
860
|
+
* // Returns: 42 (not { "COUNT(*)": 42 })
|
|
861
|
+
*
|
|
862
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
863
|
+
* // Returns: ["Alice", "Bob"] (not [{ name: "Alice" }, { name: "Bob" }])
|
|
864
|
+
* ```
|
|
865
|
+
*/
|
|
866
|
+
pluck(toggle?: boolean): this;
|
|
867
|
+
/**
|
|
868
|
+
* Causes the statement to return rows as arrays of values instead of objects.
|
|
869
|
+
*
|
|
870
|
+
* When raw mode is turned on, pluck and expand modes are turned off.
|
|
871
|
+
*
|
|
872
|
+
* @param toggle Enable (true) or disable (false) raw mode. Defaults to true.
|
|
873
|
+
* @returns The same statement for chaining.
|
|
874
|
+
*
|
|
875
|
+
* @example
|
|
876
|
+
* ```typescript
|
|
877
|
+
* const rows = db.prepare("SELECT id, name FROM users").raw().all();
|
|
878
|
+
* // Returns: [[1, "Alice"], [2, "Bob"]] (not [{ id: 1, name: "Alice" }, ...])
|
|
879
|
+
* ```
|
|
880
|
+
*/
|
|
881
|
+
raw(toggle?: boolean): this;
|
|
882
|
+
/**
|
|
883
|
+
* Causes the statement to return data namespaced by table. Each key in a row
|
|
884
|
+
* object will be a table name, and each corresponding value will be a nested
|
|
885
|
+
* object containing that table's columns. Columns from expressions or
|
|
886
|
+
* subqueries are placed under the special `$` namespace.
|
|
887
|
+
*
|
|
888
|
+
* When expand mode is turned on, pluck and raw modes are turned off.
|
|
889
|
+
*
|
|
890
|
+
* Requires the statement to have a `.columns()` method (available on real
|
|
891
|
+
* statements but not minimal mocks).
|
|
892
|
+
*
|
|
893
|
+
* @param toggle Enable (true) or disable (false) expand mode. Defaults to true.
|
|
894
|
+
* @returns The same statement for chaining.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* ```typescript
|
|
898
|
+
* const rows = db.prepare("SELECT u.id, u.name, p.title FROM users u JOIN posts p ON ...").expand().all();
|
|
899
|
+
* // Returns: [{ users: { id: 1, name: "Alice" }, posts: { title: "Hello" } }]
|
|
900
|
+
* ```
|
|
901
|
+
*/
|
|
902
|
+
expand(toggle?: boolean): this;
|
|
903
|
+
/** The database instance this statement was prepared from. */
|
|
904
|
+
readonly database: EnhanceableDatabaseSync;
|
|
905
|
+
}
|
|
782
906
|
/**
|
|
783
907
|
* Interface for an enhanced database with pragma() and transaction() methods.
|
|
784
908
|
*/
|
|
@@ -815,48 +939,45 @@ interface EnhancedMethods {
|
|
|
815
939
|
transaction<F extends (...args: any[]) => any>(fn: F): TransactionFunction<F>;
|
|
816
940
|
}
|
|
817
941
|
/**
|
|
818
|
-
* A database instance that has been enhanced with pragma()
|
|
942
|
+
* A database instance that has been enhanced with pragma(), transaction(),
|
|
943
|
+
* and statement modes (pluck/raw/expand) on statements returned by prepare().
|
|
819
944
|
*/
|
|
820
|
-
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = T & EnhancedMethods
|
|
945
|
+
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = Omit<T, "prepare"> & EnhancedMethods & {
|
|
946
|
+
prepare(...args: Parameters<T["prepare"]>): ReturnType<T["prepare"]> & EnhancedStatementMethods;
|
|
947
|
+
};
|
|
821
948
|
/**
|
|
822
|
-
* Ensures that `.pragma()
|
|
823
|
-
* given database.
|
|
949
|
+
* Ensures that `.pragma()`, `.transaction()`, and statement modes
|
|
950
|
+
* (`.pluck()`, `.raw()`, `.expand()`) are available on the given database.
|
|
824
951
|
*
|
|
825
952
|
* This function can enhance:
|
|
826
953
|
* - `node:sqlite` DatabaseSync instances (adds the methods)
|
|
827
|
-
* - `@photostructure/sqlite` DatabaseSync instances (
|
|
828
|
-
* methods)
|
|
954
|
+
* - `@photostructure/sqlite` DatabaseSync instances (adds the methods)
|
|
829
955
|
* - Any object with compatible `exec()`, `prepare()`, and `isTransaction`
|
|
830
956
|
*
|
|
831
957
|
* The enhancement is done by adding methods directly to the instance, not the
|
|
832
958
|
* prototype, so it won't affect other instances or the original class.
|
|
833
959
|
*
|
|
834
960
|
* @param db The database instance to enhance
|
|
835
|
-
* @returns The same instance with `.pragma()
|
|
836
|
-
* guaranteed
|
|
961
|
+
* @returns The same instance with `.pragma()`, `.transaction()`, and
|
|
962
|
+
* `.pluck()` / `.raw()` / `.expand()` (on prepared statements) guaranteed
|
|
837
963
|
*
|
|
838
964
|
* @example
|
|
839
965
|
* ```typescript
|
|
840
|
-
*
|
|
841
|
-
* import { DatabaseSync } from 'node:sqlite';
|
|
842
|
-
* import { enhance } from '@photostructure/sqlite';
|
|
966
|
+
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
843
967
|
*
|
|
844
968
|
* const db = enhance(new DatabaseSync(':memory:'));
|
|
845
969
|
*
|
|
846
|
-
* //
|
|
970
|
+
* // better-sqlite3-style pragma
|
|
847
971
|
* db.pragma('journal_mode = wal');
|
|
972
|
+
*
|
|
973
|
+
* // better-sqlite3-style transactions
|
|
848
974
|
* const insertMany = db.transaction((items) => {
|
|
849
975
|
* for (const item of items) insert.run(item);
|
|
850
976
|
* });
|
|
851
|
-
* ```
|
|
852
977
|
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
857
|
-
*
|
|
858
|
-
* const db = enhance(new DatabaseSync(':memory:'));
|
|
859
|
-
* // db already had these methods, enhance() just returns it unchanged
|
|
978
|
+
* // better-sqlite3-style pluck
|
|
979
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
980
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
860
981
|
* ```
|
|
861
982
|
*/
|
|
862
983
|
declare function enhance<T extends EnhanceableDatabaseSync>(db: T): EnhancedDatabaseSync<T>;
|
|
@@ -1052,4 +1173,4 @@ interface BackupOptions {
|
|
|
1052
1173
|
declare const backup: (sourceDb: DatabaseSyncInstance, destination: string | Buffer | URL, options?: BackupOptions) => Promise<number>;
|
|
1053
1174
|
declare const _default: SqliteModule;
|
|
1054
1175
|
|
|
1055
|
-
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, type DatabaseSyncOptions, type EnhanceableDatabaseSync, type EnhancedDatabaseSync, type EnhancedMethods, type PragmaOptions, SQLTagStore, type SQLTagStoreInstance, Session, type SessionOptions, type SqliteAuthorizationActions, type SqliteAuthorizationResults, type SqliteChangesetConflictTypes, type SqliteChangesetResolution, type SqliteConstants, type SqliteModule, type SqliteOpenFlags, type StatementColumnMetadata, type StatementOptions, StatementSync, type StatementSyncInstance, type TransactionFunction, type TransactionMode, type UserFunctionOptions, backup, constants, _default as default, enhance, isEnhanced };
|
|
1176
|
+
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, type DatabaseSyncLimits, type DatabaseSyncOptions, type EnhanceableDatabaseSync, type EnhancedDatabaseSync, type EnhancedMethods, type EnhancedStatementMethods, type PragmaOptions, SQLTagStore, type SQLTagStoreInstance, Session, type SessionOptions, type SqliteAuthorizationActions, type SqliteAuthorizationResults, type SqliteChangesetConflictTypes, type SqliteChangesetResolution, type SqliteConstants, type SqliteModule, type SqliteOpenFlags, type StatementColumnMetadata, type StatementOptions, StatementSync, type StatementSyncInstance, type TransactionFunction, type TransactionMode, type UserFunctionOptions, backup, constants, _default as default, enhance, isEnhanced };
|
package/dist/index.d.ts
CHANGED
|
@@ -349,9 +349,49 @@ interface DatabaseSyncInstance {
|
|
|
349
349
|
* @see https://sqlite.org/c3ref/set_authorizer.html
|
|
350
350
|
*/
|
|
351
351
|
setAuthorizer(callback: ((actionCode: number, param1: string | null, param2: string | null, param3: string | null, param4: string | null) => number) | null): void;
|
|
352
|
+
/**
|
|
353
|
+
* An object with getters and setters for each SQLite limit.
|
|
354
|
+
* Setting a property changes the limit immediately.
|
|
355
|
+
* Setting a property to `Infinity` resets the limit to its compile-time maximum.
|
|
356
|
+
* @see https://sqlite.org/c3ref/limit.html
|
|
357
|
+
*/
|
|
358
|
+
readonly limits: DatabaseSyncLimits;
|
|
359
|
+
/** @internal Native method to get a SQLite limit by ID. */
|
|
360
|
+
getLimit(limitId: number): number;
|
|
361
|
+
/** @internal Native method to set a SQLite limit by ID. Returns old value. */
|
|
362
|
+
setLimit(limitId: number, value: number): number;
|
|
352
363
|
/** Dispose of the database resources using the explicit resource management protocol. */
|
|
353
364
|
[Symbol.dispose](): void;
|
|
354
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Represents the configurable SQLite limits for a database connection.
|
|
368
|
+
* Each property corresponds to a SQLite limit constant.
|
|
369
|
+
* @see https://sqlite.org/c3ref/limit.html
|
|
370
|
+
*/
|
|
371
|
+
interface DatabaseSyncLimits {
|
|
372
|
+
/** Maximum length of any string or BLOB or table row, in bytes. */
|
|
373
|
+
length: number;
|
|
374
|
+
/** Maximum length of an SQL statement, in bytes. */
|
|
375
|
+
sqlLength: number;
|
|
376
|
+
/** Maximum number of columns in a table, result set, or index. */
|
|
377
|
+
column: number;
|
|
378
|
+
/** Maximum depth of the parse tree on any expression. */
|
|
379
|
+
exprDepth: number;
|
|
380
|
+
/** Maximum number of terms in a compound SELECT statement. */
|
|
381
|
+
compoundSelect: number;
|
|
382
|
+
/** Maximum number of instructions in a virtual machine program. */
|
|
383
|
+
vdbeOp: number;
|
|
384
|
+
/** Maximum number of arguments on a function. */
|
|
385
|
+
functionArg: number;
|
|
386
|
+
/** Maximum number of attached databases. */
|
|
387
|
+
attach: number;
|
|
388
|
+
/** Maximum length of the pattern argument to the LIKE or GLOB operators. */
|
|
389
|
+
likePatternLength: number;
|
|
390
|
+
/** Maximum index number of any parameter in an SQL statement. */
|
|
391
|
+
variableNumber: number;
|
|
392
|
+
/** Maximum depth of recursion for triggers. */
|
|
393
|
+
triggerDepth: number;
|
|
394
|
+
}
|
|
355
395
|
|
|
356
396
|
/**
|
|
357
397
|
* SQLTagStore provides cached prepared statements via tagged template syntax.
|
|
@@ -482,6 +522,25 @@ interface DatabaseSyncOptions {
|
|
|
482
522
|
* @default true
|
|
483
523
|
*/
|
|
484
524
|
readonly open?: boolean;
|
|
525
|
+
/**
|
|
526
|
+
* An object specifying initial SQLite limits to set when opening the database.
|
|
527
|
+
* Each property corresponds to a SQLite limit constant. Only integer values are
|
|
528
|
+
* accepted (no Infinity). Omitted properties retain their default values.
|
|
529
|
+
* @see https://sqlite.org/c3ref/limit.html
|
|
530
|
+
*/
|
|
531
|
+
readonly limits?: {
|
|
532
|
+
readonly length?: number;
|
|
533
|
+
readonly sqlLength?: number;
|
|
534
|
+
readonly column?: number;
|
|
535
|
+
readonly exprDepth?: number;
|
|
536
|
+
readonly compoundSelect?: number;
|
|
537
|
+
readonly vdbeOp?: number;
|
|
538
|
+
readonly functionArg?: number;
|
|
539
|
+
readonly attach?: number;
|
|
540
|
+
readonly likePatternLength?: number;
|
|
541
|
+
readonly variableNumber?: number;
|
|
542
|
+
readonly triggerDepth?: number;
|
|
543
|
+
};
|
|
485
544
|
}
|
|
486
545
|
|
|
487
546
|
/**
|
|
@@ -760,9 +819,9 @@ interface TransactionFunction<F extends (...args: any[]) => any> {
|
|
|
760
819
|
* compatible database, including `node:sqlite` DatabaseSync and this package's
|
|
761
820
|
* DatabaseSync.
|
|
762
821
|
*
|
|
763
|
-
* This module provides the `enhance()` function which adds `.pragma()
|
|
764
|
-
* `.transaction()
|
|
765
|
-
* node:sqlite DatabaseSync).
|
|
822
|
+
* This module provides the `enhance()` function which adds `.pragma()`,
|
|
823
|
+
* `.transaction()`, and statement modes (`.pluck()`, `.raw()`, `.expand()`)
|
|
824
|
+
* to database instances that don't have them (e.g., node:sqlite DatabaseSync).
|
|
766
825
|
*/
|
|
767
826
|
|
|
768
827
|
/**
|
|
@@ -776,9 +835,74 @@ interface EnhanceableDatabaseSync {
|
|
|
776
835
|
prepare(sql: string): {
|
|
777
836
|
all(): unknown[];
|
|
778
837
|
};
|
|
838
|
+
/** Whether the database connection is open */
|
|
839
|
+
readonly isOpen?: boolean;
|
|
779
840
|
/** Whether a transaction is currently active */
|
|
780
841
|
readonly isTransaction: boolean;
|
|
781
842
|
}
|
|
843
|
+
/**
|
|
844
|
+
* A statement enhanced with better-sqlite3-style `.pluck()`, `.raw()`, and
|
|
845
|
+
* `.expand()` methods. These are mutually exclusive — enabling one disables
|
|
846
|
+
* the others.
|
|
847
|
+
*/
|
|
848
|
+
interface EnhancedStatementMethods {
|
|
849
|
+
/**
|
|
850
|
+
* Causes the statement to return only the first column value of each row.
|
|
851
|
+
*
|
|
852
|
+
* When plucking is turned on, raw and expand modes are turned off.
|
|
853
|
+
*
|
|
854
|
+
* @param toggle Enable (true) or disable (false) pluck mode. Defaults to true.
|
|
855
|
+
* @returns The same statement for chaining.
|
|
856
|
+
*
|
|
857
|
+
* @example
|
|
858
|
+
* ```typescript
|
|
859
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
860
|
+
* // Returns: 42 (not { "COUNT(*)": 42 })
|
|
861
|
+
*
|
|
862
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
863
|
+
* // Returns: ["Alice", "Bob"] (not [{ name: "Alice" }, { name: "Bob" }])
|
|
864
|
+
* ```
|
|
865
|
+
*/
|
|
866
|
+
pluck(toggle?: boolean): this;
|
|
867
|
+
/**
|
|
868
|
+
* Causes the statement to return rows as arrays of values instead of objects.
|
|
869
|
+
*
|
|
870
|
+
* When raw mode is turned on, pluck and expand modes are turned off.
|
|
871
|
+
*
|
|
872
|
+
* @param toggle Enable (true) or disable (false) raw mode. Defaults to true.
|
|
873
|
+
* @returns The same statement for chaining.
|
|
874
|
+
*
|
|
875
|
+
* @example
|
|
876
|
+
* ```typescript
|
|
877
|
+
* const rows = db.prepare("SELECT id, name FROM users").raw().all();
|
|
878
|
+
* // Returns: [[1, "Alice"], [2, "Bob"]] (not [{ id: 1, name: "Alice" }, ...])
|
|
879
|
+
* ```
|
|
880
|
+
*/
|
|
881
|
+
raw(toggle?: boolean): this;
|
|
882
|
+
/**
|
|
883
|
+
* Causes the statement to return data namespaced by table. Each key in a row
|
|
884
|
+
* object will be a table name, and each corresponding value will be a nested
|
|
885
|
+
* object containing that table's columns. Columns from expressions or
|
|
886
|
+
* subqueries are placed under the special `$` namespace.
|
|
887
|
+
*
|
|
888
|
+
* When expand mode is turned on, pluck and raw modes are turned off.
|
|
889
|
+
*
|
|
890
|
+
* Requires the statement to have a `.columns()` method (available on real
|
|
891
|
+
* statements but not minimal mocks).
|
|
892
|
+
*
|
|
893
|
+
* @param toggle Enable (true) or disable (false) expand mode. Defaults to true.
|
|
894
|
+
* @returns The same statement for chaining.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* ```typescript
|
|
898
|
+
* const rows = db.prepare("SELECT u.id, u.name, p.title FROM users u JOIN posts p ON ...").expand().all();
|
|
899
|
+
* // Returns: [{ users: { id: 1, name: "Alice" }, posts: { title: "Hello" } }]
|
|
900
|
+
* ```
|
|
901
|
+
*/
|
|
902
|
+
expand(toggle?: boolean): this;
|
|
903
|
+
/** The database instance this statement was prepared from. */
|
|
904
|
+
readonly database: EnhanceableDatabaseSync;
|
|
905
|
+
}
|
|
782
906
|
/**
|
|
783
907
|
* Interface for an enhanced database with pragma() and transaction() methods.
|
|
784
908
|
*/
|
|
@@ -815,48 +939,45 @@ interface EnhancedMethods {
|
|
|
815
939
|
transaction<F extends (...args: any[]) => any>(fn: F): TransactionFunction<F>;
|
|
816
940
|
}
|
|
817
941
|
/**
|
|
818
|
-
* A database instance that has been enhanced with pragma()
|
|
942
|
+
* A database instance that has been enhanced with pragma(), transaction(),
|
|
943
|
+
* and statement modes (pluck/raw/expand) on statements returned by prepare().
|
|
819
944
|
*/
|
|
820
|
-
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = T & EnhancedMethods
|
|
945
|
+
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = Omit<T, "prepare"> & EnhancedMethods & {
|
|
946
|
+
prepare(...args: Parameters<T["prepare"]>): ReturnType<T["prepare"]> & EnhancedStatementMethods;
|
|
947
|
+
};
|
|
821
948
|
/**
|
|
822
|
-
* Ensures that `.pragma()
|
|
823
|
-
* given database.
|
|
949
|
+
* Ensures that `.pragma()`, `.transaction()`, and statement modes
|
|
950
|
+
* (`.pluck()`, `.raw()`, `.expand()`) are available on the given database.
|
|
824
951
|
*
|
|
825
952
|
* This function can enhance:
|
|
826
953
|
* - `node:sqlite` DatabaseSync instances (adds the methods)
|
|
827
|
-
* - `@photostructure/sqlite` DatabaseSync instances (
|
|
828
|
-
* methods)
|
|
954
|
+
* - `@photostructure/sqlite` DatabaseSync instances (adds the methods)
|
|
829
955
|
* - Any object with compatible `exec()`, `prepare()`, and `isTransaction`
|
|
830
956
|
*
|
|
831
957
|
* The enhancement is done by adding methods directly to the instance, not the
|
|
832
958
|
* prototype, so it won't affect other instances or the original class.
|
|
833
959
|
*
|
|
834
960
|
* @param db The database instance to enhance
|
|
835
|
-
* @returns The same instance with `.pragma()
|
|
836
|
-
* guaranteed
|
|
961
|
+
* @returns The same instance with `.pragma()`, `.transaction()`, and
|
|
962
|
+
* `.pluck()` / `.raw()` / `.expand()` (on prepared statements) guaranteed
|
|
837
963
|
*
|
|
838
964
|
* @example
|
|
839
965
|
* ```typescript
|
|
840
|
-
*
|
|
841
|
-
* import { DatabaseSync } from 'node:sqlite';
|
|
842
|
-
* import { enhance } from '@photostructure/sqlite';
|
|
966
|
+
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
843
967
|
*
|
|
844
968
|
* const db = enhance(new DatabaseSync(':memory:'));
|
|
845
969
|
*
|
|
846
|
-
* //
|
|
970
|
+
* // better-sqlite3-style pragma
|
|
847
971
|
* db.pragma('journal_mode = wal');
|
|
972
|
+
*
|
|
973
|
+
* // better-sqlite3-style transactions
|
|
848
974
|
* const insertMany = db.transaction((items) => {
|
|
849
975
|
* for (const item of items) insert.run(item);
|
|
850
976
|
* });
|
|
851
|
-
* ```
|
|
852
977
|
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
857
|
-
*
|
|
858
|
-
* const db = enhance(new DatabaseSync(':memory:'));
|
|
859
|
-
* // db already had these methods, enhance() just returns it unchanged
|
|
978
|
+
* // better-sqlite3-style pluck
|
|
979
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
980
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
860
981
|
* ```
|
|
861
982
|
*/
|
|
862
983
|
declare function enhance<T extends EnhanceableDatabaseSync>(db: T): EnhancedDatabaseSync<T>;
|
|
@@ -1052,4 +1173,4 @@ interface BackupOptions {
|
|
|
1052
1173
|
declare const backup: (sourceDb: DatabaseSyncInstance, destination: string | Buffer | URL, options?: BackupOptions) => Promise<number>;
|
|
1053
1174
|
declare const _default: SqliteModule;
|
|
1054
1175
|
|
|
1055
|
-
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, type DatabaseSyncOptions, type EnhanceableDatabaseSync, type EnhancedDatabaseSync, type EnhancedMethods, type PragmaOptions, SQLTagStore, type SQLTagStoreInstance, Session, type SessionOptions, type SqliteAuthorizationActions, type SqliteAuthorizationResults, type SqliteChangesetConflictTypes, type SqliteChangesetResolution, type SqliteConstants, type SqliteModule, type SqliteOpenFlags, type StatementColumnMetadata, type StatementOptions, StatementSync, type StatementSyncInstance, type TransactionFunction, type TransactionMode, type UserFunctionOptions, backup, constants, _default as default, enhance, isEnhanced };
|
|
1176
|
+
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, type DatabaseSyncLimits, type DatabaseSyncOptions, type EnhanceableDatabaseSync, type EnhancedDatabaseSync, type EnhancedMethods, type EnhancedStatementMethods, type PragmaOptions, SQLTagStore, type SQLTagStoreInstance, Session, type SessionOptions, type SqliteAuthorizationActions, type SqliteAuthorizationResults, type SqliteChangesetConflictTypes, type SqliteChangesetResolution, type SqliteConstants, type SqliteModule, type SqliteOpenFlags, type StatementColumnMetadata, type StatementOptions, StatementSync, type StatementSyncInstance, type TransactionFunction, type TransactionMode, type UserFunctionOptions, backup, constants, _default as default, enhance, isEnhanced };
|