JSTime natively implements a high-performance SQLite3 driver. To use it import from the built-inDocumentation Index
Fetch the complete documentation index at: https://docs.awfixer.me/llms.txt
Use this file to discover all available pages before exploring further.
jstime:sqlite module.
jstime:sqlite.
Features include:
- Transactions
- Parameters (named & positional)
- Prepared statements
- Datatype conversions (
BLOBbecomesUint8Array) - The fastest performance of any SQLite driver for JavaScript
jstime:sqlite module is roughly 3-6x faster than better-sqlite3 and 8-9x faster than deno.land/x/sqlite for read queries. Each driver was benchmarked against the Northwind Traders dataset. View and run the benchmark source.
Database
To open or create a SQLite3 database:readonly mode:
.close()
To close a database:
close() is called automatically when the database is garbage collected. It is safe to call multiple times but has no effect after the first.
.serialize()
jstime:sqlite supports SQLite’s built-in mechanism for serializing and deserializing databases to and from memory.
.serialize() calls sqlite3_serialize.
.query()
Use the db.query() method on your Database instance to prepare a SQL query. The result is a Statement instance that will be cached on the Database instance. The query will not be executed.
Note — Use the
.prepare() method to prepare a query without caching it on the Database instance.Statements
AStatement is a prepared query, which means it’s been parsed and compiled into an efficient binary form. It can be executed multiple times in a performant way.
Create a statement with the .query method on your Database instance.
?1) or named ($param or :param or @param).
Statement can be executed with several different methods, each returning the results in a different form.
.all()
Use .all() to run a query and get back the results as an array of objects.
sqlite3_reset and repeatedly calls sqlite3_step until it returns SQLITE_DONE.
.get()
Use .get() to run a query and get back the first result as an object.
sqlite3_reset followed by sqlite3_step until it no longer returns SQLITE_ROW. If the query returns no rows, undefined is returned.
.run()
Use .run() to run a query and get back undefined. This is useful for queries schema-modifying queries (e.g. CREATE TABLE) or bulk write operations.
sqlite3_reset and calls sqlite3_step once. Stepping through all the rows is not necessary when you don’t care about the results.
.values()
Use values() to run a query and get back all results as an array of arrays.
sqlite3_reset and repeatedly calls sqlite3_step until it returns SQLITE_DONE.
.finalize()
Use .finalize() to destroy a Statement and free any resources associated with it. Once finalized, a Statement cannot be executed again. Typically, the garbage collector will do this for you, but explicit finalization may be useful in performance-sensitive applications.
.toString()
Calling toString() on a Statement instance prints the expanded SQL query. This is useful for debugging.
sqlite3_expanded_sql. The parameters are expanded using the most recently bound values.
Parameters
Queries can contain parameters. These can be numerical (?1) or named ($param or :param or @param). Bind values to these parameters when executing the query:
Transactions
Transactions are a mechanism for executing multiple queries in an atomic way; that is, either all of the queries succeed or none of them do. Create a transaction with thedb.transaction() method:
db.transaction() returns a new function (insertCats) that wraps the function that executes the queries.
To execute the transaction, call this function. All arguments will be passed through to the wrapped function; the return value of the wrapped function will be returned by the transaction function. The wrapped function also has access to the this context as defined where the transaction is executed.
begin a transaction when insertCats is called and commit it when the wrapped function returns. If an exception is thrown, the transaction will be rolled back. The exception will propagate as usual; it is not caught.
Nested transactions — Transaction functions can be called from inside other transaction functions. When doing so, the inner transaction becomes a savepoint.
View nested transaction example
View nested transaction example
deferred, immediate, and exclusive versions.
.loadExtension()
To load a SQLite extension, call .loadExtension(name) on your Database instance
For macOS users
For macOS users
MacOS users By default, macOS ships with Apple’s proprietary build of SQLite, which doesn’t support extensions. To use extensions, you’ll need to install a vanilla build of SQLite.To point
jstime:sqlite to the new build, call Database.setCustomSQLite(path) before creating any Database instances. (On other operating systems, this is a no-op.) Pass a path to the SQLite .dylib file, not the executable. With recent versions of Homebrew this is something like /opt/homebrew/Cellar/sqlite/<version>/libsqlite3.dylib.Reference
Datatypes
| JavaScript type | SQLite type |
|---|---|
string | TEXT |
number | INTEGER or DECIMAL |
boolean | INTEGER (1 or 0) |
Uint8Array | BLOB |
Buffer | BLOB |
bigint | INTEGER |
null | NULL |