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
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.
Declaration
constructor(path, options) -> None
Parameters
| 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 |
Returns
None
Example
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.
Declaration
open() -> None
Returns
None
Example
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.
Declaration
close() -> None
Returns
None
Example
db.close()
exec(sql) -> None
Runs one or more SQL statements, without returning any results. Statements run in order
until one fails, which throws.
Declaration
exec(sql) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| sql | JSValue | The SQL to run |
Returns
None
Example
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.
Declaration
prepare(sql) -> StatementSync
Parameters
| Name | Type | Description |
|---|---|---|
| sql | JSValue | The SQL to compile |
Returns
StatementSync
The prepared statement
Example
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.
Declaration
location(dbName) -> string
Parameters
| Name | Type | Description |
|---|---|---|
| dbName | JSValue | The name of the attached database. Defaults to `"main"`. |
Returns
string
The path, or `null` for an in-memory database or an unknown name
Example
console.log(db.location())