bun-query-builder 0.1.34 → 0.1.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bin/cli.js CHANGED
@@ -11973,6 +11973,43 @@ function createSQLiteSQL(filename) {
11973
11973
  return Promise.reject(error);
11974
11974
  }
11975
11975
  };
11976
+ let txDepth = 0;
11977
+ sqlFunction.begin = async (fnOrName, maybeFn) => {
11978
+ const fn = typeof fnOrName === "function" ? fnOrName : maybeFn;
11979
+ if (typeof fn !== "function")
11980
+ throw new TypeError("[query-builder] sqlite begin(): a transaction callback is required");
11981
+ const isSavepoint = txDepth > 0;
11982
+ const spName = `qb_sp_${txDepth}`;
11983
+ if (isSavepoint)
11984
+ wrapper.run(`SAVEPOINT ${spName}`);
11985
+ else
11986
+ wrapper.run("BEGIN");
11987
+ txDepth++;
11988
+ try {
11989
+ const result = await fn(sqlFunction);
11990
+ txDepth--;
11991
+ if (isSavepoint)
11992
+ wrapper.run(`RELEASE SAVEPOINT ${spName}`);
11993
+ else
11994
+ wrapper.run("COMMIT");
11995
+ return result;
11996
+ } catch (err) {
11997
+ txDepth--;
11998
+ try {
11999
+ if (isSavepoint) {
12000
+ wrapper.run(`ROLLBACK TO SAVEPOINT ${spName}`);
12001
+ wrapper.run(`RELEASE SAVEPOINT ${spName}`);
12002
+ } else {
12003
+ wrapper.run("ROLLBACK");
12004
+ }
12005
+ } catch {}
12006
+ throw err;
12007
+ }
12008
+ };
12009
+ sqlFunction.savepoint = sqlFunction.begin;
12010
+ sqlFunction.beginDistributed = async () => {
12011
+ throw new Error("[query-builder] beginDistributed() (two-phase commit) is not supported on SQLite. Use begin()/transaction().");
12012
+ };
11976
12013
  sqlFunction._wrapper = wrapper;
11977
12014
  return sqlFunction;
11978
12015
  }
@@ -15892,12 +15929,15 @@ function createQueryBuilder(state) {
15892
15929
  async transaction(fn, options) {
15893
15930
  const defaults = state?.txDefaults;
15894
15931
  const opts = { ...defaults, ...options };
15932
+ const txConn = state?.sql ?? bunSql;
15933
+ const nested = state?.inTransaction === true;
15934
+ const txMethod = nested && typeof txConn?.savepoint === "function" ? "savepoint" : "begin";
15895
15935
  const runWith = async (attempt2) => {
15896
15936
  opts.logger?.({ type: "start", attempt: attempt2 });
15897
15937
  const start = Date.now();
15898
- return await bunSql.begin(async (tx) => {
15899
- const qb = createQueryBuilder({ sql: tx, meta, schema });
15900
- if (opts?.isolation) {
15938
+ return await txConn[txMethod](async (tx) => {
15939
+ const qb = createQueryBuilder({ sql: tx, meta, schema, inTransaction: true });
15940
+ if (opts?.isolation && !nested) {
15901
15941
  const level = opts.isolation;
15902
15942
  const upper = level === "read committed" ? "READ COMMITTED" : level === "repeatable read" ? "REPEATABLE READ" : "SERIALIZABLE";
15903
15943
  if (config5.dialect === "postgres") {
@@ -15962,7 +16002,8 @@ function createQueryBuilder(state) {
15962
16002
  });
15963
16003
  },
15964
16004
  async beginDistributed(name, fn) {
15965
- const res = await bunSql.beginDistributed(name, async (tx) => {
16005
+ const txConn = state?.sql ?? bunSql;
16006
+ const res = await txConn.beginDistributed(name, async (tx) => {
15966
16007
  const qb = createQueryBuilder({ sql: tx, meta, schema });
15967
16008
  return await fn(qb);
15968
16009
  });
@@ -30112,7 +30153,7 @@ function getPrefix() {
30112
30153
  }
30113
30154
  var prefix = getPrefix();
30114
30155
  // package.json
30115
- var version2 = "0.1.34";
30156
+ var version2 = "0.1.35";
30116
30157
 
30117
30158
  // bin/cli.ts
30118
30159
  init_actions();
package/dist/client.d.ts CHANGED
@@ -386,6 +386,7 @@ declare interface InternalState {
386
386
  meta?: SchemaMeta
387
387
  schema?: any
388
388
  txDefaults?: TransactionOptions
389
+ inTransaction?: boolean
389
390
  }
390
391
  declare interface TxBackoff { baseMs?: number, maxMs?: number, factor?: number, jitter?: boolean }
391
392
  declare interface TxLoggerEvent { type: 'start' | 'retry' | 'commit' | 'rollback' | 'error', attempt: number, error?: any, durationMs?: number }
package/dist/src/index.js CHANGED
@@ -11973,6 +11973,43 @@ function createSQLiteSQL(filename) {
11973
11973
  return Promise.reject(error);
11974
11974
  }
11975
11975
  };
11976
+ let txDepth = 0;
11977
+ sqlFunction.begin = async (fnOrName, maybeFn) => {
11978
+ const fn = typeof fnOrName === "function" ? fnOrName : maybeFn;
11979
+ if (typeof fn !== "function")
11980
+ throw new TypeError("[query-builder] sqlite begin(): a transaction callback is required");
11981
+ const isSavepoint = txDepth > 0;
11982
+ const spName = `qb_sp_${txDepth}`;
11983
+ if (isSavepoint)
11984
+ wrapper.run(`SAVEPOINT ${spName}`);
11985
+ else
11986
+ wrapper.run("BEGIN");
11987
+ txDepth++;
11988
+ try {
11989
+ const result = await fn(sqlFunction);
11990
+ txDepth--;
11991
+ if (isSavepoint)
11992
+ wrapper.run(`RELEASE SAVEPOINT ${spName}`);
11993
+ else
11994
+ wrapper.run("COMMIT");
11995
+ return result;
11996
+ } catch (err) {
11997
+ txDepth--;
11998
+ try {
11999
+ if (isSavepoint) {
12000
+ wrapper.run(`ROLLBACK TO SAVEPOINT ${spName}`);
12001
+ wrapper.run(`RELEASE SAVEPOINT ${spName}`);
12002
+ } else {
12003
+ wrapper.run("ROLLBACK");
12004
+ }
12005
+ } catch {}
12006
+ throw err;
12007
+ }
12008
+ };
12009
+ sqlFunction.savepoint = sqlFunction.begin;
12010
+ sqlFunction.beginDistributed = async () => {
12011
+ throw new Error("[query-builder] beginDistributed() (two-phase commit) is not supported on SQLite. Use begin()/transaction().");
12012
+ };
11976
12013
  sqlFunction._wrapper = wrapper;
11977
12014
  return sqlFunction;
11978
12015
  }
@@ -15892,12 +15929,15 @@ function createQueryBuilder(state) {
15892
15929
  async transaction(fn, options) {
15893
15930
  const defaults = state?.txDefaults;
15894
15931
  const opts = { ...defaults, ...options };
15932
+ const txConn = state?.sql ?? bunSql;
15933
+ const nested = state?.inTransaction === true;
15934
+ const txMethod = nested && typeof txConn?.savepoint === "function" ? "savepoint" : "begin";
15895
15935
  const runWith = async (attempt2) => {
15896
15936
  opts.logger?.({ type: "start", attempt: attempt2 });
15897
15937
  const start = Date.now();
15898
- return await bunSql.begin(async (tx) => {
15899
- const qb = createQueryBuilder({ sql: tx, meta, schema });
15900
- if (opts?.isolation) {
15938
+ return await txConn[txMethod](async (tx) => {
15939
+ const qb = createQueryBuilder({ sql: tx, meta, schema, inTransaction: true });
15940
+ if (opts?.isolation && !nested) {
15901
15941
  const level = opts.isolation;
15902
15942
  const upper = level === "read committed" ? "READ COMMITTED" : level === "repeatable read" ? "REPEATABLE READ" : "SERIALIZABLE";
15903
15943
  if (config5.dialect === "postgres") {
@@ -15962,7 +16002,8 @@ function createQueryBuilder(state) {
15962
16002
  });
15963
16003
  },
15964
16004
  async beginDistributed(name, fn) {
15965
- const res = await bunSql.beginDistributed(name, async (tx) => {
16005
+ const txConn = state?.sql ?? bunSql;
16006
+ const res = await txConn.beginDistributed(name, async (tx) => {
15966
16007
  const qb = createQueryBuilder({ sql: tx, meta, schema });
15967
16008
  return await fn(qb);
15968
16009
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.1.34",
4
+ "version": "0.1.35",
5
5
  "description": "A simple yet performant query builder for TypeScript. Built with Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",