deno.com

class DatabaseSync

This class represents a single connection to a SQLite database. All APIs exposed by this class execute synchronously.

Constructors #

#DatabaseSync(
location: string,
)
new

Constructs a new DatabaseSync instance.

Methods #

#applyChangeset(
changeset: Uint8Array,
): boolean

An exception is thrown if the database is not open. This method is a wrapper around sqlite3changeset_apply().

const sourceDb = new DatabaseSync(':memory:');
const targetDb = new DatabaseSync(':memory:');

sourceDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');

const session = sourceDb.createSession();

const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
insert.run(1, 'hello');
insert.run(2, 'world');

const changeset = session.changeset();
targetDb.applyChangeset(changeset);
// Now that the changeset has been applied, targetDb contains the same data as sourceDb.
#close(): void

Closes the database connection. An exception is thrown if the database is not open. This method is a wrapper around sqlite3_close_v2().

Creates and attaches a session to the database. This method is a wrapper around sqlite3session_create() and sqlite3session_attach().

#enableLoadExtension(allow: boolean): void

Enables or disables the loadExtension SQL function, and the loadExtension() method. When allowExtension is false when constructing, you cannot enable loading extensions for security reasons.

#exec(sql: string): void

This method allows one or more SQL statements to be executed without returning any results. This method is useful when executing SQL statements read from a file. This method is a wrapper around sqlite3_exec().

#function(
name: string,
options: FunctionOptions,
func: (...args: SupportedValueType[]) => SupportedValueType,
): void

This method is used to create SQLite user-defined functions. This method is a wrapper around sqlite3_create_function_v2().

#function(
name: string,
func: (...args: SupportedValueType[]) => SupportedValueType,
): void
#loadExtension(path: string): void

Loads a shared library into the database connection. This method is a wrapper around sqlite3_load_extension(). It is required to enable the allowExtension option when constructing the DatabaseSync instance.

#open(): void

Opens the database specified in the location argument of the DatabaseSyncconstructor. This method should only be used when the database is not opened via the constructor. An exception is thrown if the database is already open.

#prepare(sql: string): StatementSync

Compiles a SQL statement into a prepared statement. This method is a wrapper around sqlite3_prepare_v2().