The Database class represents a connection that can prepare and execute SQL statements.
Creates a new database connection.
| Param | Type | Description |
|---|---|---|
| path | string |
Path to the database file |
| options | object |
Options. |
The path parameter points to the SQLite database file to open. If the file pointed to by path does not exists, it will be created.
To open an in-memory database, please pass :memory: as the path parameter.
You can use the options parameter to specify various options. Options supported by the parameter are:
syncUrl: open the database as embedded replica synchronizing from the provided URL.syncPeriod: synchronize the database periodically everysyncPeriodseconds.authToken: authentication token for the provider URL (optional).timeout: number of milliseconds to wait on locked database before returningSQLITE_BUSYerrordefaultQueryTimeout: default maximum number of milliseconds a query is allowed to run before being interrupted withSQLITE_INTERRUPTerror
The function returns a Database object.
Prepares a SQL statement for execution.
| Param | Type | Description |
|---|---|---|
| sql | string |
The SQL statement string to prepare. |
The function returns a Statement object.
Returns a function that runs the given function in a transaction.
| Param | Type | Description |
|---|---|---|
| function | function |
The function to run in a transaction. |
This function is currently not supported.
This function is currently not supported.
This function is currently not supported.
This function is currently not supported.
This function is currently not supported.
This function is currently not supported.
Configure authorization rules for the database. Accepts three formats:
- Legacy format — a map from table name to
Authorization.ALLOWorAuthorization.DENY - Rule-based format — an
AuthorizerConfigobject with ordered rules and pattern matching null— removes the authorizer entirely
A simple object mapping table names to Authorization.ALLOW (0) or Authorization.DENY (1).
Tables without an entry are denied by default.
const { Authorization } = require('libsql');
db.authorizer({
"users": Authorization.ALLOW,
"secrets": Authorization.DENY,
});
// Access to "users" is allowed.
const stmt = db.prepare("SELECT * FROM users");
// Access to "secrets" throws SQLITE_AUTH.
const stmt = db.prepare("SELECT * FROM secrets"); // Error!An object with a rules array and an optional defaultPolicy. Rules are evaluated in order — first match wins. If no rule matches, defaultPolicy applies (defaults to DENY).
const { Authorization, Action } = require('libsql');
db.authorizer({
rules: [
// Hide sensitive columns (returns NULL instead of the real value)
{ action: Action.READ, table: "users", column: "password_hash", policy: Authorization.IGNORE },
{ action: Action.READ, table: "users", column: "ssn", policy: Authorization.IGNORE },
// Allow all reads
{ action: Action.READ, policy: Authorization.ALLOW },
// Allow inserts on tables matching a glob pattern
{ action: Action.INSERT, table: { glob: "logs_*" }, policy: Authorization.ALLOW },
// Deny DDL operations
{ action: [Action.CREATE_TABLE, Action.DROP_TABLE, Action.ALTER_TABLE], policy: Authorization.DENY },
// Allow transactions and selects
{ action: Action.TRANSACTION, policy: Authorization.ALLOW },
{ action: Action.SELECT, policy: Authorization.ALLOW },
],
defaultPolicy: Authorization.DENY,
});| Field | Type | Description |
|---|---|---|
| action | number | number[] |
Action code(s) to match (from Action). Omit to match all actions. |
| table | string | { glob: string } |
Table name pattern. Omit to match any table. |
| column | string | { glob: string } |
Column name pattern (relevant for READ/UPDATE). Omit to match any. |
| entity | string | { glob: string } |
Entity name (index, trigger, view, pragma, function). Omit to match any. |
| accessor | string | { glob: string } |
The innermost trigger or view that caused this access. See below. |
| policy | number |
Authorization.ALLOW, Authorization.DENY, or Authorization.IGNORE. |
The accessor field corresponds to the 4th argument of SQLite's C-level authorizer callback. When a READ occurs because a view is being expanded, SQLite sets this to the view name. For direct table access, it is null.
This enables view-scoped authorization: you can allow reads from an underlying table only when accessed through a specific view, while blocking direct access.
db.authorizer({
rules: [
{ action: Action.SELECT, policy: Authorization.ALLOW },
// Allow reads from the view itself
{ action: Action.READ, table: "my_view", policy: Authorization.ALLOW },
// Allow reads from the underlying table ONLY when accessed via my_view
{ action: Action.READ, table: "underlying_data", accessor: "my_view", policy: Authorization.ALLOW },
],
defaultPolicy: Authorization.DENY,
});
// Works — accessed through the view
db.prepare("SELECT * FROM my_view");
// Blocked — direct access has accessor=null, which doesn't match "my_view"
db.prepare("SELECT * FROM underlying_data"); // Error: SQLITE_AUTHPattern fields (table, column, entity, accessor) accept either:
- A plain string for exact matching:
"users" - An object with a
globkey for glob matching:{ glob: "logs_*" }
Glob patterns support * (match any number of characters) and ? (match exactly one character).
// Exact match
{ action: Action.READ, table: "users", policy: Authorization.ALLOW }
// Glob: all tables starting with "logs_"
{ action: Action.READ, table: { glob: "logs_*" }, policy: Authorization.ALLOW }
// Glob: single-character wildcard
{ action: Action.READ, table: { glob: "t?_data" }, policy: Authorization.ALLOW }
// Glob: match all tables
{ action: Action.READ, table: { glob: "*" }, policy: Authorization.ALLOW }| Value | Effect |
|---|---|
Authorization.ALLOW (0) |
Permit the operation. |
Authorization.DENY (1) |
Reject the entire SQL statement with a SQLITE_AUTH error. |
Authorization.IGNORE (2) |
For READ: return NULL instead of the column value. Otherwise: deny. |
Pass null to remove the authorizer and allow all operations:
db.authorizer(null);Loads a SQLite3 extension
Executes a SQL statement.
| Param | Type | Description |
|---|---|---|
| sql | string |
The SQL statement string to execute. |
| queryOptions | object |
Optional per-query overrides (for example, { queryTimeout: 100 }). |
Cancel ongoing operations and make them return at earliest opportunity.
Note: This is an extension in libSQL and not available in better-sqlite3.
Closes the database connection.
Executes the SQL statement and returns an info object.
| Param | Type | Description |
|---|---|---|
| bindParameters | array of objects |
The bind parameters for executing the statement. |
| queryOptions | object |
Optional per-query overrides (for example, { queryTimeout: 100 }). |
The returned info object contains two properties: changes that describes the number of modified rows and info.lastInsertRowid that represents the rowid of the last inserted row.
Executes the SQL statement and returns the first row.
| Param | Type | Description |
|---|---|---|
| bindParameters | array of objects |
The bind parameters for executing the statement. |
| queryOptions | object |
Optional per-query overrides (for example, { queryTimeout: 100 }). |
Executes the SQL statement and returns an array of the resulting rows.
| Param | Type | Description |
|---|---|---|
| bindParameters | array of objects |
The bind parameters for executing the statement. |
| queryOptions | object |
Optional per-query overrides (for example, { queryTimeout: 100 }). |
Executes the SQL statement and returns an iterator to the resulting rows.
| Param | Type | Description |
|---|---|---|
| bindParameters | array of objects |
The bind parameters for executing the statement. |
| queryOptions | object |
Optional per-query overrides (for example, { queryTimeout: 100 }). |
This function is currently not supported.
This function is currently not supported.
Toggle raw mode.
| Param | Type | Description |
|---|---|---|
| rawMode | boolean |
Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled. |
This function enables or disables raw mode. Prepared statements return objects by default, but if raw mode is enabled, the functions return arrays instead.
Toggle query duration timing.
Returns the columns in the result set returned by this prepared statement.
Returns true if the statement returns data (i.e., it is a SELECT statement or an INSERT/UPDATE/DELETE with a RETURNING clause), false otherwise.
This function is currently not supported.