@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
new Database()
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
new Statement()
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
<undefined
|T
>
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
<undefined
| T
>
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
Type declaration
busyTimeout?
optional
busyTimeout:number
Time (in milliseconds) to wait for the database to be unlocked before throwing an error.
Default
5000
filename?
optional
filename:string
The filename of the database. If the file does not exist, a new one will be created.
foreignKeys?
optional
foreignKeys:boolean
Enable foreign key constraints.
idleTimeout?
optional
idleTimeout:number
Maximum amount of time (in seconds) that a connection is allowed to be idle before it is closed.
Default
infinity
in_memory?
optional
in_memory:boolean
If true, the database will be opened in-memory.
Default
false
maxConnections?
optional
maxConnections:number
Maximum number of connections to the database.
Default
5
maxLifetime?
optional
maxLifetime:number
Maximum amount of time (in seconds) that a connection is allowed to exist before it is closed. Set to null
to disable.
Default
3600
minConnections?
optional
minConnections:number
Minimum number of connections to the database.
Default
0
pageSize?
optional
pageSize:number
Set the SQlite page size.
Default
4096
wal?
optional
wal:boolean
If true, the database will use the WAL mode.
Default
true
Parameter
Parameter:
null
|number
|bigint
|string
|Uint8Array
Result
Result:
object
Type declaration
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
>