forked from oven-sh/bun-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.d.ts
747 lines (722 loc) · 19.4 KB
/
sqlite.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/**
* Fast SQLite3 driver for Bun.js
* @since v0.0.83
*
* @example
* ```ts
* import { Database } from 'bun:sqlite';
*
* var db = new Database('app.db');
* db.query('SELECT * FROM users WHERE name = ?').all('John');
* // => [{ id: 1, name: 'John' }]
* ```
*
* The following types can be used when binding parameters:
*
* | JavaScript type | SQLite type |
* | -------------- | ----------- |
* | `string` | `TEXT` |
* | `number` | `INTEGER` or `DECIMAL` |
* | `boolean` | `INTEGER` (1 or 0) |
* | `Uint8Array` | `BLOB` |
* | `Buffer` | `BLOB` |
* | `bigint` | `INTEGER` |
* | `null` | `NULL` |
*/
declare module "bun:sqlite" {
export class Database {
/**
* Open or create a SQLite3 database
*
* @param filename The filename of the database to open. Pass an empty string (`""`) or `":memory:"` or undefined for an in-memory database.
* @param options defaults to `{readwrite: true, create: true}`. If a number, then it's treated as `SQLITE_OPEN_*` constant flags.
*
* @example
*
* ```ts
* const db = new Database("mydb.sqlite");
* db.run("CREATE TABLE foo (bar TEXT)");
* db.run("INSERT INTO foo VALUES (?)", "baz");
* console.log(db.query("SELECT * FROM foo").all());
* ```
*
* @example
*
* Open an in-memory database
*
* ```ts
* const db = new Database(":memory:");
* db.run("CREATE TABLE foo (bar TEXT)");
* db.run("INSERT INTO foo VALUES (?)", "hiiiiii");
* console.log(db.query("SELECT * FROM foo").all());
* ```
*
* @example
*
* Open read-only
*
* ```ts
* const db = new Database("mydb.sqlite", {readonly: true});
* ```
*/
constructor(
filename?: string,
options?:
| number
| {
/**
* Open the database as read-only (no write operations, no create).
*
* Equivalent to {@link constants.SQLITE_OPEN_READONLY}
*/
readonly?: boolean;
/**
* Allow creating a new database
*
* Equivalent to {@link constants.SQLITE_OPEN_CREATE}
*/
create?: boolean;
/**
* Open the database as read-write
*
* Equivalent to {@link constants.SQLITE_OPEN_READWRITE}
*/
readwrite?: boolean;
}
);
/**
* This is an alias of `new Database()`
*
* See {@link Database}
*/
static open(
filename: string,
options?:
| number
| {
/**
* Open the database as read-only (no write operations, no create).
*
* Equivalent to {@link constants.SQLITE_OPEN_READONLY}
*/
readonly?: boolean;
/**
* Allow creating a new database
*
* Equivalent to {@link constants.SQLITE_OPEN_CREATE}
*/
create?: boolean;
/**
* Open the database as read-write
*
* Equivalent to {@link constants.SQLITE_OPEN_READWRITE}
*/
readwrite?: boolean;
}
): Database;
/**
* Execute a SQL query **without returning any results**.
*
* This does not cache the query, so if you want to run a query multiple times, you should use {@link prepare} instead.
*
* @example
* ```ts
* db.run("CREATE TABLE foo (bar TEXT)");
* db.run("INSERT INTO foo VALUES (?)", "baz");
* ```
*
* Useful for queries like:
* - `CREATE TABLE`
* - `INSERT INTO`
* - `UPDATE`
* - `DELETE FROM`
* - `DROP TABLE`
* - `PRAGMA`
* - `ATTACH DATABASE`
* - `DETACH DATABASE`
* - `REINDEX`
* - `VACUUM`
* - `EXPLAIN ANALYZE`
* - `CREATE INDEX`
* - `CREATE TRIGGER`
* - `CREATE VIEW`
* - `CREATE VIRTUAL TABLE`
* - `CREATE TEMPORARY TABLE`
*
* @param sql The SQL query to run
*
* @param bindings Optional bindings for the query
*
* @returns `Database` instance
*
* Under the hood, this calls `sqlite3_prepare_v3` followed by `sqlite3_step` and `sqlite3_finalize`.
*
* * The following types can be used when binding parameters:
*
* | JavaScript type | SQLite type |
* | -------------- | ----------- |
* | `string` | `TEXT` |
* | `number` | `INTEGER` or `DECIMAL` |
* | `boolean` | `INTEGER` (1 or 0) |
* | `Uint8Array` | `BLOB` |
* | `Buffer` | `BLOB` |
* | `bigint` | `INTEGER` |
* | `null` | `NULL` |
*/
run<ParamsType = SQLQueryBindings>(
sqlQuery: string,
...bindings: ParamsType[]
): void;
/**
This is an alias of {@link Database.prototype.run}
*/
exec<ParamsType = SQLQueryBindings>(
sqlQuery: string,
...bindings: ParamsType[]
): void;
/**
* Compile a SQL query and return a {@link Statement} object. This is the
* same as {@link prepare} except that it caches the compiled query.
*
* This **does not execute** the query, but instead prepares it for later
* execution and caches the compiled query if possible.
*
* @example
* ```ts
* // compile the query
* const stmt = db.query("SELECT * FROM foo WHERE bar = ?");
* // run the query
* stmt.all("baz");
*
* // run the query again
* stmt.all();
* ```
*
* @param sql The SQL query to compile
*
* @returns `Statment` instance
*
* Under the hood, this calls `sqlite3_prepare_v3`.
*
*/
query<ParamsType = SQLQueryBindings, ReturnType = any>(
sqlQuery: string
): Statement<ParamsType, ReturnType>;
/**
* Compile a SQL query and return a {@link Statement} object.
*
* This does not cache the compiled query and does not execute the query.
*
* @example
* ```ts
* // compile the query
* const stmt = db.query("SELECT * FROM foo WHERE bar = ?");
* // run the query
* stmt.all("baz");
* ```
*
* @param sql The SQL query to compile
* @param params Optional bindings for the query
*
* @returns `Statment` instance
*
* Under the hood, this calls `sqlite3_prepare_v3`.
*
*/
prepare<ParamsType = SQLQueryBindings, ReturnType = any>(
sql: string,
...params: ParamsType[]
): Statement<ParamsType, ReturnType>;
/**
* Is the database in a transaction?
*
* @returns `true` if the database is in a transaction, `false` otherwise
*
* @example
* ```ts
* db.run("CREATE TABLE foo (bar TEXT)");
* db.run("INSERT INTO foo VALUES (?)", "baz");
* db.run("BEGIN");
* db.run("INSERT INTO foo VALUES (?)", "qux");
* console.log(db.inTransaction());
* ```
*/
get inTransaction(): boolean;
/**
* Close the database connection.
*
* It is safe to call this method multiple times. If the database is already
* closed, this is a no-op. Running queries after the database has been
* closed will throw an error.
*
* @example
* ```ts
* db.close();
* ```
* This is called automatically when the database instance is garbage collected.
*
* Internally, this calls `sqlite3_close_v2`.
*/
close(): void;
/**
* The filename passed when `new Database()` was called
* @example
* ```ts
* const db = new Database("mydb.sqlite");
* console.log(db.filename);
* // => "mydb.sqlite"
* ```
*/
readonly filename: string;
/**
* The underlying `sqlite3` database handle
*
* In native code, this is not a file descriptor, but an index into an array of database handles
*/
readonly handle: number;
/**
* Load a SQLite3 extension
*
* macOS requires a custom SQLite3 library to be linked because the Apple build of SQLite for macOS disables loading extensions. See {@link Database.setCustomSQLite}
*
* Bun chooses the Apple build of SQLite on macOS because it brings a ~50% performance improvement.
*
* @param extension name/path of the extension to load
* @param entryPoint optional entry point of the extension
*/
loadExtension(extension: string, entryPoint?: string): void;
/**
* Change the dynamic library path to SQLite
*
* @note macOS-only
*
* This only works before SQLite is loaded, so
* that's before you call `new Database()`.
*
* It can only be run once because this will load
* the SQLite library into the process.
*
* @param path The path to the SQLite library
*
*/
static setCustomSQLite(path: string): boolean;
/**
* Creates a function that always runs inside a transaction. When the
* function is invoked, it will begin a new transaction. When the function
* returns, the transaction will be committed. If an exception is thrown,
* the transaction will be rolled back (and the exception will propagate as
* usual).
*
* @param insideTransaction The callback which runs inside a transaction
*
* @example
* ```ts
* // setup
* import { Database } from "bun:sqlite";
* const db = Database.open(":memory:");
* db.exec(
* "CREATE TABLE cats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, age INTEGER)"
* );
*
* const insert = db.prepare("INSERT INTO cats (name, age) VALUES ($name, $age)");
* const insertMany = db.transaction((cats) => {
* for (const cat of cats) insert.run(cat);
* });
*
* insertMany([
* { $name: "Joey", $age: 2 },
* { $name: "Sally", $age: 4 },
* { $name: "Junior", $age: 1 },
* ]);
* ```
*/
transaction(insideTransaction: (...args: any) => void): CallableFunction & {
/**
* uses "BEGIN DEFERRED"
*/
deferred: (...args: any) => void;
/**
* uses "BEGIN IMMEDIATE"
*/
immediate: (...args: any) => void;
/**
* uses "BEGIN EXCLUSIVE"
*/
exclusive: (...args: any) => void;
};
}
/**
* A prepared statement.
*
* This is returned by {@link Database.prepare} and {@link Database.query}.
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
* stmt.all("baz");
* // => [{bar: "baz"}]
* ```
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
* stmt.get("baz");
* // => {bar: "baz"}
* ```
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
* stmt.run("baz");
* // => undefined
* ```
*/
export class Statement<ParamsType = SQLQueryBindings, ReturnType = any> {
/**
* Creates a new prepared statement from native code.
*
* This is used internally by the {@link Database} class. Probably you don't need to call this yourself.
*/
constructor(nativeHandle: any);
/**
* Execute the prepared statement and return all results as objects.
*
* @param params optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
*
* stmt.all("baz");
* // => [{bar: "baz"}]
*
* stmt.all();
* // => [{bar: "baz"}]
*
* stmt.all("foo");
* // => [{bar: "foo"}]
* ```
*/
all(...params: ParamsType[]): ReturnType[];
/**
* Execute the prepared statement and return **the first** result.
*
* If no result is returned, this returns `null`.
*
* @param params optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
*
* stmt.all("baz");
* // => [{bar: "baz"}]
*
* stmt.all();
* // => [{bar: "baz"}]
*
* stmt.all("foo");
* // => [{bar: "foo"}]
* ```
*
* The following types can be used when binding parameters:
*
* | JavaScript type | SQLite type |
* | -------------- | ----------- |
* | `string` | `TEXT` |
* | `number` | `INTEGER` or `DECIMAL` |
* | `boolean` | `INTEGER` (1 or 0) |
* | `Uint8Array` | `BLOB` |
* | `Buffer` | `BLOB` |
* | `bigint` | `INTEGER` |
* | `null` | `NULL` |
*
*/
get(...params: ParamsType[]): ReturnType | null;
/**
* Execute the prepared statement. This returns `undefined`.
*
* @param params optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.
*
* @example
* ```ts
* const stmt = db.prepare("UPDATE foo SET bar = ?");
* stmt.run("baz");
* // => undefined
*
* stmt.run();
* // => undefined
*
* stmt.run("foo");
* // => undefined
* ```
*
* The following types can be used when binding parameters:
*
* | JavaScript type | SQLite type |
* | -------------- | ----------- |
* | `string` | `TEXT` |
* | `number` | `INTEGER` or `DECIMAL` |
* | `boolean` | `INTEGER` (1 or 0) |
* | `Uint8Array` | `BLOB` |
* | `Buffer` | `BLOB` |
* | `bigint` | `INTEGER` |
* | `null` | `NULL` |
*
*/
run(...params: ParamsType[]): void;
/**
* Execute the prepared statement and return the results as an array of arrays.
*
* This is a little faster than {@link all}.
*
* @param params optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
*
* stmt.values("baz");
* // => [['baz']]
*
* stmt.values();
* // => [['baz']]
*
* stmt.values("foo");
* // => [['foo']]
* ```
*
* The following types can be used when binding parameters:
*
* | JavaScript type | SQLite type |
* | -------------- | ----------- |
* | `string` | `TEXT` |
* | `number` | `INTEGER` or `DECIMAL` |
* | `boolean` | `INTEGER` (1 or 0) |
* | `Uint8Array` | `BLOB` |
* | `Buffer` | `BLOB` |
* | `bigint` | `INTEGER` |
* | `null` | `NULL` |
*
*/
values(
...params: ParamsType[]
): Array<Array<string | bigint | number | boolean | Uint8Array>>;
/**
* The names of the columns returned by the prepared statement.
* @example
* ```ts
* const stmt = db.prepare("SELECT bar FROM foo WHERE bar = ?");
*
* console.log(stmt.columnNames);
* // => ["bar"]
* ```
*/
readonly columnNames: string[];
/**
* The number of parameters expected in the prepared statement.
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
* console.log(stmt.paramsCount);
* // => 1
* ```
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ? AND baz = ?");
* console.log(stmt.paramsCount);
* // => 2
* ```
*
*/
readonly paramsCount: number;
/**
* Finalize the prepared statement, freeing the resources used by the
* statement and preventing it from being executed again.
*
* This is called automatically when the prepared statement is garbage collected.
*
* It is safe to call this multiple times. Calling this on a finalized
* statement has no effect.
*
* Internally, this calls `sqlite3_finalize`.
*/
finalize(): void;
/**
* Return the expanded SQL string for the prepared statement.
*
* Internally, this calls `sqlite3_expanded_sql()` on the underlying `sqlite3_stmt`.
*
* @example
* ```ts
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?", "baz");
* console.log(stmt.toString());
* // => "SELECT * FROM foo WHERE bar = 'baz'"
* console.log(stmt);
* // => "SELECT * FROM foo WHERE bar = 'baz'"
* ```
*/
toString(): string;
/**
* Native object representing the underlying `sqlite3_stmt`
*
* This is left untyped because the ABI of the native bindings may change at any time.
*/
readonly native: any;
}
/**
* Constants from `sqlite3.h`
*
* This list isn't exhaustive, but some of the ones which are relevant
*/
export const constants: {
/**
* Open the database as read-only (no write operations, no create).
* @value 0x00000001
*/
SQLITE_OPEN_READONLY: number;
/**
* Open the database for reading and writing
* @value 0x00000002
*/
SQLITE_OPEN_READWRITE: number;
/**
* Allow creating a new database
* @value 0x00000004
*/
SQLITE_OPEN_CREATE: number;
/**
*
* @value 0x00000008
*/
SQLITE_OPEN_DELETEONCLOSE: number;
/**
*
* @value 0x00000010
*/
SQLITE_OPEN_EXCLUSIVE: number;
/**
*
* @value 0x00000020
*/
SQLITE_OPEN_AUTOPROXY: number;
/**
*
* @value 0x00000040
*/
SQLITE_OPEN_URI: number;
/**
*
* @value 0x00000080
*/
SQLITE_OPEN_MEMORY: number;
/**
*
* @value 0x00000100
*/
SQLITE_OPEN_MAIN_DB: number;
/**
*
* @value 0x00000200
*/
SQLITE_OPEN_TEMP_DB: number;
/**
*
* @value 0x00000400
*/
SQLITE_OPEN_TRANSIENT_DB: number;
/**
*
* @value 0x00000800
*/
SQLITE_OPEN_MAIN_JOURNAL: number;
/**
*
* @value 0x00001000
*/
SQLITE_OPEN_TEMP_JOURNAL: number;
/**
*
* @value 0x00002000
*/
SQLITE_OPEN_SUBJOURNAL: number;
/**
*
* @value 0x00004000
*/
SQLITE_OPEN_SUPER_JOURNAL: number;
/**
*
* @value 0x00008000
*/
SQLITE_OPEN_NOMUTEX: number;
/**
*
* @value 0x00010000
*/
SQLITE_OPEN_FULLMUTEX: number;
/**
*
* @value 0x00020000
*/
SQLITE_OPEN_SHAREDCACHE: number;
/**
*
* @value 0x00040000
*/
SQLITE_OPEN_PRIVATECACHE: number;
/**
*
* @value 0x00080000
*/
SQLITE_OPEN_WAL: number;
/**
*
* @value 0x01000000
*/
SQLITE_OPEN_NOFOLLOW: number;
/**
*
* @value 0x02000000
*/
SQLITE_OPEN_EXRESCODE: number;
/**
*
* @value 0x01
*/
SQLITE_PREPARE_PERSISTENT: number;
/**
*
* @value 0x02
*/
SQLITE_PREPARE_NORMALIZE: number;
/**
*
* @value 0x04
*/
SQLITE_PREPARE_NO_VTAB: number;
};
/**
* The native module implementing the sqlite3 C bindings
*
* It is lazily-initialized, so this will return `undefined` until the first
* call to new Database().
*
* The native module makes no gurantees about ABI stability, so it is left
* untyped
*
* If you need to use it directly for some reason, please let us know because
* that probably points to a deficiency in this API.
*
*/
export var native: any;
export type SQLQueryBindings =
| string
| bigint
| TypedArray
| number
| boolean
| null
| Record<string, string | bigint | TypedArray | number | boolean | null>;
export default Database;
}