API Docs

A 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

isOpen

boolean

Whether the database is currently open.

isTransaction

boolean

Whether the database is inside a transaction. Throws if the database isn't open.

Methods

constructor(path, options) -> None

Opens a SQLite database.

Statements prepared on this database start with its readBigInts, returnArrays, allowBareNamedParameters and allowUnknownNamedParameters settings.

constructor(path, options) -> None
Name Type Description
path string 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 optional object How to open the database
open optional boolean Open the database now. If false, call open() later. Defaults to true
readOnly optional boolean Open the database read-only. Defaults to false
enableForeignKeyConstraints optional boolean Enforce foreign key constraints. Defaults to true
enableDoubleQuotedStringLiterals optional boolean Accept double-quoted string literals. Defaults to false
timeout optional number Milliseconds to wait for a locked database before failing. Defaults to 0
readBigInts optional boolean Read INTEGER values as BigInts. Defaults to false
returnArrays optional boolean Return rows as arrays instead of objects. Defaults to false
allowBareNamedParameters optional boolean Allow named parameters without their prefix character. Defaults to true
allowUnknownNamedParameters optional boolean Ignore named parameters that the statement doesn't use. Defaults to false
None
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
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
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 string The SQL to run
None
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 string The SQL to compile
The prepared statement
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 optional string The name of the attached database. Defaults to "main".
string
The path, or null for an in-memory database or an unknown name
console.log(db.location())