feat: init

This commit is contained in:
2026-02-13 22:02:30 +01:00
commit 8f9ff830fb
16711 changed files with 3307340 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
import type { Hyperdrive } from "@cloudflare/workers-types";
export declare function getHyperdrive(bindingName: string): Promise<Hyperdrive>;

View File

@@ -0,0 +1,11 @@
function getCloudflareEnv() {
return globalThis.__env__ || import("cloudflare:workers").then((mod) => mod.env);
}
export async function getHyperdrive(bindingName) {
const env = await getCloudflareEnv();
const binding = env[bindingName];
if (!binding) {
throw new Error(`[db0] [hyperdrive] binding \`${bindingName}\` not found`);
}
return binding;
}

View File

@@ -0,0 +1,11 @@
import type { Primitive, Statement, PreparedStatement } from "db0";
export declare abstract class BoundableStatement<T> implements Statement {
_statement: T;
constructor(rawStmt: T);
bind(...params: Primitive[]): PreparedStatement;
abstract all(...params: Primitive[]): Promise<unknown[]>;
abstract run(...params: Primitive[]): Promise<{
success: boolean;
}>;
abstract get(...params: Primitive[]): Promise<unknown>;
}

View File

@@ -0,0 +1,29 @@
export class BoundableStatement {
_statement;
constructor(rawStmt) {
this._statement = rawStmt;
}
bind(...params) {
return new BoundStatement(this, params);
}
}
class BoundStatement {
#statement;
#params;
constructor(statement, params) {
this.#statement = statement;
this.#params = params;
}
bind(...params) {
return new BoundStatement(this.#statement, params);
}
all() {
return this.#statement.all(...this.#params);
}
run() {
return this.#statement.run(...this.#params);
}
get() {
return this.#statement.get(...this.#params);
}
}

View File

@@ -0,0 +1,8 @@
import Database from "better-sqlite3";
import type { Connector } from "db0";
export interface ConnectorOptions {
cwd?: string;
path?: string;
name?: string;
}
export default function sqliteConnector(opts: ConnectorOptions): Connector<Database.Database>;

46
node_modules/db0/dist/connectors/better-sqlite3.mjs generated vendored Normal file
View File

@@ -0,0 +1,46 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import Database from "better-sqlite3";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function sqliteConnector(opts) {
let _db;
const getDB = () => {
if (_db) {
return _db;
}
if (opts.name === ":memory:") {
_db = new Database(":memory:");
return _db;
}
const filePath = resolve(opts.cwd || ".", opts.path || `.data/${opts.name || "db"}.sqlite3`);
mkdirSync(dirname(filePath), { recursive: true });
_db = new Database(filePath);
return _db;
};
return {
name: "sqlite",
dialect: "sqlite",
getInstance: () => getDB(),
exec: (sql) => getDB().exec(sql),
prepare: (sql) => new StatementWrapper(() => getDB().prepare(sql)),
dispose: () => {
_db?.close?.();
_db = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
async all(...params) {
return this._statement().all(...params);
}
async run(...params) {
const res = this._statement().run(...params);
return {
success: res.changes > 0,
...res
};
}
async get(...params) {
return this._statement().get(...params);
}
}

8
node_modules/db0/dist/connectors/bun-sqlite.d.mts generated vendored Normal file
View File

@@ -0,0 +1,8 @@
import { Database } from "bun:sqlite";
import type { Connector } from "db0";
export interface ConnectorOptions {
cwd?: string;
path?: string;
name?: string;
}
export default function bunSqliteConnector(opts: ConnectorOptions): Connector<Database>;

46
node_modules/db0/dist/connectors/bun-sqlite.mjs generated vendored Normal file
View File

@@ -0,0 +1,46 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import { Database } from "bun:sqlite";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function bunSqliteConnector(opts) {
let _db;
const getDB = () => {
if (_db) {
return _db;
}
if (opts.name === ":memory:") {
_db = new Database(":memory:");
} else {
const filePath = resolve(opts.cwd || ".", opts.path || `.data/${opts.name || "db"}.bun.sqlite`);
mkdirSync(dirname(filePath), { recursive: true });
_db = new Database(filePath);
}
return _db;
};
return {
name: "sqlite",
dialect: "sqlite",
getInstance: () => getDB(),
exec: (sql) => getDB().exec(sql),
prepare: (sql) => new StatementWrapper(getDB().prepare(sql)),
dispose: () => {
_db?.close?.();
_db = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
all(...params) {
return Promise.resolve(this._statement.all(...params));
}
run(...params) {
const res = this._statement.run(...params);
return Promise.resolve({
success: true,
...res
});
}
get(...params) {
return Promise.resolve(this._statement.get(...params));
}
}

6
node_modules/db0/dist/connectors/cloudflare-d1.d.mts generated vendored Normal file
View File

@@ -0,0 +1,6 @@
import type { D1Database } from "@cloudflare/workers-types";
import type { Connector } from "db0";
export interface ConnectorOptions {
bindingName?: string;
}
export default function cloudflareD1Connector(options: ConnectorOptions): Connector<D1Database>;

32
node_modules/db0/dist/connectors/cloudflare-d1.mjs generated vendored Normal file
View File

@@ -0,0 +1,32 @@
import { BoundableStatement } from "./_internal/statement.mjs";
export default function cloudflareD1Connector(options) {
const getDB = () => {
// TODO: Remove legacy __cf_env__ support in next major version
const binding = globalThis.__env__?.[options.bindingName] || globalThis.__cf_env__?.[options.bindingName];
if (!binding) {
throw new Error(`[db0] [d1] binding \`${options.bindingName}\` not found`);
}
return binding;
};
return {
name: "cloudflare-d1",
dialect: "sqlite",
getInstance: () => getDB(),
exec: (sql) => getDB().exec(sql),
prepare: (sql) => new StatementWrapper(getDB().prepare(sql))
};
}
class StatementWrapper extends BoundableStatement {
async all(...params) {
const res = await this._statement.bind(...params).all();
return res.results;
}
async run(...params) {
const res = await this._statement.bind(...params).run();
return res;
}
async get(...params) {
const res = await this._statement.bind(...params).first();
return res;
}
}

View File

@@ -0,0 +1,7 @@
import mysql from "mysql2/promise";
import type { Connector } from "db0";
type OmitMysqlConfig = Omit<mysql.ConnectionOptions, "user" | "database" | "password" | "password1" | "password2" | "password3" | "port" | "host" | "uri" | "localAddress" | "socketPath" | "insecureAuth" | "passwordSha1" | "disableEval">;
export type ConnectorOptions = {
bindingName: string;
} & OmitMysqlConfig;
export default function cloudflareHyperdriveMysqlConnector(opts: ConnectorOptions): Connector<mysql.Connection>;

View File

@@ -0,0 +1,58 @@
import mysql from "mysql2/promise";
import { BoundableStatement } from "./_internal/statement.mjs";
import { getHyperdrive } from "./_internal/cloudflare.mjs";
export default function cloudflareHyperdriveMysqlConnector(opts) {
let _connection;
const getConnection = async () => {
if (_connection) {
return _connection;
}
const hyperdrive = await getHyperdrive(opts.bindingName);
_connection = await mysql.createConnection({
...opts,
host: hyperdrive.host,
user: hyperdrive.user,
password: hyperdrive.password,
database: hyperdrive.database,
port: hyperdrive.port,
disableEval: true
});
return _connection;
};
const query = (sql, params) => getConnection().then((c) => c.query(sql, params)).then((res) => res[0]);
return {
name: "cloudflare-hyperdrive-mysql",
dialect: "mysql",
getInstance: () => getConnection(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: async () => {
await _connection?.end?.();
_connection = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const res = await this.#query(this.#sql, params);
return res;
}
async run(...params) {
const res = await this.#query(this.#sql, params);
return {
success: true,
...res
};
}
async get(...params) {
const res = await this.#query(this.#sql, params);
return res[0];
}
}

View File

@@ -0,0 +1,7 @@
import pg from "pg";
import type { Connector } from "db0";
type OmitPgConfig = Omit<pg.ClientConfig, "user" | "database" | "password" | "port" | "host" | "connectionString">;
export type ConnectorOptions = {
bindingName: string;
} & OmitPgConfig;
export default function cloudflareHyperdrivePostgresqlConnector(opts: ConnectorOptions): Connector<pg.Client>;

View File

@@ -0,0 +1,65 @@
import pg from "pg";
import { BoundableStatement } from "./_internal/statement.mjs";
import { getHyperdrive } from "./_internal/cloudflare.mjs";
export default function cloudflareHyperdrivePostgresqlConnector(opts) {
let _client;
async function getClient() {
if (_client) {
return _client;
}
const hyperdrive = await getHyperdrive(opts.bindingName);
const client = new pg.Client({
...opts,
connectionString: hyperdrive.connectionString
});
_client = client.connect().then(() => {
_client = client;
return _client;
});
return _client;
}
const query = async (sql, params) => {
const client = await getClient();
return client.query(normalizeParams(sql), params);
};
return {
name: "cloudflare-hyperdrive-postgresql",
dialect: "postgresql",
getInstance: () => getClient(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: async () => {
await (await _client)?.end?.();
_client = undefined;
}
};
}
// https://www.postgresql.org/docs/9.3/sql-prepare.html
function normalizeParams(sql) {
let i = 0;
return sql.replace(/\?/g, () => `$${++i}`);
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const res = await this.#query(this.#sql, params);
return res.rows;
}
async run(...params) {
const res = await this.#query(this.#sql, params);
return {
success: true,
...res
};
}
async get(...params) {
const res = await this.#query(this.#sql, params);
return res.rows[0];
}
}

7
node_modules/db0/dist/connectors/libsql/core.d.mts generated vendored Normal file
View File

@@ -0,0 +1,7 @@
import type { Client } from "@libsql/client";
import type { Connector } from "db0";
export type ConnectorOptions = {
getClient: () => Client;
name?: string;
};
export default function libSqlCoreConnector(opts: ConnectorOptions): Connector<Client>;

44
node_modules/db0/dist/connectors/libsql/core.mjs generated vendored Normal file
View File

@@ -0,0 +1,44 @@
import { BoundableStatement } from "../_internal/statement.mjs";
export default function libSqlCoreConnector(opts) {
const query = (sql) => opts.getClient().execute(sql);
return {
name: opts.name || "libsql-core",
dialect: "libsql",
getInstance: async () => opts.getClient(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: () => {
opts.getClient()?.close?.();
}
};
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const res = await this.#query({
sql: this.#sql,
args: params
});
return res.rows;
}
async run(...params) {
const res = await this.#query({
sql: this.#sql,
args: params
});
return { ...res };
}
async get(...params) {
const res = await this.#query({
sql: this.#sql,
args: params
});
return res.rows[0];
}
}

4
node_modules/db0/dist/connectors/libsql/http.d.mts generated vendored Normal file
View File

@@ -0,0 +1,4 @@
import type { Config, Client } from "@libsql/client";
import type { Connector } from "db0";
export type ConnectorOptions = Config;
export default function libSqlConnector(opts: ConnectorOptions): Connector<Client>;

15
node_modules/db0/dist/connectors/libsql/http.mjs generated vendored Normal file
View File

@@ -0,0 +1,15 @@
import { createClient } from "@libsql/client/http";
import libSqlCore from "./core.mjs";
export default function libSqlConnector(opts) {
let _client;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
}
return _client;
};
return libSqlCore({
name: "libsql-http",
getClient
});
}

4
node_modules/db0/dist/connectors/libsql/node.d.mts generated vendored Normal file
View File

@@ -0,0 +1,4 @@
import type { Config, Client } from "@libsql/client";
import type { Connector } from "db0";
export type ConnectorOptions = Config;
export default function libSqlConnector(opts: ConnectorOptions): Connector<Client>;

15
node_modules/db0/dist/connectors/libsql/node.mjs generated vendored Normal file
View File

@@ -0,0 +1,15 @@
import { createClient } from "@libsql/client";
import libSqlCore from "./core.mjs";
export default function libSqlConnector(opts) {
let _client;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
}
return _client;
};
return libSqlCore({
name: "libsql-node",
getClient
});
}

4
node_modules/db0/dist/connectors/libsql/web.d.mts generated vendored Normal file
View File

@@ -0,0 +1,4 @@
import type { Config, Client } from "@libsql/client";
import type { Connector } from "db0";
export type ConnectorOptions = Config;
export default function libSqlConnector(opts: ConnectorOptions): Connector<Client>;

15
node_modules/db0/dist/connectors/libsql/web.mjs generated vendored Normal file
View File

@@ -0,0 +1,15 @@
import { createClient } from "@libsql/client/http";
import libSqlCore from "./core.mjs";
export default function libSqlConnector(opts) {
let _client;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
}
return _client;
};
return libSqlCore({
name: "libsql-web",
getClient
});
}

4
node_modules/db0/dist/connectors/mysql2.d.mts generated vendored Normal file
View File

@@ -0,0 +1,4 @@
import mysql from "mysql2/promise";
import type { Connector } from "db0";
export type ConnectorOptions = mysql.ConnectionOptions;
export default function mysqlConnector(opts: ConnectorOptions): Connector<mysql.Connection>;

48
node_modules/db0/dist/connectors/mysql2.mjs generated vendored Normal file
View File

@@ -0,0 +1,48 @@
import mysql from "mysql2/promise";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function mysqlConnector(opts) {
let _connection;
const getConnection = async () => {
if (_connection) {
return _connection;
}
_connection = await mysql.createConnection({ ...opts });
return _connection;
};
const query = (sql, params) => getConnection().then((c) => c.query(sql, params)).then((res) => res[0]);
return {
name: "mysql",
dialect: "mysql",
getInstance: () => getConnection(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: async () => {
await _connection?.end?.();
_connection = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const res = await this.#query(this.#sql, params);
return res;
}
async run(...params) {
const res = await this.#query(this.#sql, params);
return {
success: true,
...res
};
}
async get(...params) {
const res = await this.#query(this.#sql, params);
return res[0];
}
}

8
node_modules/db0/dist/connectors/node-sqlite.d.mts generated vendored Normal file
View File

@@ -0,0 +1,8 @@
import type { Connector } from "db0";
import type { DatabaseSync } from "node:sqlite";
export interface ConnectorOptions {
cwd?: string;
path?: string;
name?: string;
}
export default function nodeSqlite3Connector(opts: ConnectorOptions): Connector<DatabaseSync>;

54
node_modules/db0/dist/connectors/node-sqlite.mjs generated vendored Normal file
View File

@@ -0,0 +1,54 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function nodeSqlite3Connector(opts) {
let _db;
const getDB = () => {
if (_db) {
return _db;
}
const nodeSqlite = globalThis.process?.getBuiltinModule?.("node:sqlite");
if (!nodeSqlite) {
throw new Error("`node:sqlite` module is not available. Please ensure you are running in Node.js >= 22.5 or Deno >= 2.2.");
}
if (opts.name === ":memory:") {
_db = new nodeSqlite.DatabaseSync(":memory:");
return _db;
}
const filePath = resolve(opts.cwd || ".", opts.path || `.data/${opts.name || "db"}.sqlite`);
mkdirSync(dirname(filePath), { recursive: true });
_db = new nodeSqlite.DatabaseSync(filePath);
return _db;
};
return {
name: "node-sqlite",
dialect: "sqlite",
getInstance: () => getDB(),
exec(sql) {
getDB().exec(sql);
return { success: true };
},
prepare: (sql) => new StatementWrapper(() => getDB().prepare(sql)),
dispose: () => {
_db?.close?.();
_db = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
async all(...params) {
const raws = this._statement().all(...params);
return raws;
}
async run(...params) {
const res = this._statement().run(...params);
return {
success: true,
...res
};
}
async get(...params) {
const raw = this._statement().get(...params);
return raw;
}
}

5
node_modules/db0/dist/connectors/pglite.d.mts generated vendored Normal file
View File

@@ -0,0 +1,5 @@
import type { PGliteOptions, PGliteInterfaceExtensions } from "@electric-sql/pglite";
import { PGlite } from "@electric-sql/pglite";
import type { Connector } from "db0";
export type ConnectorOptions = PGliteOptions;
export default function pgliteConnector<TOptions extends ConnectorOptions>(opts?: TOptions): Connector<PGlite & PGliteInterfaceExtensions<TOptions["extensions"]>>;

54
node_modules/db0/dist/connectors/pglite.mjs generated vendored Normal file
View File

@@ -0,0 +1,54 @@
import { PGlite } from "@electric-sql/pglite";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function pgliteConnector(opts) {
let _client;
function getClient() {
return _client ||= PGlite.create(opts).then((res) => _client = res);
}
const query = async (sql, params) => {
const client = await getClient();
const normalizedSql = normalizeParams(sql);
const result = await client.query(normalizedSql, params);
return result;
};
return {
name: "pglite",
dialect: "postgresql",
getInstance: () => getClient(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: async () => {
await (await _client)?.close?.();
_client = undefined;
}
};
}
// https://www.postgresql.org/docs/9.3/sql-prepare.html
function normalizeParams(sql) {
let i = 0;
return sql.replace(/\?/g, () => `$${++i}`);
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const result = await this.#query(this.#sql, params);
return result.rows;
}
async run(...params) {
const result = await this.#query(this.#sql, params);
return {
success: true,
...result
};
}
async get(...params) {
const result = await this.#query(this.#sql, params);
return result.rows[0];
}
}

4
node_modules/db0/dist/connectors/planetscale.d.mts generated vendored Normal file
View File

@@ -0,0 +1,4 @@
import { Client, type Config } from "@planetscale/database";
import type { Connector } from "db0";
export type ConnectorOptions = Config;
export default function planetscaleConnector(opts: ConnectorOptions): Connector<Client>;

50
node_modules/db0/dist/connectors/planetscale.mjs generated vendored Normal file
View File

@@ -0,0 +1,50 @@
import { Client } from "@planetscale/database";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function planetscaleConnector(opts) {
let _client;
function getClient() {
if (_client) {
return _client;
}
const client = new Client(opts);
_client = client;
return client;
}
// Discussion on how @planetscale/database client works:
// https://github.com/drizzle-team/drizzle-orm/issues/1743#issuecomment-1879479647
const query = (sql, params) => getClient().execute(sql, params);
return {
name: "planetscale",
dialect: "mysql",
getInstance: () => getClient(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: () => {
_client = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const res = await this.#query(this.#sql, params);
return res.rows;
}
async run(...params) {
const res = await this.#query(this.#sql, params);
return {
success: true,
...res
};
}
async get(...params) {
const res = await this.#query(this.#sql, params);
return res.rows[0];
}
}

6
node_modules/db0/dist/connectors/postgresql.d.mts generated vendored Normal file
View File

@@ -0,0 +1,6 @@
import pg from "pg";
import type { Connector } from "db0";
export type ConnectorOptions = {
url: string;
} | pg.ClientConfig;
export default function postgresqlConnector(opts: ConnectorOptions): Connector<pg.Client>;

60
node_modules/db0/dist/connectors/postgresql.mjs generated vendored Normal file
View File

@@ -0,0 +1,60 @@
import pg from "pg";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function postgresqlConnector(opts) {
let _client;
function getClient() {
if (_client) {
return _client;
}
const client = new pg.Client("url" in opts ? opts.url : opts);
_client = client.connect().then(() => {
_client = client;
return _client;
});
return _client;
}
const query = async (sql, params) => {
const client = await getClient();
return client.query(normalizeParams(sql), params);
};
return {
name: "postgresql",
dialect: "postgresql",
getInstance: () => getClient(),
exec: (sql) => query(sql),
prepare: (sql) => new StatementWrapper(sql, query),
dispose: async () => {
await (await _client)?.end?.();
_client = undefined;
}
};
}
// https://www.postgresql.org/docs/9.3/sql-prepare.html
function normalizeParams(sql) {
let i = 0;
return sql.replace(/\?/g, () => `$${++i}`);
}
class StatementWrapper extends BoundableStatement {
#query;
#sql;
constructor(sql, query) {
super();
this.#sql = sql;
this.#query = query;
}
async all(...params) {
const res = await this.#query(this.#sql, params);
return res.rows;
}
async run(...params) {
const res = await this.#query(this.#sql, params);
return {
success: true,
...res
};
}
async get(...params) {
const res = await this.#query(this.#sql, params);
return res.rows[0];
}
}

8
node_modules/db0/dist/connectors/sqlite3.d.mts generated vendored Normal file
View File

@@ -0,0 +1,8 @@
import sqlite3 from "sqlite3";
import type { Connector } from "db0";
export interface ConnectorOptions {
cwd?: string;
path?: string;
name?: string;
}
export default function nodeSqlite3Connector(opts: ConnectorOptions): Connector<sqlite3.Database>;

88
node_modules/db0/dist/connectors/sqlite3.mjs generated vendored Normal file
View File

@@ -0,0 +1,88 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import sqlite3 from "sqlite3";
import { BoundableStatement } from "./_internal/statement.mjs";
export default function nodeSqlite3Connector(opts) {
let _db;
const _activeStatements = new Set();
const getDB = () => {
if (_db) {
return _db;
}
if (opts.name === ":memory:") {
_db = new sqlite3.Database(":memory:");
return _db;
}
const filePath = resolve(opts.cwd || ".", opts.path || `.data/${opts.name || "db"}.sqlite3`);
mkdirSync(dirname(filePath), { recursive: true });
_db = new sqlite3.Database(filePath);
return _db;
};
const query = (sql) => new Promise((resolve, reject) => {
getDB().exec(sql, (err) => {
if (err) {
return reject(err);
}
resolve({ success: true });
});
});
return {
name: "sqlite3",
dialect: "sqlite",
getInstance: () => getDB(),
exec: (sql) => query(sql),
prepare: (sql) => {
const stmt = new StatementWrapper(sql, getDB());
_activeStatements.add(stmt);
return stmt;
},
dispose: async () => {
await Promise.all([..._activeStatements].map((s) => s.finalize().catch((error) => {
console.warn("[db0] [sqlite3] failed to finalize statement", error);
})));
_activeStatements.clear();
await new Promise((resolve, reject) => _db?.close?.((error) => error ? reject(error) : resolve()));
_db = undefined;
}
};
}
class StatementWrapper extends BoundableStatement {
#onError;
constructor(sql, db) {
super(db.prepare(sql, (err) => {
if (err && this.#onError) {
return this.#onError(err);
}
}));
}
async all(...params) {
const rows = await new Promise((resolve, reject) => {
this.#onError = reject;
this._statement.all(...params, (err, rows) => err ? reject(err) : resolve(rows));
});
return rows;
}
async run(...params) {
await new Promise((resolve, reject) => {
this.#onError = reject;
this._statement.run(...params, (err) => err ? reject(err) : resolve());
});
return { success: true };
}
async get(...params) {
const row = await new Promise((resolve, reject) => {
this.#onError = reject;
this._statement.get(...params, (err, row) => err ? reject(err) : resolve(row));
});
return row;
}
finalize() {
try {
// TODO: Can we await on finalize cb?
this._statement.finalize();
return Promise.resolve();
} catch (error) {
return Promise.reject(error);
}
}
}