DatabaseSync
TypeA connection to a SQLite database, as Node's DatabaseSync from require('node:sqlite').
Every method runs synchronously. Databases are opened with foreign key constraints enforced and double-quoted string literals disabled, as in Node.
Not implemented from Node: function(), aggregate(), loadExtension(),
enableLoadExtension(), createSession(), applyChangeset(), backup(),
createTagStore() and Symbol.dispose.
Properties
isTransaction
Whether the database is inside a transaction. Throws if the database isn't open.
Methods
constructor(path, options) -> None
Opens a SQLite database.
Supported options, all optional:
open(boolean, defaulttrue): open the database now. Iffalse, callopen()later.readOnly(boolean, defaultfalse): open the database read-only.enableForeignKeyConstraints(boolean, defaulttrue): enforce foreign key constraints.enableDoubleQuotedStringLiterals(boolean, defaultfalse): accept double-quoted string literals.timeout(integer, default0): milliseconds to wait for a locked database before failing.readBigInts(boolean, defaultfalse): read INTEGER values asBigInts.returnArrays(boolean, defaultfalse): return rows as arrays instead of objects.allowBareNamedParameters(boolean, defaulttrue): allow named parameters without their prefix character.allowUnknownNamedParameters(boolean, defaultfalse): ignore named parameters that the statement doesn't use.
Statements prepared on this database start with its readBigInts, returnArrays,
allowBareNamedParameters and allowUnknownNamedParameters settings.
constructor(path, options) -> None
| Name | Type | Description |
|---|---|---|
| path | JSValue | The path of the database file, or ":memory:" for an in-memory database. A leading ~ is expanded to the home directory. Relative paths are relative to Hammerspoon's working directory, so use absolute paths. |
| options | JSValue | The options described above |
const { DatabaseSync } = require('node:sqlite')
const db = new DatabaseSync('~/notes.db')
db.exec('CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT)')
open() -> None
Opens the database. Only needed if it was constructed with open: false, or after
close(). Throws if the database is already open.
open() -> None
const db = new DatabaseSync('~/notes.db', { open: false })
db.open()
close() -> None
Closes the database, finalizing every statement prepared on it. Throws if the database isn't open.
close() -> None
db.close()
exec(sql) -> None
Runs one or more SQL statements, without returning any results. Statements run in order until one fails, which throws.
exec(sql) -> None
| Name | Type | Description |
|---|---|---|
| sql | JSValue | The SQL to run |
db.exec(`
CREATE TABLE IF NOT EXISTS visits (app TEXT, at INTEGER);
DELETE FROM visits WHERE at < unixepoch() - 86400;
`)
prepare(sql) -> StatementSync
Compiles a SQL statement so it can be run, any number of times, with different parameters.
Only the first statement in sql is compiled.
prepare(sql) -> StatementSync
| Name | Type | Description |
|---|---|---|
| sql | JSValue | The SQL to compile |
const insert = db.prepare('INSERT INTO visits (app, at) VALUES (?, unixepoch())')
insert.run('Safari')
location(dbName) -> string
The file path of a database attached to this connection.
location(dbName) -> string
| Name | Type | Description |
|---|---|---|
| dbName | JSValue | The name of the attached database. Defaults to "main". |
null for an in-memory database or an unknown nameconsole.log(db.location())