pqb 0.71.0 → 0.71.2

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.js CHANGED
@@ -319,6 +319,17 @@ var Expression = class {
319
319
  }
320
320
  return sql;
321
321
  }
322
+ /**
323
+ * Transform the expression value after loading it.
324
+ *
325
+ * It is meant to transform expressions selected by a query.
326
+ *
327
+ * @param fn - function to transform expression value with
328
+ */
329
+ transform(fn) {
330
+ pushOrNewArrayToObjectImmutable(this.q, "transform", fn);
331
+ return this;
332
+ }
322
333
  };
323
334
  const isExpression = (arg) => arg instanceof Expression;
324
335
  const isTemplateLiteralArgs = (args) => Array.isArray(args[0]) && "raw" in args[0] && Array.isArray(args[0].raw);
@@ -391,7 +402,7 @@ const templateLiteralToSQL = (template, ctx, quotedAs) => {
391
402
  for (let last = parts.length - 1; i < last; i++) {
392
403
  sql += parts[i];
393
404
  const value = template[i + 1];
394
- if (value instanceof Expression) sql += value.toSQL(ctx, quotedAs);
405
+ if (isExpression(value)) sql += value.toSQL(ctx, quotedAs);
395
406
  else {
396
407
  values.push(value);
397
408
  literalValues.push(sql.length);
@@ -1436,6 +1447,7 @@ const indexInnerToCode = (index, t) => {
1436
1447
  "name",
1437
1448
  "using",
1438
1449
  "nullsNotDistinct",
1450
+ "deferrable",
1439
1451
  "include",
1440
1452
  "with",
1441
1453
  "tablespace",
@@ -1617,6 +1629,7 @@ const columnIndexesToCode = (items) => {
1617
1629
  options.using && `using: ${singleQuote(options.using)},`,
1618
1630
  options.include && `include: ${typeof options.include === "string" ? singleQuote(options.include) : `[${options.include.map(singleQuote).join(", ")}]`},`,
1619
1631
  options.nullsNotDistinct && `nullsNotDistinct: true,`,
1632
+ options.deferrable && `deferrable: ${singleQuote(options.deferrable)},`,
1620
1633
  options.with && `with: ${singleQuote(options.with)},`,
1621
1634
  options.tablespace && `tablespace: ${singleQuote(options.tablespace)},`,
1622
1635
  options.where && `where: ${singleQuote(options.where)},`
@@ -5619,20 +5632,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, shape, whereExists,
5619
5632
  const args0 = args.length ? args[0] : returnArg;
5620
5633
  if (typeof args0 === "function") {
5621
5634
  let q = first;
5622
- if (q.joinQueryAfterCallback) {
5623
- let base = q.baseQuery;
5624
- if (q.q.as) base = base.as(q.q.as);
5625
- const { q: query } = q.joinQueryAfterCallback(base, joinTo);
5626
- if (query.and || query.or || query.scopes) {
5627
- q = _clone(q);
5628
- if (query.and) pushQueryArrayImmutable(q, "and", query.and);
5629
- if (query.or) pushQueryArrayImmutable(q, "or", query.or);
5630
- if (query.scopes) q.q.scopes = {
5631
- ...q.q.scopes,
5632
- ...query.scopes
5633
- };
5634
- }
5635
- }
5635
+ if (q.joinQueryAfterCallback) q = q.joinQueryAfterCallback(q, joinTo);
5636
5636
  const joinedShapes = {
5637
5637
  ...joinTo.q.joinedShapes,
5638
5638
  [joinTo.q.as || joinTo.table]: joinTo.shape
@@ -7677,7 +7677,7 @@ const encodeRow = (ctx, values, QueryClass, row, runtimeDefaults, quotedAs, hook
7677
7677
  };
7678
7678
  const encodeValue = (ctx, values, QueryClass, value, quotedAs) => {
7679
7679
  if (value && typeof value === "object") {
7680
- if (value instanceof Expression) return value.toSQL(ctx, quotedAs);
7680
+ if (isExpression(value)) return value.toSQL(ctx, quotedAs);
7681
7681
  else if (value instanceof QueryClass) return `(${moveMutativeQueryToCte(ctx, value)})`;
7682
7682
  else if ("fromHook" in value) return value.fromHook;
7683
7683
  }
@@ -7996,7 +7996,7 @@ var FnExpression = class extends Expression {
7996
7996
  const q = this.q;
7997
7997
  sql.push(this.args.map((arg) => {
7998
7998
  if (typeof arg === "string") return arg === "*" ? "*" : fnArgToSql(ctx, q, arg, quotedAs);
7999
- else if (arg instanceof Expression) return arg.toSQL(ctx, quotedAs);
7999
+ else if (isExpression(arg)) return arg.toSQL(ctx, quotedAs);
8000
8000
  else if ("pairs" in arg) {
8001
8001
  const args = [];
8002
8002
  const { pairs } = arg;
@@ -10969,6 +10969,7 @@ const processNestedSelectPathEntry = (batches, path, thisKey, thisReturnType, i,
10969
10969
  key: thisKey
10970
10970
  });
10971
10971
  else {
10972
+ if (!data) return;
10972
10973
  const { key, returnType } = path[++i];
10973
10974
  if (!thisReturnType || thisReturnType === "all") for (const row of data) processNestedSelectPathEntry(batches, path, key, returnType, i, last, row);
10974
10975
  else processNestedSelectPathEntry(batches, path, key, returnType, i, last, data);
@@ -10999,7 +11000,8 @@ const processSelectAsArg = (q, selectAs, as, key, arg, columnAlias, outerReturnT
10999
11000
  }
11000
11001
  if (isExpression(value)) column = value.result.value;
11001
11002
  else {
11002
- if (isRelationQuery(value) && value.q.subQuery !== 1) {
11003
+ const isSelfReferencingQuery = value.q.subQuery === 1;
11004
+ if (isRelationQuery(value) && !isSelfReferencingQuery) {
11003
11005
  joinQuery = true;
11004
11006
  setSelectRelation(query.q);
11005
11007
  value = value.joinQuery(value, q);
@@ -11020,17 +11022,17 @@ const processSelectAsArg = (q, selectAs, as, key, arg, columnAlias, outerReturnT
11020
11022
  if (as) value.q.joinedForSelect = _copyQueryAliasToQuery(value, q, as);
11021
11023
  }
11022
11024
  if (value.q.getColumn?.data.skipValueToArray) value.q.notFoundDefault ??= null;
11023
- else if (!value.q.type && (value.q.returnType === "value" || value.q.returnType === "valueOrThrow")) {
11025
+ else if (!value.q.type && (value.q.returnType === "value" || value.q.returnType === "valueOrThrow") && !isSelfReferencingQuery) {
11024
11026
  const column = Object.create(value.q.getColumn || UnknownColumn.instance);
11025
11027
  column.data = {
11026
11028
  ...column.data,
11027
- name: void 0,
11028
11029
  valueToArray: true
11029
11030
  };
11031
+ if (joinQuery) column.data.name = void 0;
11030
11032
  value.q.getColumn = column;
11031
11033
  if (value.q.expr) value.q.expr.q.getColumn = column;
11032
11034
  }
11033
- if (outerReturnType === "value" && value.q.returnType === "valueOrThrow") value.q.returnType = "value";
11035
+ if ((outerReturnType === "value" || isSelfReferencingQuery) && value.q.returnType === "valueOrThrow") value.q.returnType = "value";
11034
11036
  column = value.q.getColumn;
11035
11037
  value = prepareSubQueryForSql(q, value);
11036
11038
  }