Skip to main content

Class: Database<D>

Database is the primary API for accessing the Tableland network as a database. This class provides a small and simple API that will feel very familiar to web2 database users. It includes the concept of prepared statements, SQL parameter binding, execution and query modes, and more. It is actually similar to the better-sqlite3, and D1 APIs in many respects.

Type parameters

D = unknown

Constructors

new Database()

new Database<D>(config): Database<D>

Create a Database instance with the specified connection configuration.

Parameters

config: Partial <ReadConfig & SignerConfig> & Partial <AutoWaitConfig>= {}

The connection configuration. These keys are evaluated lazily, so it is possible to omit the baseUrl or signer, depending on your query needs. For a read-only Database for instance, only the baseUrl needs to be provided.

Returns

Database<D>

Source

@tableland/sdk/src/database.ts:39

Properties

config

readonly config: Partial <ReadConfig & SignerConfig> & Partial <AutoWaitConfig>

Source

@tableland/sdk/src/database.ts:30

Methods

batch()

batch<T>(statements, controller?): Promise <Result<T>[]>

Execute a set of Statements in batch mode. Batching sends multiple SQL statements inside a single call to the network. This can have a huge performance impact, as it only sends one transaction to the Tableland smart contract, thereby reducing gas costs. Batched statements are similar to SQL transactions. If a statement in the sequence fails, then an error is returned for that specific statement, and it aborts or rolls back the entire sequence.

Type parameters

T = D

Parameters

statements: Statement<unknown>[]

A set of Statement objects to batch and submit.

controller?: PollingController

An optional object used to control receipt polling behavior.

Returns

Promise <Result<T>[]>

An array of run results.

Source

@tableland/sdk/src/database.ts:82


dump()

dump(_controller?): Promise<ArrayBuffer>

Export a (set of) tables to the SQLite binary format. Not implemented yet!

Parameters

_controller?: PollingController

Returns

Promise<ArrayBuffer>

Source

@tableland/sdk/src/database.ts:207


exec()

exec<T>(statementStrings, controller?): Promise <ExecResult<T>>

Executes one or more queries directly without prepared statements or parameters binding. This method can have poorer performance (prepared statements can be reused in some cases) and, more importantly, is less safe. Only use this method for maintenance and one-shot tasks (example: migration jobs). The input can be one or multiple queries separated by the standard ;. If an error occurs, an exception is thrown with the query and error messages (see below for Errors). Currently, the entire string of statements is submitted as a single transaction. In the future, more "intelligent" transaction planning, splitting, and batching may be used.

Type parameters

T = D

Parameters

statementStrings: string

A set of SQL statement strings separated by semi-colons.

controller?: PollingController

An optional object used to control receipt polling behavior.

Returns

Promise <ExecResult<T>>

A single run result.

Source

@tableland/sdk/src/database.ts:183


prepare()

prepare<T>(sql): Statement<T>

Create a new prepared statement. Both static and prepared statements are supported. In the current implementation, the prepared statements are prepared locally, and executed remotely (on-chain).

Type parameters

T = D

Parameters

sql: string

The SQL statement string to prepare.

Returns

Statement<T>

A Statement object constructed with the given SQL string.

Source

@tableland/sdk/src/database.ts:61


forSigner()

static forSigner(signer): Promise <Database<unknown>>

Create a Database that is connected to the given Signer.

Parameters

signer: Signer

An ethersjs Signer to use for mutating queries.

Returns

Promise <Database<unknown>>

A Database with a Signer, and a default baseUrl.

Source

@tableland/sdk/src/database.ts:48