@photostructure/sqlite 0.4.0 → 0.5.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 +14 -1
- package/README.md +0 -6
- package/dist/index.cjs +182 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +86 -24
- package/dist/index.d.mts +86 -24
- package/dist/index.d.ts +86 -24
- package/dist/index.mjs +182 -26
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/prebuilds/darwin-arm64/@photostructure+sqlite.glibc.node +0 -0
- package/prebuilds/darwin-x64/@photostructure+sqlite.glibc.node +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 +379 -55
- package/src/index.ts +1 -0
package/dist/index.d.cts
CHANGED
|
@@ -760,9 +760,9 @@ interface TransactionFunction<F extends (...args: any[]) => any> {
|
|
|
760
760
|
* compatible database, including `node:sqlite` DatabaseSync and this package's
|
|
761
761
|
* DatabaseSync.
|
|
762
762
|
*
|
|
763
|
-
* This module provides the `enhance()` function which adds `.pragma()
|
|
764
|
-
* `.transaction()
|
|
765
|
-
* node:sqlite DatabaseSync).
|
|
763
|
+
* This module provides the `enhance()` function which adds `.pragma()`,
|
|
764
|
+
* `.transaction()`, and statement modes (`.pluck()`, `.raw()`, `.expand()`)
|
|
765
|
+
* to database instances that don't have them (e.g., node:sqlite DatabaseSync).
|
|
766
766
|
*/
|
|
767
767
|
|
|
768
768
|
/**
|
|
@@ -776,9 +776,74 @@ interface EnhanceableDatabaseSync {
|
|
|
776
776
|
prepare(sql: string): {
|
|
777
777
|
all(): unknown[];
|
|
778
778
|
};
|
|
779
|
+
/** Whether the database connection is open */
|
|
780
|
+
readonly isOpen?: boolean;
|
|
779
781
|
/** Whether a transaction is currently active */
|
|
780
782
|
readonly isTransaction: boolean;
|
|
781
783
|
}
|
|
784
|
+
/**
|
|
785
|
+
* A statement enhanced with better-sqlite3-style `.pluck()`, `.raw()`, and
|
|
786
|
+
* `.expand()` methods. These are mutually exclusive — enabling one disables
|
|
787
|
+
* the others.
|
|
788
|
+
*/
|
|
789
|
+
interface EnhancedStatementMethods {
|
|
790
|
+
/**
|
|
791
|
+
* Causes the statement to return only the first column value of each row.
|
|
792
|
+
*
|
|
793
|
+
* When plucking is turned on, raw and expand modes are turned off.
|
|
794
|
+
*
|
|
795
|
+
* @param toggle Enable (true) or disable (false) pluck mode. Defaults to true.
|
|
796
|
+
* @returns The same statement for chaining.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* ```typescript
|
|
800
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
801
|
+
* // Returns: 42 (not { "COUNT(*)": 42 })
|
|
802
|
+
*
|
|
803
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
804
|
+
* // Returns: ["Alice", "Bob"] (not [{ name: "Alice" }, { name: "Bob" }])
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
pluck(toggle?: boolean): this;
|
|
808
|
+
/**
|
|
809
|
+
* Causes the statement to return rows as arrays of values instead of objects.
|
|
810
|
+
*
|
|
811
|
+
* When raw mode is turned on, pluck and expand modes are turned off.
|
|
812
|
+
*
|
|
813
|
+
* @param toggle Enable (true) or disable (false) raw mode. Defaults to true.
|
|
814
|
+
* @returns The same statement for chaining.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
* ```typescript
|
|
818
|
+
* const rows = db.prepare("SELECT id, name FROM users").raw().all();
|
|
819
|
+
* // Returns: [[1, "Alice"], [2, "Bob"]] (not [{ id: 1, name: "Alice" }, ...])
|
|
820
|
+
* ```
|
|
821
|
+
*/
|
|
822
|
+
raw(toggle?: boolean): this;
|
|
823
|
+
/**
|
|
824
|
+
* Causes the statement to return data namespaced by table. Each key in a row
|
|
825
|
+
* object will be a table name, and each corresponding value will be a nested
|
|
826
|
+
* object containing that table's columns. Columns from expressions or
|
|
827
|
+
* subqueries are placed under the special `$` namespace.
|
|
828
|
+
*
|
|
829
|
+
* When expand mode is turned on, pluck and raw modes are turned off.
|
|
830
|
+
*
|
|
831
|
+
* Requires the statement to have a `.columns()` method (available on real
|
|
832
|
+
* statements but not minimal mocks).
|
|
833
|
+
*
|
|
834
|
+
* @param toggle Enable (true) or disable (false) expand mode. Defaults to true.
|
|
835
|
+
* @returns The same statement for chaining.
|
|
836
|
+
*
|
|
837
|
+
* @example
|
|
838
|
+
* ```typescript
|
|
839
|
+
* const rows = db.prepare("SELECT u.id, u.name, p.title FROM users u JOIN posts p ON ...").expand().all();
|
|
840
|
+
* // Returns: [{ users: { id: 1, name: "Alice" }, posts: { title: "Hello" } }]
|
|
841
|
+
* ```
|
|
842
|
+
*/
|
|
843
|
+
expand(toggle?: boolean): this;
|
|
844
|
+
/** The database instance this statement was prepared from. */
|
|
845
|
+
readonly database: EnhanceableDatabaseSync;
|
|
846
|
+
}
|
|
782
847
|
/**
|
|
783
848
|
* Interface for an enhanced database with pragma() and transaction() methods.
|
|
784
849
|
*/
|
|
@@ -815,48 +880,45 @@ interface EnhancedMethods {
|
|
|
815
880
|
transaction<F extends (...args: any[]) => any>(fn: F): TransactionFunction<F>;
|
|
816
881
|
}
|
|
817
882
|
/**
|
|
818
|
-
* A database instance that has been enhanced with pragma()
|
|
883
|
+
* A database instance that has been enhanced with pragma(), transaction(),
|
|
884
|
+
* and statement modes (pluck/raw/expand) on statements returned by prepare().
|
|
819
885
|
*/
|
|
820
|
-
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = T & EnhancedMethods
|
|
886
|
+
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = Omit<T, "prepare"> & EnhancedMethods & {
|
|
887
|
+
prepare(...args: Parameters<T["prepare"]>): ReturnType<T["prepare"]> & EnhancedStatementMethods;
|
|
888
|
+
};
|
|
821
889
|
/**
|
|
822
|
-
* Ensures that `.pragma()
|
|
823
|
-
* given database.
|
|
890
|
+
* Ensures that `.pragma()`, `.transaction()`, and statement modes
|
|
891
|
+
* (`.pluck()`, `.raw()`, `.expand()`) are available on the given database.
|
|
824
892
|
*
|
|
825
893
|
* This function can enhance:
|
|
826
894
|
* - `node:sqlite` DatabaseSync instances (adds the methods)
|
|
827
|
-
* - `@photostructure/sqlite` DatabaseSync instances (
|
|
828
|
-
* methods)
|
|
895
|
+
* - `@photostructure/sqlite` DatabaseSync instances (adds the methods)
|
|
829
896
|
* - Any object with compatible `exec()`, `prepare()`, and `isTransaction`
|
|
830
897
|
*
|
|
831
898
|
* The enhancement is done by adding methods directly to the instance, not the
|
|
832
899
|
* prototype, so it won't affect other instances or the original class.
|
|
833
900
|
*
|
|
834
901
|
* @param db The database instance to enhance
|
|
835
|
-
* @returns The same instance with `.pragma()
|
|
836
|
-
* guaranteed
|
|
902
|
+
* @returns The same instance with `.pragma()`, `.transaction()`, and
|
|
903
|
+
* `.pluck()` / `.raw()` / `.expand()` (on prepared statements) guaranteed
|
|
837
904
|
*
|
|
838
905
|
* @example
|
|
839
906
|
* ```typescript
|
|
840
|
-
*
|
|
841
|
-
* import { DatabaseSync } from 'node:sqlite';
|
|
842
|
-
* import { enhance } from '@photostructure/sqlite';
|
|
907
|
+
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
843
908
|
*
|
|
844
909
|
* const db = enhance(new DatabaseSync(':memory:'));
|
|
845
910
|
*
|
|
846
|
-
* //
|
|
911
|
+
* // better-sqlite3-style pragma
|
|
847
912
|
* db.pragma('journal_mode = wal');
|
|
913
|
+
*
|
|
914
|
+
* // better-sqlite3-style transactions
|
|
848
915
|
* const insertMany = db.transaction((items) => {
|
|
849
916
|
* for (const item of items) insert.run(item);
|
|
850
917
|
* });
|
|
851
|
-
* ```
|
|
852
918
|
*
|
|
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
|
|
919
|
+
* // better-sqlite3-style pluck
|
|
920
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
921
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
860
922
|
* ```
|
|
861
923
|
*/
|
|
862
924
|
declare function enhance<T extends EnhanceableDatabaseSync>(db: T): EnhancedDatabaseSync<T>;
|
|
@@ -1052,4 +1114,4 @@ interface BackupOptions {
|
|
|
1052
1114
|
declare const backup: (sourceDb: DatabaseSyncInstance, destination: string | Buffer | URL, options?: BackupOptions) => Promise<number>;
|
|
1053
1115
|
declare const _default: SqliteModule;
|
|
1054
1116
|
|
|
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 };
|
|
1117
|
+
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, 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.mts
CHANGED
|
@@ -760,9 +760,9 @@ interface TransactionFunction<F extends (...args: any[]) => any> {
|
|
|
760
760
|
* compatible database, including `node:sqlite` DatabaseSync and this package's
|
|
761
761
|
* DatabaseSync.
|
|
762
762
|
*
|
|
763
|
-
* This module provides the `enhance()` function which adds `.pragma()
|
|
764
|
-
* `.transaction()
|
|
765
|
-
* node:sqlite DatabaseSync).
|
|
763
|
+
* This module provides the `enhance()` function which adds `.pragma()`,
|
|
764
|
+
* `.transaction()`, and statement modes (`.pluck()`, `.raw()`, `.expand()`)
|
|
765
|
+
* to database instances that don't have them (e.g., node:sqlite DatabaseSync).
|
|
766
766
|
*/
|
|
767
767
|
|
|
768
768
|
/**
|
|
@@ -776,9 +776,74 @@ interface EnhanceableDatabaseSync {
|
|
|
776
776
|
prepare(sql: string): {
|
|
777
777
|
all(): unknown[];
|
|
778
778
|
};
|
|
779
|
+
/** Whether the database connection is open */
|
|
780
|
+
readonly isOpen?: boolean;
|
|
779
781
|
/** Whether a transaction is currently active */
|
|
780
782
|
readonly isTransaction: boolean;
|
|
781
783
|
}
|
|
784
|
+
/**
|
|
785
|
+
* A statement enhanced with better-sqlite3-style `.pluck()`, `.raw()`, and
|
|
786
|
+
* `.expand()` methods. These are mutually exclusive — enabling one disables
|
|
787
|
+
* the others.
|
|
788
|
+
*/
|
|
789
|
+
interface EnhancedStatementMethods {
|
|
790
|
+
/**
|
|
791
|
+
* Causes the statement to return only the first column value of each row.
|
|
792
|
+
*
|
|
793
|
+
* When plucking is turned on, raw and expand modes are turned off.
|
|
794
|
+
*
|
|
795
|
+
* @param toggle Enable (true) or disable (false) pluck mode. Defaults to true.
|
|
796
|
+
* @returns The same statement for chaining.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* ```typescript
|
|
800
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
801
|
+
* // Returns: 42 (not { "COUNT(*)": 42 })
|
|
802
|
+
*
|
|
803
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
804
|
+
* // Returns: ["Alice", "Bob"] (not [{ name: "Alice" }, { name: "Bob" }])
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
pluck(toggle?: boolean): this;
|
|
808
|
+
/**
|
|
809
|
+
* Causes the statement to return rows as arrays of values instead of objects.
|
|
810
|
+
*
|
|
811
|
+
* When raw mode is turned on, pluck and expand modes are turned off.
|
|
812
|
+
*
|
|
813
|
+
* @param toggle Enable (true) or disable (false) raw mode. Defaults to true.
|
|
814
|
+
* @returns The same statement for chaining.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
* ```typescript
|
|
818
|
+
* const rows = db.prepare("SELECT id, name FROM users").raw().all();
|
|
819
|
+
* // Returns: [[1, "Alice"], [2, "Bob"]] (not [{ id: 1, name: "Alice" }, ...])
|
|
820
|
+
* ```
|
|
821
|
+
*/
|
|
822
|
+
raw(toggle?: boolean): this;
|
|
823
|
+
/**
|
|
824
|
+
* Causes the statement to return data namespaced by table. Each key in a row
|
|
825
|
+
* object will be a table name, and each corresponding value will be a nested
|
|
826
|
+
* object containing that table's columns. Columns from expressions or
|
|
827
|
+
* subqueries are placed under the special `$` namespace.
|
|
828
|
+
*
|
|
829
|
+
* When expand mode is turned on, pluck and raw modes are turned off.
|
|
830
|
+
*
|
|
831
|
+
* Requires the statement to have a `.columns()` method (available on real
|
|
832
|
+
* statements but not minimal mocks).
|
|
833
|
+
*
|
|
834
|
+
* @param toggle Enable (true) or disable (false) expand mode. Defaults to true.
|
|
835
|
+
* @returns The same statement for chaining.
|
|
836
|
+
*
|
|
837
|
+
* @example
|
|
838
|
+
* ```typescript
|
|
839
|
+
* const rows = db.prepare("SELECT u.id, u.name, p.title FROM users u JOIN posts p ON ...").expand().all();
|
|
840
|
+
* // Returns: [{ users: { id: 1, name: "Alice" }, posts: { title: "Hello" } }]
|
|
841
|
+
* ```
|
|
842
|
+
*/
|
|
843
|
+
expand(toggle?: boolean): this;
|
|
844
|
+
/** The database instance this statement was prepared from. */
|
|
845
|
+
readonly database: EnhanceableDatabaseSync;
|
|
846
|
+
}
|
|
782
847
|
/**
|
|
783
848
|
* Interface for an enhanced database with pragma() and transaction() methods.
|
|
784
849
|
*/
|
|
@@ -815,48 +880,45 @@ interface EnhancedMethods {
|
|
|
815
880
|
transaction<F extends (...args: any[]) => any>(fn: F): TransactionFunction<F>;
|
|
816
881
|
}
|
|
817
882
|
/**
|
|
818
|
-
* A database instance that has been enhanced with pragma()
|
|
883
|
+
* A database instance that has been enhanced with pragma(), transaction(),
|
|
884
|
+
* and statement modes (pluck/raw/expand) on statements returned by prepare().
|
|
819
885
|
*/
|
|
820
|
-
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = T & EnhancedMethods
|
|
886
|
+
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = Omit<T, "prepare"> & EnhancedMethods & {
|
|
887
|
+
prepare(...args: Parameters<T["prepare"]>): ReturnType<T["prepare"]> & EnhancedStatementMethods;
|
|
888
|
+
};
|
|
821
889
|
/**
|
|
822
|
-
* Ensures that `.pragma()
|
|
823
|
-
* given database.
|
|
890
|
+
* Ensures that `.pragma()`, `.transaction()`, and statement modes
|
|
891
|
+
* (`.pluck()`, `.raw()`, `.expand()`) are available on the given database.
|
|
824
892
|
*
|
|
825
893
|
* This function can enhance:
|
|
826
894
|
* - `node:sqlite` DatabaseSync instances (adds the methods)
|
|
827
|
-
* - `@photostructure/sqlite` DatabaseSync instances (
|
|
828
|
-
* methods)
|
|
895
|
+
* - `@photostructure/sqlite` DatabaseSync instances (adds the methods)
|
|
829
896
|
* - Any object with compatible `exec()`, `prepare()`, and `isTransaction`
|
|
830
897
|
*
|
|
831
898
|
* The enhancement is done by adding methods directly to the instance, not the
|
|
832
899
|
* prototype, so it won't affect other instances or the original class.
|
|
833
900
|
*
|
|
834
901
|
* @param db The database instance to enhance
|
|
835
|
-
* @returns The same instance with `.pragma()
|
|
836
|
-
* guaranteed
|
|
902
|
+
* @returns The same instance with `.pragma()`, `.transaction()`, and
|
|
903
|
+
* `.pluck()` / `.raw()` / `.expand()` (on prepared statements) guaranteed
|
|
837
904
|
*
|
|
838
905
|
* @example
|
|
839
906
|
* ```typescript
|
|
840
|
-
*
|
|
841
|
-
* import { DatabaseSync } from 'node:sqlite';
|
|
842
|
-
* import { enhance } from '@photostructure/sqlite';
|
|
907
|
+
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
843
908
|
*
|
|
844
909
|
* const db = enhance(new DatabaseSync(':memory:'));
|
|
845
910
|
*
|
|
846
|
-
* //
|
|
911
|
+
* // better-sqlite3-style pragma
|
|
847
912
|
* db.pragma('journal_mode = wal');
|
|
913
|
+
*
|
|
914
|
+
* // better-sqlite3-style transactions
|
|
848
915
|
* const insertMany = db.transaction((items) => {
|
|
849
916
|
* for (const item of items) insert.run(item);
|
|
850
917
|
* });
|
|
851
|
-
* ```
|
|
852
918
|
*
|
|
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
|
|
919
|
+
* // better-sqlite3-style pluck
|
|
920
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
921
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
860
922
|
* ```
|
|
861
923
|
*/
|
|
862
924
|
declare function enhance<T extends EnhanceableDatabaseSync>(db: T): EnhancedDatabaseSync<T>;
|
|
@@ -1052,4 +1114,4 @@ interface BackupOptions {
|
|
|
1052
1114
|
declare const backup: (sourceDb: DatabaseSyncInstance, destination: string | Buffer | URL, options?: BackupOptions) => Promise<number>;
|
|
1053
1115
|
declare const _default: SqliteModule;
|
|
1054
1116
|
|
|
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 };
|
|
1117
|
+
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, 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
|
@@ -760,9 +760,9 @@ interface TransactionFunction<F extends (...args: any[]) => any> {
|
|
|
760
760
|
* compatible database, including `node:sqlite` DatabaseSync and this package's
|
|
761
761
|
* DatabaseSync.
|
|
762
762
|
*
|
|
763
|
-
* This module provides the `enhance()` function which adds `.pragma()
|
|
764
|
-
* `.transaction()
|
|
765
|
-
* node:sqlite DatabaseSync).
|
|
763
|
+
* This module provides the `enhance()` function which adds `.pragma()`,
|
|
764
|
+
* `.transaction()`, and statement modes (`.pluck()`, `.raw()`, `.expand()`)
|
|
765
|
+
* to database instances that don't have them (e.g., node:sqlite DatabaseSync).
|
|
766
766
|
*/
|
|
767
767
|
|
|
768
768
|
/**
|
|
@@ -776,9 +776,74 @@ interface EnhanceableDatabaseSync {
|
|
|
776
776
|
prepare(sql: string): {
|
|
777
777
|
all(): unknown[];
|
|
778
778
|
};
|
|
779
|
+
/** Whether the database connection is open */
|
|
780
|
+
readonly isOpen?: boolean;
|
|
779
781
|
/** Whether a transaction is currently active */
|
|
780
782
|
readonly isTransaction: boolean;
|
|
781
783
|
}
|
|
784
|
+
/**
|
|
785
|
+
* A statement enhanced with better-sqlite3-style `.pluck()`, `.raw()`, and
|
|
786
|
+
* `.expand()` methods. These are mutually exclusive — enabling one disables
|
|
787
|
+
* the others.
|
|
788
|
+
*/
|
|
789
|
+
interface EnhancedStatementMethods {
|
|
790
|
+
/**
|
|
791
|
+
* Causes the statement to return only the first column value of each row.
|
|
792
|
+
*
|
|
793
|
+
* When plucking is turned on, raw and expand modes are turned off.
|
|
794
|
+
*
|
|
795
|
+
* @param toggle Enable (true) or disable (false) pluck mode. Defaults to true.
|
|
796
|
+
* @returns The same statement for chaining.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* ```typescript
|
|
800
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
801
|
+
* // Returns: 42 (not { "COUNT(*)": 42 })
|
|
802
|
+
*
|
|
803
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
804
|
+
* // Returns: ["Alice", "Bob"] (not [{ name: "Alice" }, { name: "Bob" }])
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
pluck(toggle?: boolean): this;
|
|
808
|
+
/**
|
|
809
|
+
* Causes the statement to return rows as arrays of values instead of objects.
|
|
810
|
+
*
|
|
811
|
+
* When raw mode is turned on, pluck and expand modes are turned off.
|
|
812
|
+
*
|
|
813
|
+
* @param toggle Enable (true) or disable (false) raw mode. Defaults to true.
|
|
814
|
+
* @returns The same statement for chaining.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
* ```typescript
|
|
818
|
+
* const rows = db.prepare("SELECT id, name FROM users").raw().all();
|
|
819
|
+
* // Returns: [[1, "Alice"], [2, "Bob"]] (not [{ id: 1, name: "Alice" }, ...])
|
|
820
|
+
* ```
|
|
821
|
+
*/
|
|
822
|
+
raw(toggle?: boolean): this;
|
|
823
|
+
/**
|
|
824
|
+
* Causes the statement to return data namespaced by table. Each key in a row
|
|
825
|
+
* object will be a table name, and each corresponding value will be a nested
|
|
826
|
+
* object containing that table's columns. Columns from expressions or
|
|
827
|
+
* subqueries are placed under the special `$` namespace.
|
|
828
|
+
*
|
|
829
|
+
* When expand mode is turned on, pluck and raw modes are turned off.
|
|
830
|
+
*
|
|
831
|
+
* Requires the statement to have a `.columns()` method (available on real
|
|
832
|
+
* statements but not minimal mocks).
|
|
833
|
+
*
|
|
834
|
+
* @param toggle Enable (true) or disable (false) expand mode. Defaults to true.
|
|
835
|
+
* @returns The same statement for chaining.
|
|
836
|
+
*
|
|
837
|
+
* @example
|
|
838
|
+
* ```typescript
|
|
839
|
+
* const rows = db.prepare("SELECT u.id, u.name, p.title FROM users u JOIN posts p ON ...").expand().all();
|
|
840
|
+
* // Returns: [{ users: { id: 1, name: "Alice" }, posts: { title: "Hello" } }]
|
|
841
|
+
* ```
|
|
842
|
+
*/
|
|
843
|
+
expand(toggle?: boolean): this;
|
|
844
|
+
/** The database instance this statement was prepared from. */
|
|
845
|
+
readonly database: EnhanceableDatabaseSync;
|
|
846
|
+
}
|
|
782
847
|
/**
|
|
783
848
|
* Interface for an enhanced database with pragma() and transaction() methods.
|
|
784
849
|
*/
|
|
@@ -815,48 +880,45 @@ interface EnhancedMethods {
|
|
|
815
880
|
transaction<F extends (...args: any[]) => any>(fn: F): TransactionFunction<F>;
|
|
816
881
|
}
|
|
817
882
|
/**
|
|
818
|
-
* A database instance that has been enhanced with pragma()
|
|
883
|
+
* A database instance that has been enhanced with pragma(), transaction(),
|
|
884
|
+
* and statement modes (pluck/raw/expand) on statements returned by prepare().
|
|
819
885
|
*/
|
|
820
|
-
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = T & EnhancedMethods
|
|
886
|
+
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = Omit<T, "prepare"> & EnhancedMethods & {
|
|
887
|
+
prepare(...args: Parameters<T["prepare"]>): ReturnType<T["prepare"]> & EnhancedStatementMethods;
|
|
888
|
+
};
|
|
821
889
|
/**
|
|
822
|
-
* Ensures that `.pragma()
|
|
823
|
-
* given database.
|
|
890
|
+
* Ensures that `.pragma()`, `.transaction()`, and statement modes
|
|
891
|
+
* (`.pluck()`, `.raw()`, `.expand()`) are available on the given database.
|
|
824
892
|
*
|
|
825
893
|
* This function can enhance:
|
|
826
894
|
* - `node:sqlite` DatabaseSync instances (adds the methods)
|
|
827
|
-
* - `@photostructure/sqlite` DatabaseSync instances (
|
|
828
|
-
* methods)
|
|
895
|
+
* - `@photostructure/sqlite` DatabaseSync instances (adds the methods)
|
|
829
896
|
* - Any object with compatible `exec()`, `prepare()`, and `isTransaction`
|
|
830
897
|
*
|
|
831
898
|
* The enhancement is done by adding methods directly to the instance, not the
|
|
832
899
|
* prototype, so it won't affect other instances or the original class.
|
|
833
900
|
*
|
|
834
901
|
* @param db The database instance to enhance
|
|
835
|
-
* @returns The same instance with `.pragma()
|
|
836
|
-
* guaranteed
|
|
902
|
+
* @returns The same instance with `.pragma()`, `.transaction()`, and
|
|
903
|
+
* `.pluck()` / `.raw()` / `.expand()` (on prepared statements) guaranteed
|
|
837
904
|
*
|
|
838
905
|
* @example
|
|
839
906
|
* ```typescript
|
|
840
|
-
*
|
|
841
|
-
* import { DatabaseSync } from 'node:sqlite';
|
|
842
|
-
* import { enhance } from '@photostructure/sqlite';
|
|
907
|
+
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
843
908
|
*
|
|
844
909
|
* const db = enhance(new DatabaseSync(':memory:'));
|
|
845
910
|
*
|
|
846
|
-
* //
|
|
911
|
+
* // better-sqlite3-style pragma
|
|
847
912
|
* db.pragma('journal_mode = wal');
|
|
913
|
+
*
|
|
914
|
+
* // better-sqlite3-style transactions
|
|
848
915
|
* const insertMany = db.transaction((items) => {
|
|
849
916
|
* for (const item of items) insert.run(item);
|
|
850
917
|
* });
|
|
851
|
-
* ```
|
|
852
918
|
*
|
|
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
|
|
919
|
+
* // better-sqlite3-style pluck
|
|
920
|
+
* const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
|
|
921
|
+
* const names = db.prepare("SELECT name FROM users").pluck().all();
|
|
860
922
|
* ```
|
|
861
923
|
*/
|
|
862
924
|
declare function enhance<T extends EnhanceableDatabaseSync>(db: T): EnhancedDatabaseSync<T>;
|
|
@@ -1052,4 +1114,4 @@ interface BackupOptions {
|
|
|
1052
1114
|
declare const backup: (sourceDb: DatabaseSyncInstance, destination: string | Buffer | URL, options?: BackupOptions) => Promise<number>;
|
|
1053
1115
|
declare const _default: SqliteModule;
|
|
1054
1116
|
|
|
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 };
|
|
1117
|
+
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, 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 };
|