Skip to content
Tauri
Releases

@tauri-apps/plugin-sql

Classes

default

Database

The Database class serves as the primary interface for communicating with the rust side of the sql plugin.

Constructors

constructor()
new default(path): default
Parameters
ParameterType
pathstring
Returns

default

Source: index.ts:29

Properties

PropertyType
pathstring

Methods

close()
close(db?): Promise< boolean >

close

Closes the database connection pool.

Example
const success = await db.close();
Parameters
ParameterTypeDescription
db?stringOptionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
Returns

Promise< boolean >

Source: index.ts:161


execute()
execute(query, bindValues?): Promise< QueryResult >

execute

Passes a SQL expression to the database for execution.

Example
// for sqlite & postgres
// INSERT example
const result = await db.execute('INSERT into todos (id, title, status) VALUES ($1, $2, $3)', [
todos.id,
todos.title,
todos.status,
]);
// UPDATE example
const result = await db.execute('UPDATE todos SET title = $1, completed = $2 WHERE id = $3', [
todos.title,
todos.status,
todos.id,
]);
// for mysql
// INSERT example
const result = await db.execute('INSERT into todos (id, title, status) VALUES (?, ?, ?)', [
todos.id,
todos.title,
todos.status,
]);
// UPDATE example
const result = await db.execute('UPDATE todos SET title = ?, completed = ? WHERE id = ?', [
todos.title,
todos.status,
todos.id,
]);
Parameters
ParameterType
querystring
bindValues?unknown[]
Returns

Promise< QueryResult >

Source: index.ts:108


select()
select<T>(query, bindValues?): Promise< T >

select

Passes in a SELECT query to the database for execution.

Example
// for sqlite & postgres
const result = await db.select('SELECT * from todos WHERE id = $1', id);
// for mysql
const result = await db.select('SELECT * from todos WHERE id = ?', id);
Type parameters
Parameter
T
Parameters
ParameterType
querystring
bindValues?unknown[]
Returns

Promise< T >

Source: index.ts:140


get()
static get(path): default

get

A static initializer which synchronously returns an instance of the Database class while deferring the actual database connection until the first invocation or selection on the database.

Sqlite

The path is relative to tauri::path::BaseDirectory::App and must start with sqlite:.

Example
const db = Database.get('sqlite:test.db');
Parameters
ParameterType
pathstring
Returns

default

Source: index.ts:72


load()
static load(path): Promise< default >

load

A static initializer which connects to the underlying database and returns a Database instance once a connection to the database is established.

Sqlite

The path is relative to tauri::path::BaseDirectory::App and must start with sqlite:.

Example
const db = await Database.load('sqlite:test.db');
Parameters
ParameterType
pathstring
Returns

Promise< default >

Source: index.ts:48

Interfaces

QueryResult

Properties

PropertyTypeDescription
lastInsertIdnumberThe last inserted id.

This value is always 0 when using the Postgres driver. If the
last inserted id is required on Postgres, the select function
must be used, with a RETURNING clause
(INSERT INTO todos (title) VALUES ($1) RETURNING id).
rowsAffectednumberThe number of rows affected by the query.

© 2024 Tauri Contributors. CC-BY / MIT