pqb 0.57.3 → 0.57.5

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/index.mjs CHANGED
@@ -3618,11 +3618,11 @@ const isQueryReturnsAll = (q) => !q.q.returnType || q.q.returnType === "all";
3618
3618
  function simpleColumnToSQL(ctx, key, column, quotedAs) {
3619
3619
  if (!column) return `"${key}"`;
3620
3620
  const { data } = column;
3621
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
3621
+ return data.computed ? `(${data.computed.toSQL(ctx, quotedAs)})` : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
3622
3622
  }
3623
3623
  function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
3624
3624
  const { data } = column;
3625
- return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
3625
+ return data.computed ? `(${data.computed.toSQL(ctx, quotedAs)})` : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
3626
3626
  }
3627
3627
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
3628
3628
  const index = column.indexOf(".");
@@ -3677,7 +3677,7 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
3677
3677
  return `"${tableName}"."${col.data.name}"`;
3678
3678
  }
3679
3679
  if (col.data.computed) {
3680
- return col.data.computed.toSQL(ctx, quoted);
3680
+ return `(${col.data.computed.toSQL(ctx, quoted)})`;
3681
3681
  }
3682
3682
  return `"${tableName}"."${key}"`;
3683
3683
  }
@@ -3717,7 +3717,7 @@ const tableColumnToSqlWithAs = (ctx, data, column, table, key, as, quotedAs, sel
3717
3717
  return `"${tableName}"."${col.data.name}" "${as}"`;
3718
3718
  }
3719
3719
  if (col.data.computed) {
3720
- return `${col.data.computed.toSQL(ctx, quoted)} "${as}"`;
3720
+ return `(${col.data.computed.toSQL(ctx, quoted)}) "${as}"`;
3721
3721
  }
3722
3722
  }
3723
3723
  return `"${tableName}"."${key}"${key === as ? "" : ` "${as}"`}`;
@@ -3730,7 +3730,7 @@ const ownColumnToSqlWithAs = (ctx, data, column, as, quotedAs, select, jsonList)
3730
3730
  return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}"${col.data.name === as ? "" : ` "${as}"`}`;
3731
3731
  }
3732
3732
  if (col.data.computed) {
3733
- return `${col.data.computed.toSQL(ctx, quotedAs)} "${as}"`;
3733
+ return `(${col.data.computed.toSQL(ctx, quotedAs)}) "${as}"`;
3734
3734
  }
3735
3735
  }
3736
3736
  return `${quotedAs ? `${quotedAs}.` : ""}"${column}"${column === as ? "" : ` "${as}"`}`;
@@ -3756,7 +3756,7 @@ const makeRowToJson = (table, shape, aliasName) => {
3756
3756
  `'${key}', "${table}"."${aliasName && column.data.name || key}"${column.data.jsonCast ? `::${column.data.jsonCast}` : ""}`
3757
3757
  );
3758
3758
  }
3759
- return isSimple ? `row_to_json("${table}".*)` : `CASE WHEN "${table}".* IS NULL THEN NULL ELSE json_build_object(` + list.join(", ") + ") END";
3759
+ return isSimple ? `row_to_json("${table}".*)` : `CASE WHEN to_jsonb("${table}") IS NULL THEN NULL ELSE json_build_object(` + list.join(", ") + ") END";
3760
3760
  };
3761
3761
 
3762
3762
  const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
@@ -4245,7 +4245,8 @@ const skipQueryKeysForSubQuery = {
4245
4245
  catchAfterCommitErrors: true,
4246
4246
  log: true,
4247
4247
  logger: true,
4248
- autoPreparedStatements: true
4248
+ autoPreparedStatements: true,
4249
+ catch: true
4249
4250
  };
4250
4251
  const getIsJoinSubQuery = (query) => {
4251
4252
  const {
@@ -6167,7 +6168,20 @@ const queryMethodByReturnType = {
6167
6168
  class Then {
6168
6169
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
6169
6170
  catch(fn) {
6170
- return this.then(void 0, fn);
6171
+ const q = _clone(this);
6172
+ q.q.catch = true;
6173
+ return q.then(void 0, fn);
6174
+ }
6175
+ catchUniqueError(fn) {
6176
+ const q = _clone(this);
6177
+ q.q.catch = true;
6178
+ return q.then(void 0, (err) => {
6179
+ if (err instanceof QueryError && err.isUnique) {
6180
+ return fn(err);
6181
+ } else {
6182
+ throw err;
6183
+ }
6184
+ });
6171
6185
  }
6172
6186
  }
6173
6187
  let queryError = void 0;
@@ -6186,6 +6200,7 @@ Object.defineProperty(Then.prototype, "then", {
6186
6200
  });
6187
6201
  function maybeWrappedThen(resolve, reject) {
6188
6202
  const { q } = this;
6203
+ const shouldCatch = q.catch || !!reject;
6189
6204
  let beforeHooks;
6190
6205
  let afterHooks;
6191
6206
  let afterCommitHooks;
@@ -6217,7 +6232,8 @@ function maybeWrappedThen(resolve, reject) {
6217
6232
  afterHooks,
6218
6233
  afterCommitHooks,
6219
6234
  resolve2,
6220
- reject2
6235
+ reject2,
6236
+ shouldCatch
6221
6237
  );
6222
6238
  })
6223
6239
  ).then(resolve, reject);
@@ -6230,7 +6246,8 @@ function maybeWrappedThen(resolve, reject) {
6230
6246
  afterHooks,
6231
6247
  afterCommitHooks,
6232
6248
  resolve,
6233
- reject
6249
+ reject,
6250
+ shouldCatch
6234
6251
  );
6235
6252
  }
6236
6253
  }
@@ -6240,7 +6257,7 @@ const callAfterHook = function(cb) {
6240
6257
  return cb(this[0], this[1]);
6241
6258
  };
6242
6259
  const beginSql = { text: "BEGIN" };
6243
- const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks, resolve, reject) => {
6260
+ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks, resolve, reject, shouldCatch) => {
6244
6261
  var _a;
6245
6262
  const { q: query } = q;
6246
6263
  let sql;
@@ -6273,7 +6290,8 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
6273
6290
  queryResult = await execQuery(
6274
6291
  adapter,
6275
6292
  queryMethodByReturnType[tempReturnType],
6276
- sql
6293
+ sql,
6294
+ shouldCatch && trx
6277
6295
  );
6278
6296
  if (log) {
6279
6297
  log.afterQuery(sql, logData);
@@ -6303,7 +6321,12 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
6303
6321
  if (log) {
6304
6322
  logData = log.beforeQuery(sql);
6305
6323
  }
6306
- const result2 = await execQuery(adapter, queryMethod, sql);
6324
+ const result2 = await execQuery(
6325
+ adapter,
6326
+ queryMethod,
6327
+ sql,
6328
+ shouldCatch && trx
6329
+ );
6307
6330
  if (queryResult) {
6308
6331
  queryResult.rowCount += result2.rowCount;
6309
6332
  queryResult.rows.push(...result2.rows);
@@ -6575,8 +6598,13 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
6575
6598
  throw error;
6576
6599
  }
6577
6600
  };
6578
- const execQuery = (adapter, method, sql) => {
6579
- return adapter[method](sql.text, sql.values).then((result) => {
6601
+ const execQuery = (adapter, method, sql, catchTrx) => {
6602
+ const catchingSavepoint = catchTrx ? String(catchTrx.catchI = (catchTrx.catchI || 0) + 1) : void 0;
6603
+ return adapter[method](
6604
+ sql.text,
6605
+ sql.values,
6606
+ catchingSavepoint
6607
+ ).then((result) => {
6580
6608
  if (result.rowCount && !result.rows.length) {
6581
6609
  result.rows.length = result.rowCount;
6582
6610
  result.rows.fill({});