Modules

Docs / API / Modules / db

Database

Persistence

@s2script/sdk/db

engine-generic async SQLite database.

Import

import { SqlValue } from "@s2script/sdk/db";

Author-time types ship in the npm package; the engine injects the runtime at plugin load. Add @s2script/sdk to your plugin's dependencies.

API reference

Generated from the shipped type definitions.

type
SqlValue

A value bindable as a SQL parameter or returned in a Row.

type
Row

One query result row — a column-name→SqlValue map.

interface
ExecuteResult

The outcome of a non-SELECT statement run via Database.execute.

changes: number
Rows affected by the statement (INSERT/UPDATE/DELETE).
lastInsertId: number
Rowid of the last inserted row (0 if the statement inserted nothing).
interface
DriverConnection

A driver-owned connection — what a Driver hands back from Driver.connect.

query(sql: string, params?: SqlValue[]): Promise<Row[]>
Run a SELECT and resolve every result Row; params bind ? placeholders.
execute(sql: string, params?: SqlValue[]): Promise<ExecuteResult>
Run a non-SELECT statement and resolve its ExecuteResult; params bind ? placeholders.
close(): Promise<void>
Close the underlying connection.
interface
ConnectionConfig

Named-connection config resolved by Database.open and passed to Driver.connect.

driver: string
Driver name to connect with (e.g. "sqlite").
name: string
The connection's configured name.
interface
Driver

A registered database backend; register a custom one via Database.registerDriver.

readonly name: string
The driver's name, matched against ConnectionConfig.driver.
connect(config: ConnectionConfig): Promise<DriverConnection>
Open a connection for config, resolving a DriverConnection.
interface
Database

A live database connection (delegates to its driver).

query(sql: string, params?: SqlValue[]): Promise<Row[]>
Run a SELECT and resolve every result Row; params bind ? placeholders.
execute(sql: string, params?: SqlValue[]): Promise<ExecuteResult>
Run a non-SELECT statement and resolve its ExecuteResult; params bind ? placeholders.
close(): Promise<void>
Close the connection.
const
Database

Entry point for opening databases and registering drivers.

open(name?: string): Promise<Database>

Open a connection by name (default "default"). Resolves the driver + config, then connects.

name
Connection name from config; omit for "default".

ReturnsResolves the connected Database handle.

ThrowsRejects if the name is unknown or the driver fails to connect.

import { Database } from "@s2script/sdk/db";
const db = await Database.open("clientprefs");
await db.execute("CREATE TABLE IF NOT EXISTS cookies (steamid TEXT, name TEXT, value TEXT)");
const rows = await db.query("SELECT value FROM cookies WHERE steamid = ?", ["76561199999999999"]);
registerDriver(driver: Driver): void
Register a custom driver (per-plugin context). SQLite is built in.
Back to modules All packages

s2script — Source 2 plugin framework

GitHub