@caido/quickjs-types / extra/sqlite
extra/sqlite
Classes
Database
A SQLite database.
The implementation uses a connection pool and is fully asynchronous. Each connection will be spawned in a worker thread.
Example
const db = await open({ filename: "path/to/database.sqlite" });
await db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT);");
await db.exec("INSERT INTO test (name) VALUES ('foo');");Constructors
Constructor
new Database():
Database
Returns
Methods
exec()
exec(
sql:string):Promise<void>
This method allows one or more SQL statements to be executed without returning any results.
Parameters
| Parameter | Type |
|---|---|
sql | string |
Returns
Promise<void>
prepare()
prepare(
sql:string):Promise<Statement>
Compiles a SQL statement into a prepared statement.
Parameters
| Parameter | Type |
|---|---|
sql | string |
Returns
Promise<Statement>
Statement
This class represents a single prepared statement. This class cannot be instantiated via its constructor. Instead, instances are created via the database.prepare() method.
Constructors
Constructor
new Statement():
Statement
Returns
Methods
all()
all<
T>(...params:Parameter[]):Promise<T[]>
This method executes a prepared statement and returns all results as an array of objects. If the prepared statement does not return any results, this method returns an empty array. The prepared statement parameters are bound using the values in params.
Type Parameters
| Type Parameter | Default type |
|---|---|
T extends object | object |
Parameters
| Parameter | Type | Description |
|---|---|---|
...params | Parameter[] | The values to bind to the prepared statement. Named parameters are not supported. |
Returns
Promise<T[]>
get()
get<
T>(...params:Parameter[]):Promise<T|undefined>
This method executes a prepared statement and returns the first result as an object. If the prepared statement does not return any results, this method returns undefined. The prepared statement parameters are bound using the values in params.
Type Parameters
| Type Parameter | Default type |
|---|---|
T extends object | object |
Parameters
| Parameter | Type | Description |
|---|---|---|
...params | Parameter[] | The values to bind to the prepared statement. Named parameters are not supported. |
Returns
Promise<T | undefined>
run()
This method executes a prepared statement and returns an object summarizing the resulting changes. The prepared statement parameters are bound using the values in params.
Parameters
| Parameter | Type | Description |
|---|---|---|
...params | Parameter[] | The values to bind to the prepared statement. Named parameters are not supported. |
Returns
Promise<Result>
Type Aliases
OpenOptions
OpenOptions =
object
Properties
busyTimeout?
optionalbusyTimeout:number
Time (in milliseconds) to wait for the database to be unlocked before throwing an error.
Default
5000filename?
optionalfilename:string
The filename of the database. If the file does not exist, a new one will be created.
foreignKeys?
optionalforeignKeys:boolean
Enable foreign key constraints.
idleTimeout?
optionalidleTimeout:number
Maximum amount of time (in seconds) that a connection is allowed to be idle before it is closed.
Default
infinityin_memory?
optionalin_memory:boolean
If true, the database will be opened in-memory.
Default
falsemaxConnections?
optionalmaxConnections:number
Maximum number of connections to the database.
Default
5maxLifetime?
optionalmaxLifetime:number
Maximum amount of time (in seconds) that a connection is allowed to exist before it is closed. Set to null to disable.
Default
3600minConnections?
optionalminConnections:number
Minimum number of connections to the database.
Default
0pageSize?
optionalpageSize:number
Set the SQlite page size.
Default
4096wal?
optionalwal:boolean
If true, the database will use the WAL mode.
Default
trueParameter
Parameter =
null|number|bigint|string|Uint8Array
Result
Result =
object
Properties
changes
changes:
number
lastInsertRowid
lastInsertRowid:
number
Functions
open()
open(
options:OpenOptions):Promise<Database>
Open a SQLite database.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | OpenOptions | The options to open the database. |
Returns
Promise<Database>
