Skip to main content
A Database object is returned by connect(). It holds the connection state and is used to prepare statements and run ad-hoc SQL.

connect(path, options?)

Opens a connection to a database file and returns a Database instance.
import { connect } from '@tursodatabase/database';

const db = await connect('turso.db');
const db = await connect(':memory:');
path
string
required
Path to the database file. Use ":memory:" for a transient in-memory database. The file is created when it does not exist.
options
DatabaseOpts
Optional configuration object.
Returns Database

db.prepare(sql)

Compiles a SQL statement and returns a reusable Statement object.
const stmt = db.prepare('SELECT * FROM users WHERE id = ?');
sql
string
required
The SQL statement to compile.
Returns Statement — see Statement for the full method list.

db.exec(sql)

Executes one or more SQL statements without returning rows. Useful for DDL and scripts.
db.exec(`
  CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);
  CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT);
`);
sql
string
required
One or more SQL statements separated by semicolons.
Returns this

db.close()

Closes the database connection and finalizes all outstanding prepared statements. Any further use of the Database or any of its Statement objects will throw.
db.close();
Returns this

db.lastInsertRowid()

Returns the rowid of the row most recently inserted by this connection. Returns number

db.changes()

Returns the number of rows modified by the most recent INSERT, UPDATE, or DELETE statement. Returns number

db.totalChanges()

Returns the total number of rows modified since the connection was opened. Returns number

db.transaction(fn)

Wraps a function in a transaction. Returns a new function that, when called, executes the original function within a SQLite transaction.
fn
function
required
The function to wrap. It receives the same arguments as the returned function.
The returned function also has .deferred, .immediate, and .exclusive properties for controlling the transaction mode.
import { Database } from '@tursodatabase/database';

const db = new Database('app.db');
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');

const insert = db.transaction((name) => {
  db.prepare('INSERT INTO users (name) VALUES (?)').run(name);
});

// Runs in a DEFERRED transaction
insert('Alice');

// Runs in an IMMEDIATE transaction
insert.immediate('Bob');

db.defaultSafeIntegers(toggle?)

Sets the default safeIntegers mode for all new statements prepared on this connection. When enabled, 64-bit integers are returned as BigInt instead of number.
toggle
boolean
default:"true"
Pass false to disable. Omit or pass true to enable.

Properties

PropertyTypeDescription
readonlybooleanWhether the connection is in read-only mode.
memorybooleanWhether this is an in-memory database.
pathstringFile path the connection was opened with.
openbooleanWhether the connection is still open.

Unsupported methods

The following methods from the better-sqlite3 API are not yet implemented:
These methods throw or are no-ops when called. They are listed here for reference when migrating from better-sqlite3.
MethodStatus
db.pragma(string, options?)Not supported
db.backup(destination, options?)Not supported
db.serialize(options?)Not supported
db.function(name, options?, fn)Not supported
db.aggregate(name, options)Not supported
db.table(name, definition)Not supported
db.authorizer(rules)Not supported
db.loadExtension(path, entryPoint?)Not supported
db.interrupt()Not supported

Build docs developers (and LLMs) love