@photostructure/sqlite 0.3.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 +65 -16
- package/README.md +5 -10
- package/binding.gyp +2 -2
- package/dist/index.cjs +314 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +346 -89
- package/dist/index.d.mts +346 -89
- package/dist/index.d.ts +346 -89
- package/dist/index.mjs +311 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +72 -63
- 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/aggregate_function.cpp +222 -114
- package/src/aggregate_function.h +5 -6
- package/src/binding.cpp +30 -21
- package/src/enhance.ts +552 -0
- package/src/index.ts +84 -9
- package/src/shims/node_errors.h +34 -15
- package/src/shims/sqlite_errors.h +34 -8
- package/src/sql-tag-store.ts +6 -9
- package/src/sqlite_impl.cpp +1044 -394
- package/src/sqlite_impl.h +46 -7
- package/src/transaction.ts +178 -0
- package/src/types/database-sync-instance.ts +6 -40
- package/src/types/pragma-options.ts +23 -0
- package/src/types/statement-sync-instance.ts +38 -12
- package/src/types/transaction.ts +72 -0
- package/src/upstream/node_sqlite.cc +143 -43
- package/src/upstream/node_sqlite.h +15 -11
- package/src/upstream/sqlite3.c +102 -58
- package/src/upstream/sqlite3.h +5 -5
- package/src/user_function.cpp +138 -141
- package/src/user_function.h +3 -0
package/dist/index.d.mts
CHANGED
|
@@ -98,6 +98,41 @@ interface SQLTagStoreInstance {
|
|
|
98
98
|
iterate(strings: TemplateStringsArray, ...values: unknown[]): IterableIterator<unknown>;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Metadata about a column in a prepared statement's result set.
|
|
103
|
+
* Matches Node.js sqlite module's StatementColumnMetadata.
|
|
104
|
+
*/
|
|
105
|
+
interface StatementColumnMetadata {
|
|
106
|
+
/**
|
|
107
|
+
* The unaliased name of the column in the origin table, or `null` if the
|
|
108
|
+
* column is the result of an expression or subquery.
|
|
109
|
+
* This property is the result of `sqlite3_column_origin_name()`.
|
|
110
|
+
*/
|
|
111
|
+
column: string | null;
|
|
112
|
+
/**
|
|
113
|
+
* The unaliased name of the origin database, or `null` if the column is
|
|
114
|
+
* the result of an expression or subquery.
|
|
115
|
+
* This property is the result of `sqlite3_column_database_name()`.
|
|
116
|
+
*/
|
|
117
|
+
database: string | null;
|
|
118
|
+
/**
|
|
119
|
+
* The name assigned to the column in the result set of a SELECT statement.
|
|
120
|
+
* This property is the result of `sqlite3_column_name()`.
|
|
121
|
+
*/
|
|
122
|
+
name: string;
|
|
123
|
+
/**
|
|
124
|
+
* The unaliased name of the origin table, or `null` if the column is
|
|
125
|
+
* the result of an expression or subquery.
|
|
126
|
+
* This property is the result of `sqlite3_column_table_name()`.
|
|
127
|
+
*/
|
|
128
|
+
table: string | null;
|
|
129
|
+
/**
|
|
130
|
+
* The declared data type of the column, or `null` if the column is
|
|
131
|
+
* the result of an expression or subquery.
|
|
132
|
+
* This property is the result of `sqlite3_column_decltype()`.
|
|
133
|
+
*/
|
|
134
|
+
type: string | null;
|
|
135
|
+
}
|
|
101
136
|
/**
|
|
102
137
|
* A prepared SQL statement that can be executed multiple times with different parameters.
|
|
103
138
|
* This interface represents an instance of the StatementSync class.
|
|
@@ -107,8 +142,6 @@ interface StatementSyncInstance {
|
|
|
107
142
|
readonly sourceSQL: string;
|
|
108
143
|
/** The expanded SQL string with bound parameters, if expandedSQL option was set. */
|
|
109
144
|
readonly expandedSQL: string | undefined;
|
|
110
|
-
/** Whether this statement has been finalized. */
|
|
111
|
-
readonly finalized: boolean;
|
|
112
145
|
/**
|
|
113
146
|
* This method executes a prepared statement and returns an object.
|
|
114
147
|
* @param parameters Optional named and anonymous parameters to bind to the statement.
|
|
@@ -159,20 +192,9 @@ interface StatementSyncInstance {
|
|
|
159
192
|
setReturnArrays(returnArrays: boolean): void;
|
|
160
193
|
/**
|
|
161
194
|
* Returns an array of objects, each representing a column in the statement's result set.
|
|
162
|
-
*
|
|
163
|
-
* @returns Array of column metadata objects.
|
|
164
|
-
*/
|
|
165
|
-
columns(): Array<{
|
|
166
|
-
name: string;
|
|
167
|
-
type?: string;
|
|
168
|
-
}>;
|
|
169
|
-
/**
|
|
170
|
-
* Finalizes the prepared statement and releases its resources.
|
|
171
|
-
* Called automatically by Symbol.dispose.
|
|
195
|
+
* @returns Array of column metadata objects with name, column, database, table, and type.
|
|
172
196
|
*/
|
|
173
|
-
|
|
174
|
-
/** Dispose of the statement resources using the explicit resource management protocol. */
|
|
175
|
-
[Symbol.dispose](): void;
|
|
197
|
+
columns(): StatementColumnMetadata[];
|
|
176
198
|
}
|
|
177
199
|
|
|
178
200
|
/**
|
|
@@ -274,7 +296,7 @@ interface DatabaseSyncInstance {
|
|
|
274
296
|
* @param options Optional configuration for applying the changeset.
|
|
275
297
|
* @returns true if successful, false if aborted.
|
|
276
298
|
*/
|
|
277
|
-
applyChangeset(changeset:
|
|
299
|
+
applyChangeset(changeset: Uint8Array, options?: ChangesetApplyOptions): boolean;
|
|
278
300
|
/**
|
|
279
301
|
* Enables or disables the loading of SQLite extensions.
|
|
280
302
|
* @param enable If true, enables extension loading. If false, disables it.
|
|
@@ -327,44 +349,6 @@ interface DatabaseSyncInstance {
|
|
|
327
349
|
* @see https://sqlite.org/c3ref/set_authorizer.html
|
|
328
350
|
*/
|
|
329
351
|
setAuthorizer(callback: ((actionCode: number, param1: string | null, param2: string | null, param3: string | null, param4: string | null) => number) | null): void;
|
|
330
|
-
/**
|
|
331
|
-
* Makes a backup of the database. This method abstracts the sqlite3_backup_init(),
|
|
332
|
-
* sqlite3_backup_step() and sqlite3_backup_finish() functions.
|
|
333
|
-
*
|
|
334
|
-
* The backed-up database can be used normally during the backup process. Mutations
|
|
335
|
-
* coming from the same connection will be reflected in the backup right away.
|
|
336
|
-
* However, mutations from other connections will cause the backup process to restart.
|
|
337
|
-
*
|
|
338
|
-
* @param path The path where the backup will be created. If the file already exists, the contents will be overwritten.
|
|
339
|
-
* @param options Optional configuration for the backup operation.
|
|
340
|
-
* @param options.rate Number of pages to be transmitted in each batch of the backup. @default 100
|
|
341
|
-
* @param options.source Name of the source database. This can be 'main' (the default primary database) or any other database that have been added with ATTACH DATABASE. @default 'main'
|
|
342
|
-
* @param options.target Name of the target database. This can be 'main' (the default primary database) or any other database that have been added with ATTACH DATABASE. @default 'main'
|
|
343
|
-
* @param options.progress Callback function that will be called with the number of pages copied and the total number of pages.
|
|
344
|
-
* @returns A promise that resolves when the backup is completed and rejects if an error occurs.
|
|
345
|
-
*
|
|
346
|
-
* @example
|
|
347
|
-
* // Basic backup
|
|
348
|
-
* await db.backup('./backup.db');
|
|
349
|
-
*
|
|
350
|
-
* @example
|
|
351
|
-
* // Backup with progress
|
|
352
|
-
* await db.backup('./backup.db', {
|
|
353
|
-
* rate: 10,
|
|
354
|
-
* progress: ({ totalPages, remainingPages }) => {
|
|
355
|
-
* console.log(`Progress: ${totalPages - remainingPages}/${totalPages}`);
|
|
356
|
-
* }
|
|
357
|
-
* });
|
|
358
|
-
*/
|
|
359
|
-
backup(path: string | Buffer | URL, options?: {
|
|
360
|
-
rate?: number;
|
|
361
|
-
source?: string;
|
|
362
|
-
target?: string;
|
|
363
|
-
progress?: (info: {
|
|
364
|
-
totalPages: number;
|
|
365
|
-
remainingPages: number;
|
|
366
|
-
}) => void;
|
|
367
|
-
}): Promise<number>;
|
|
368
352
|
/** Dispose of the database resources using the explicit resource management protocol. */
|
|
369
353
|
[Symbol.dispose](): void;
|
|
370
354
|
}
|
|
@@ -422,7 +406,6 @@ declare class SQLTagStore {
|
|
|
422
406
|
iterate(strings: TemplateStringsArray, ...values: unknown[]): IterableIterator<unknown>;
|
|
423
407
|
/**
|
|
424
408
|
* Get a cached statement or prepare a new one.
|
|
425
|
-
* If a cached statement has been finalized, it's evicted and a new one is prepared.
|
|
426
409
|
*/
|
|
427
410
|
private getOrPrepare;
|
|
428
411
|
/**
|
|
@@ -682,6 +665,280 @@ interface SqliteOpenFlags {
|
|
|
682
665
|
SQLITE_OPEN_WAL: number;
|
|
683
666
|
}
|
|
684
667
|
|
|
668
|
+
/**
|
|
669
|
+
* Options for the pragma() method.
|
|
670
|
+
*
|
|
671
|
+
* @see https://sqlite.org/pragma.html
|
|
672
|
+
*/
|
|
673
|
+
interface PragmaOptions {
|
|
674
|
+
/**
|
|
675
|
+
* When true, returns only the first column of the first row.
|
|
676
|
+
* This is useful for pragmas that return a single value.
|
|
677
|
+
*
|
|
678
|
+
* @default false
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* ```typescript
|
|
682
|
+
* // Without simple: returns [{ cache_size: -16000 }]
|
|
683
|
+
* db.pragma('cache_size');
|
|
684
|
+
*
|
|
685
|
+
* // With simple: returns -16000
|
|
686
|
+
* db.pragma('cache_size', { simple: true });
|
|
687
|
+
* ```
|
|
688
|
+
*/
|
|
689
|
+
readonly simple?: boolean;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Transaction isolation modes supported by SQLite.
|
|
694
|
+
*
|
|
695
|
+
* @see https://sqlite.org/lang_transaction.html
|
|
696
|
+
*/
|
|
697
|
+
type TransactionMode = "deferred" | "immediate" | "exclusive";
|
|
698
|
+
/**
|
|
699
|
+
* A function wrapped in a transaction. When called, it automatically:
|
|
700
|
+
* - Begins a transaction (or savepoint if nested)
|
|
701
|
+
* - Executes the wrapped function
|
|
702
|
+
* - Commits on success, rolls back on error
|
|
703
|
+
*
|
|
704
|
+
* The function also has variants for different transaction modes accessible as
|
|
705
|
+
* properties (`.deferred`, `.immediate`, `.exclusive`).
|
|
706
|
+
*
|
|
707
|
+
* **Note:** Each variant is itself a `TransactionFunction` with circular
|
|
708
|
+
* references to all other variants. This matches better-sqlite3's behavior
|
|
709
|
+
* where you can chain variants like `txn.deferred.immediate()` - the last
|
|
710
|
+
* variant in the chain determines the actual transaction mode used.
|
|
711
|
+
*
|
|
712
|
+
* @example
|
|
713
|
+
* ```typescript
|
|
714
|
+
* const insertMany = db.transaction((items: Item[]) => {
|
|
715
|
+
* for (const item of items) insert.run(item);
|
|
716
|
+
* return items.length;
|
|
717
|
+
* });
|
|
718
|
+
*
|
|
719
|
+
* // Use default mode (DEFERRED)
|
|
720
|
+
* insertMany([{ id: 1 }, { id: 2 }]);
|
|
721
|
+
*
|
|
722
|
+
* // Use IMMEDIATE mode for write transactions
|
|
723
|
+
* insertMany.immediate([{ id: 3 }, { id: 4 }]);
|
|
724
|
+
*
|
|
725
|
+
* // Use EXCLUSIVE mode for exclusive access
|
|
726
|
+
* insertMany.exclusive([{ id: 5 }, { id: 6 }]);
|
|
727
|
+
* ```
|
|
728
|
+
*/
|
|
729
|
+
interface TransactionFunction<F extends (...args: any[]) => any> {
|
|
730
|
+
/**
|
|
731
|
+
* Execute the wrapped function within a transaction using the default mode (DEFERRED).
|
|
732
|
+
*/
|
|
733
|
+
(...args: Parameters<F>): ReturnType<F>;
|
|
734
|
+
/**
|
|
735
|
+
* Execute with DEFERRED transaction mode.
|
|
736
|
+
* The database lock is not acquired until the first read or write operation.
|
|
737
|
+
* This is the default SQLite behavior.
|
|
738
|
+
*/
|
|
739
|
+
readonly deferred: TransactionFunction<F>;
|
|
740
|
+
/**
|
|
741
|
+
* Execute with IMMEDIATE transaction mode.
|
|
742
|
+
* Acquires a write lock immediately, blocking other writers.
|
|
743
|
+
* Recommended for transactions that will write to prevent SQLITE_BUSY errors.
|
|
744
|
+
*/
|
|
745
|
+
readonly immediate: TransactionFunction<F>;
|
|
746
|
+
/**
|
|
747
|
+
* Execute with EXCLUSIVE transaction mode.
|
|
748
|
+
* Acquires an exclusive lock immediately, blocking all other connections.
|
|
749
|
+
* Use sparingly as it prevents all concurrent access.
|
|
750
|
+
*/
|
|
751
|
+
readonly exclusive: TransactionFunction<F>;
|
|
752
|
+
/**
|
|
753
|
+
* The database connection this transaction function is bound to.
|
|
754
|
+
*/
|
|
755
|
+
readonly database: DatabaseSyncInstance;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Enhancement utilities for adding better-sqlite3-style methods to any
|
|
760
|
+
* compatible database, including `node:sqlite` DatabaseSync and this package's
|
|
761
|
+
* DatabaseSync.
|
|
762
|
+
*
|
|
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
|
+
*/
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Minimal interface for a database that can be enhanced. This matches the
|
|
770
|
+
* subset of functionality needed by pragma() and transaction().
|
|
771
|
+
*/
|
|
772
|
+
interface EnhanceableDatabaseSync {
|
|
773
|
+
/** Execute SQL without returning results */
|
|
774
|
+
exec(sql: string): void;
|
|
775
|
+
/** Prepare a statement that can return results */
|
|
776
|
+
prepare(sql: string): {
|
|
777
|
+
all(): unknown[];
|
|
778
|
+
};
|
|
779
|
+
/** Whether the database connection is open */
|
|
780
|
+
readonly isOpen?: boolean;
|
|
781
|
+
/** Whether a transaction is currently active */
|
|
782
|
+
readonly isTransaction: boolean;
|
|
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
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Interface for an enhanced database with pragma() and transaction() methods.
|
|
849
|
+
*/
|
|
850
|
+
interface EnhancedMethods {
|
|
851
|
+
/**
|
|
852
|
+
* Executes a PRAGMA statement and returns its result.
|
|
853
|
+
*
|
|
854
|
+
* @param source The PRAGMA command (without "PRAGMA" prefix)
|
|
855
|
+
* @param options Optional configuration
|
|
856
|
+
* @returns Array of rows, or single value if `simple: true`
|
|
857
|
+
*
|
|
858
|
+
* @example
|
|
859
|
+
* ```typescript
|
|
860
|
+
* db.pragma('cache_size', { simple: true }); // -16000
|
|
861
|
+
* db.pragma('journal_mode = wal');
|
|
862
|
+
* ```
|
|
863
|
+
*/
|
|
864
|
+
pragma(source: string, options?: PragmaOptions): unknown;
|
|
865
|
+
/**
|
|
866
|
+
* Creates a function that always runs inside a transaction.
|
|
867
|
+
*
|
|
868
|
+
* @param fn The function to wrap in a transaction
|
|
869
|
+
* @returns A transaction function with `.deferred`, `.immediate`,
|
|
870
|
+
* `.exclusive` variants
|
|
871
|
+
*
|
|
872
|
+
* @example
|
|
873
|
+
* ```typescript
|
|
874
|
+
* const insertMany = db.transaction((items) => {
|
|
875
|
+
* for (const item of items) insert.run(item);
|
|
876
|
+
* });
|
|
877
|
+
* insertMany(['a', 'b', 'c']); // All in one transaction
|
|
878
|
+
* ```
|
|
879
|
+
*/
|
|
880
|
+
transaction<F extends (...args: any[]) => any>(fn: F): TransactionFunction<F>;
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* A database instance that has been enhanced with pragma(), transaction(),
|
|
884
|
+
* and statement modes (pluck/raw/expand) on statements returned by prepare().
|
|
885
|
+
*/
|
|
886
|
+
type EnhancedDatabaseSync<T extends EnhanceableDatabaseSync> = Omit<T, "prepare"> & EnhancedMethods & {
|
|
887
|
+
prepare(...args: Parameters<T["prepare"]>): ReturnType<T["prepare"]> & EnhancedStatementMethods;
|
|
888
|
+
};
|
|
889
|
+
/**
|
|
890
|
+
* Ensures that `.pragma()`, `.transaction()`, and statement modes
|
|
891
|
+
* (`.pluck()`, `.raw()`, `.expand()`) are available on the given database.
|
|
892
|
+
*
|
|
893
|
+
* This function can enhance:
|
|
894
|
+
* - `node:sqlite` DatabaseSync instances (adds the methods)
|
|
895
|
+
* - `@photostructure/sqlite` DatabaseSync instances (adds the methods)
|
|
896
|
+
* - Any object with compatible `exec()`, `prepare()`, and `isTransaction`
|
|
897
|
+
*
|
|
898
|
+
* The enhancement is done by adding methods directly to the instance, not the
|
|
899
|
+
* prototype, so it won't affect other instances or the original class.
|
|
900
|
+
*
|
|
901
|
+
* @param db The database instance to enhance
|
|
902
|
+
* @returns The same instance with `.pragma()`, `.transaction()`, and
|
|
903
|
+
* `.pluck()` / `.raw()` / `.expand()` (on prepared statements) guaranteed
|
|
904
|
+
*
|
|
905
|
+
* @example
|
|
906
|
+
* ```typescript
|
|
907
|
+
* import { DatabaseSync, enhance } from '@photostructure/sqlite';
|
|
908
|
+
*
|
|
909
|
+
* const db = enhance(new DatabaseSync(':memory:'));
|
|
910
|
+
*
|
|
911
|
+
* // better-sqlite3-style pragma
|
|
912
|
+
* db.pragma('journal_mode = wal');
|
|
913
|
+
*
|
|
914
|
+
* // better-sqlite3-style transactions
|
|
915
|
+
* const insertMany = db.transaction((items) => {
|
|
916
|
+
* for (const item of items) insert.run(item);
|
|
917
|
+
* });
|
|
918
|
+
*
|
|
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();
|
|
922
|
+
* ```
|
|
923
|
+
*/
|
|
924
|
+
declare function enhance<T extends EnhanceableDatabaseSync>(db: T): EnhancedDatabaseSync<T>;
|
|
925
|
+
/**
|
|
926
|
+
* Type guard to check if a database has enhanced methods.
|
|
927
|
+
*
|
|
928
|
+
* @param db The database to check
|
|
929
|
+
* @returns True if the database has `.pragma()` and `.transaction()` methods
|
|
930
|
+
*
|
|
931
|
+
* @example
|
|
932
|
+
* ```typescript
|
|
933
|
+
* import { isEnhanced } from '@photostructure/sqlite';
|
|
934
|
+
*
|
|
935
|
+
* if (isEnhanced(db)) {
|
|
936
|
+
* db.pragma('cache_size', { simple: true });
|
|
937
|
+
* }
|
|
938
|
+
* ```
|
|
939
|
+
*/
|
|
940
|
+
declare function isEnhanced(db: EnhanceableDatabaseSync): db is EnhanceableDatabaseSync & EnhancedMethods;
|
|
941
|
+
|
|
685
942
|
/**
|
|
686
943
|
* All SQLite constants exported by this module.
|
|
687
944
|
*
|
|
@@ -699,12 +956,41 @@ interface SqliteOpenFlags {
|
|
|
699
956
|
type SqliteConstants = SqliteOpenFlags & SqliteChangesetResolution & SqliteChangesetConflictTypes & SqliteAuthorizationResults & SqliteAuthorizationActions;
|
|
700
957
|
/**
|
|
701
958
|
* Options for creating a prepared statement.
|
|
959
|
+
*
|
|
960
|
+
* **Note:** The per-statement override options (`readBigInts`, `returnArrays`,
|
|
961
|
+
* `allowBareNamedParameters`, `allowUnknownNamedParameters`) are a **Node.js v25+**
|
|
962
|
+
* feature. On Node.js v24 and earlier, `node:sqlite` silently ignores these options.
|
|
963
|
+
* This library implements them for forward compatibility with Node.js v25+.
|
|
702
964
|
*/
|
|
703
965
|
interface StatementOptions {
|
|
704
966
|
/** If true, the prepared statement's expandedSQL property will contain the expanded SQL. @default false */
|
|
705
967
|
readonly expandedSQL?: boolean;
|
|
706
968
|
/** If true, anonymous parameters are enabled for the statement. @default false */
|
|
707
969
|
readonly anonymousParameters?: boolean;
|
|
970
|
+
/**
|
|
971
|
+
* If true, read integer values as JavaScript BigInt. Overrides database-level setting.
|
|
972
|
+
* **Node.js v25+ feature** - silently ignored by `node:sqlite` on v24 and earlier.
|
|
973
|
+
* @default database default
|
|
974
|
+
*/
|
|
975
|
+
readonly readBigInts?: boolean;
|
|
976
|
+
/**
|
|
977
|
+
* If true, return results as arrays rather than objects. Overrides database-level setting.
|
|
978
|
+
* **Node.js v25+ feature** - silently ignored by `node:sqlite` on v24 and earlier.
|
|
979
|
+
* @default database default
|
|
980
|
+
*/
|
|
981
|
+
readonly returnArrays?: boolean;
|
|
982
|
+
/**
|
|
983
|
+
* If true, allows bare named parameters (without prefix). Overrides database-level setting.
|
|
984
|
+
* **Node.js v25+ feature** - silently ignored by `node:sqlite` on v24 and earlier.
|
|
985
|
+
* @default database default
|
|
986
|
+
*/
|
|
987
|
+
readonly allowBareNamedParameters?: boolean;
|
|
988
|
+
/**
|
|
989
|
+
* If true, unknown named parameters are ignored. Overrides database-level setting.
|
|
990
|
+
* **Node.js v25+ feature** - silently ignored by `node:sqlite` on v24 and earlier.
|
|
991
|
+
* @default database default
|
|
992
|
+
*/
|
|
993
|
+
readonly allowUnknownNamedParameters?: boolean;
|
|
708
994
|
}
|
|
709
995
|
/**
|
|
710
996
|
* The main SQLite module interface.
|
|
@@ -736,48 +1022,19 @@ interface SqliteModule {
|
|
|
736
1022
|
*/
|
|
737
1023
|
constants: SqliteConstants;
|
|
738
1024
|
}
|
|
739
|
-
/**
|
|
740
|
-
* The DatabaseSync class represents a synchronous connection to a SQLite database.
|
|
741
|
-
* All database operations are performed synchronously, blocking the thread until completion.
|
|
742
|
-
*
|
|
743
|
-
* @example
|
|
744
|
-
* ```typescript
|
|
745
|
-
* import { DatabaseSync } from '@photostructure/sqlite';
|
|
746
|
-
*
|
|
747
|
-
* // Create an in-memory database
|
|
748
|
-
* const db = new DatabaseSync(':memory:');
|
|
749
|
-
*
|
|
750
|
-
* // Create a file-based database
|
|
751
|
-
* const fileDb = new DatabaseSync('./mydata.db');
|
|
752
|
-
*
|
|
753
|
-
* // Create with options
|
|
754
|
-
* const readOnlyDb = new DatabaseSync('./data.db', { readOnly: true });
|
|
755
|
-
* ```
|
|
756
|
-
*/
|
|
757
1025
|
declare const DatabaseSync: SqliteModule["DatabaseSync"];
|
|
758
|
-
/**
|
|
759
|
-
* The StatementSync class represents a prepared SQL statement.
|
|
760
|
-
* This class should not be instantiated directly; use DatabaseSync.prepare() instead.
|
|
761
|
-
*
|
|
762
|
-
* @example
|
|
763
|
-
* ```typescript
|
|
764
|
-
* const stmt = db.prepare('SELECT * FROM users WHERE id = ?');
|
|
765
|
-
* const user = stmt.get(123);
|
|
766
|
-
* stmt.finalize();
|
|
767
|
-
* ```
|
|
768
|
-
*/
|
|
769
1026
|
declare const StatementSync: SqliteModule["StatementSync"];
|
|
770
1027
|
interface Session {
|
|
771
1028
|
/**
|
|
772
1029
|
* Generate a changeset containing all changes recorded by the session.
|
|
773
|
-
* @returns A
|
|
1030
|
+
* @returns A Uint8Array containing the changeset data.
|
|
774
1031
|
*/
|
|
775
|
-
changeset():
|
|
1032
|
+
changeset(): Uint8Array;
|
|
776
1033
|
/**
|
|
777
1034
|
* Generate a patchset containing all changes recorded by the session.
|
|
778
|
-
* @returns A
|
|
1035
|
+
* @returns A Uint8Array containing the patchset data.
|
|
779
1036
|
*/
|
|
780
|
-
patchset():
|
|
1037
|
+
patchset(): Uint8Array;
|
|
781
1038
|
/**
|
|
782
1039
|
* Close the session and release its resources.
|
|
783
1040
|
*/
|
|
@@ -857,4 +1114,4 @@ interface BackupOptions {
|
|
|
857
1114
|
declare const backup: (sourceDb: DatabaseSyncInstance, destination: string | Buffer | URL, options?: BackupOptions) => Promise<number>;
|
|
858
1115
|
declare const _default: SqliteModule;
|
|
859
1116
|
|
|
860
|
-
export { type AggregateOptions, type BackupOptions, type ChangesetApplyOptions, DatabaseSync, type DatabaseSyncInstance, type DatabaseSyncOptions, SQLTagStore, type SQLTagStoreInstance, Session, type SessionOptions, type SqliteAuthorizationActions, type SqliteAuthorizationResults, type SqliteChangesetConflictTypes, type SqliteChangesetResolution, type SqliteConstants, type SqliteModule, type SqliteOpenFlags, type StatementOptions, StatementSync, type StatementSyncInstance, type UserFunctionOptions, backup, constants, _default as default };
|
|
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 };
|