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 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
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 JSValue 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 JSValue The SQL to compile
StatementSync
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 JSValue 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())