SQLProvider.exec()method
Execute an SQL query built from a template literal and return the resulting rows.
exec(strings: TemplateStringsArray, ...values: ImmutableArray<unknown>): Promise<ImmutableArray<X>>
new SQLProvider<I, T>()
| Return | |
|---|---|
SQLProvider<I, T> | Abstract database provider that implements CRUD and query operations by generating and executing SQL. |
Abstract database provider that implements CRUD and query operations by generating and executing SQL.
exec() to run a query against a concrete database (e.g. SQLite, PostgreSQL).sql* helpers build composable SQLFragment objects so dialect differences can be overridden.UnimplementedError.The abstract SQL base provider. SQLProvider implements the DBProvider surface in terms of SQL, leaving one method for concrete subclasses to provide: exec<X>(strings, ...values), which runs a parameterised query and returns rows as plain objects.
Each scalar field in a collection schema maps to a generated column extracted from the data JSON blob, so queries use indexed column comparisons. SQLProvider supports integer (number with step: 1) and string identifiers. Realtime sequences are not supported — getItemSequence / getQuerySequence throw UnimplementedError.
SQLProvider is abstract — bind it to a driver by implementing exec(). The built-in SQLiteProvider and PostgreSQLProvider extend it with dialect-specific JSON path syntax; concrete drivers live in the shelving/cloudflare and shelving/bun modules.
import { SQLiteProvider } from "shelving/db";
// A concrete subclass implements exec() against a specific driver.
class D1Provider extends SQLiteProvider {
async exec(strings, ...values) {
return this.db.prepare(strings.join("?")).bind(...values).all();
}
}class MyProvider extends SQLProvider { async exec(strings, ...values) { ... } }
const item = await new MyProvider().getItem(collection, "abc");Execute an SQL query built from a template literal and return the resulting rows.
exec(strings: TemplateStringsArray, ...values: ImmutableArray<unknown>): Promise<ImmutableArray<X>>
Define an SQL fragment using Javascript template literal format.
sql(strings: TemplateStringsArray, ...values: ImmutableArray<unknown>): SQLFragment
Define an SQL fragment for an escaped identifier, e.g. "myTable".
sqlIdentifier(name: string): SQLFragment
Define an SQL fragment that extracts a value at a key for comparison, e.g. "a" #>> {"b","c"} in Postgres.
sqlExtract(key: Segments): SQLFragment
Define an SQL fragment that joins a series of fragments with a separator, e.g. "a" = 1 AND "b" = 2.
sqlConcat(values: ImmutableArray<SQLFragment>, separator = ", ", before = "", after = ""): SQLFragment
Define an SQL fragment for setting a list of values, e.g. "a" = 1, "b" = 2.
sqlSetters(data: TT): SQLFragment
Define an SQL fragment for a set of updates, e.g. "a" = 1, "b" = "b" + 5.
sqlUpdates(updates: Updates<TT>): SQLFragment
Define an SQL fragment for a single update action.
sqlUpdate({ action, key, value }: Update): SQLFragmentDefine an SQL fragment for VALUES syntax, e.g. ("a", "b") VALUES (1, 2).
sqlValues(data: Data): SQLFragment
Define an SQL fragment for the WHERE, ORDER BY and LIMIT clauses of a query, e.g. WHERE x = 1 ORDER BY "name" LIMIT 0, 50.
sqlClauses(query: Query<Item>): void
Define an SQL fragment for a WHERE clause, e.g. WHERE x = 1 AND y <= 100.
sqlWhere(query: Query<Item>): void
Define an SQL fragment for a single filter clause on a column, e.g. x = 1 or x IN (1, 2).
sqlFilter({ key, operator, value }: QueryFilter): SQLFragmentDefine an SQL fragment for an ORDER BY clause, e.g. ORDER BY "a" ASC, "b" DESC.
sqlOrder(query: Query<Item>): void
Define an SQL fragment for an individual column in an ORDER BY, e.g. "a" ASC.
sqlSort({ key, direction }: QueryOrder): SQLFragmentDefine an SQL fragment for a LIMIT clause, e.g. LIMIT 50.
sqlLimit(query: Query<Item>): void