Synchronous query runners¶
Some query runners support executing queries synchronously if you provide a Promise implementation that supports it, like synchronous-promise.
Supported query runners that connect to a database
Supported general purposes query runners
Unwrapping synchronous promises¶
ts-sql-query ships a sync helper that unwraps the result of a synchronous promise in a blocking manner, similar to how await unwraps regular promises. When combined with a promise implementation like synchronous-promise — which resolves synchronously and does not defer .then execution — and one of the supported query runners listed above, it lets you interact with ts-sql-query in a fully synchronous style.
The promise passed to sync() must be truly synchronous — typically a database operation wrapped with SynchronousPromise. If sync() detects that the operation has not resolved (or rejected) by the time .then(...) returns, it throws an error, preventing misuse.
No need to re-implement sync()
Previously, the recommendation was to copy a sync() implementation into your own codebase. That is no longer necessary — import it from ts-sql-query (or its ts-sql-query/extras/sync subpath) instead.
Usage Example¶
import { BetterSqlite3QueryRunner } from "ts-sql-query/queryRunners/BetterSqlite3QueryRunner";
import { sync } from "ts-sql-query"; // or "ts-sql-query/extras/sync"
import Database from "better-sqlite3";
import { SynchronousPromise } from "synchronous-promise";
const db = new Database('foobar.db', options);
function main() {
const connection = new DBConnection(new BetterSqlite3QueryRunner(db, { promise: SynchronousPromise }));
// Do your queries here, surrounding each one with the sync function. For example:
const selectCompanies = sync(
connection.selectFrom(tCompany)
.where(tCompany.id.equals(123))
.select({
id: tCompany.id,
name: tCompany.name
})
.executeSelectMany()
);
selectCompanies // ...
// var result = sync(connection.insertInto...)
// result = sync(connection.update...)
// result = sync(connection.delete...)
}