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.mjs CHANGED
@@ -296,6 +296,17 @@ var Expression = class {
296
296
  }
297
297
  return sql;
298
298
  }
299
+ /**
300
+ * Transform the expression value after loading it.
301
+ *
302
+ * It is meant to transform expressions selected by a query.
303
+ *
304
+ * @param fn - function to transform expression value with
305
+ */
306
+ transform(fn) {
307
+ pushOrNewArrayToObjectImmutable(this.q, "transform", fn);
308
+ return this;
309
+ }
299
310
  };
300
311
  const isExpression = (arg) => arg instanceof Expression;
301
312
  const isTemplateLiteralArgs = (args) => Array.isArray(args[0]) && "raw" in args[0] && Array.isArray(args[0].raw);
@@ -368,7 +379,7 @@ const templateLiteralToSQL = (template, ctx, quotedAs) => {
368
379
  for (let last = parts.length - 1; i < last; i++) {
369
380
  sql += parts[i];
370
381
  const value = template[i + 1];
371
- if (value instanceof Expression) sql += value.toSQL(ctx, quotedAs);
382
+ if (isExpression(value)) sql += value.toSQL(ctx, quotedAs);
372
383
  else {
373
384
  values.push(value);
374
385
  literalValues.push(sql.length);
@@ -1413,6 +1424,7 @@ const indexInnerToCode = (index, t) => {
1413
1424
  "name",
1414
1425
  "using",
1415
1426
  "nullsNotDistinct",
1427
+ "deferrable",
1416
1428
  "include",
1417
1429
  "with",
1418
1430
  "tablespace",
@@ -1594,6 +1606,7 @@ const columnIndexesToCode = (items) => {
1594
1606
  options.using && `using: ${singleQuote(options.using)},`,
1595
1607
  options.include && `include: ${typeof options.include === "string" ? singleQuote(options.include) : `[${options.include.map(singleQuote).join(", ")}]`},`,
1596
1608
  options.nullsNotDistinct && `nullsNotDistinct: true,`,
1609
+ options.deferrable && `deferrable: ${singleQuote(options.deferrable)},`,
1597
1610
  options.with && `with: ${singleQuote(options.with)},`,
1598
1611
  options.tablespace && `tablespace: ${singleQuote(options.tablespace)},`,
1599
1612
  options.where && `where: ${singleQuote(options.where)},`
@@ -5596,20 +5609,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, shape, whereExists,
5596
5609
  const args0 = args.length ? args[0] : returnArg;
5597
5610
  if (typeof args0 === "function") {
5598
5611
  let q = first;
5599
- if (q.joinQueryAfterCallback) {
5600
- let base = q.baseQuery;
5601
- if (q.q.as) base = base.as(q.q.as);
5602
- const { q: query } = q.joinQueryAfterCallback(base, joinTo);
5603
- if (query.and || query.or || query.scopes) {
5604
- q = _clone(q);
5605
- if (query.and) pushQueryArrayImmutable(q, "and", query.and);
5606
- if (query.or) pushQueryArrayImmutable(q, "or", query.or);
5607
- if (query.scopes) q.q.scopes = {
5608
- ...q.q.scopes,
5609
- ...query.scopes
5610
- };
5611
- }
5612
- }
5612
+ if (q.joinQueryAfterCallback) q = q.joinQueryAfterCallback(q, joinTo);
5613
5613
  const joinedShapes = {
5614
5614
  ...joinTo.q.joinedShapes,
5615
5615
  [joinTo.q.as || joinTo.table]: joinTo.shape
@@ -7654,7 +7654,7 @@ const encodeRow = (ctx, values, QueryClass, row, runtimeDefaults, quotedAs, hook
7654
7654
  };
7655
7655
  const encodeValue = (ctx, values, QueryClass, value, quotedAs) => {
7656
7656
  if (value && typeof value === "object") {
7657
- if (value instanceof Expression) return value.toSQL(ctx, quotedAs);
7657
+ if (isExpression(value)) return value.toSQL(ctx, quotedAs);
7658
7658
  else if (value instanceof QueryClass) return `(${moveMutativeQueryToCte(ctx, value)})`;
7659
7659
  else if ("fromHook" in value) return value.fromHook;
7660
7660
  }
@@ -7973,7 +7973,7 @@ var FnExpression = class extends Expression {
7973
7973
  const q = this.q;
7974
7974
  sql.push(this.args.map((arg) => {
7975
7975
  if (typeof arg === "string") return arg === "*" ? "*" : fnArgToSql(ctx, q, arg, quotedAs);
7976
- else if (arg instanceof Expression) return arg.toSQL(ctx, quotedAs);
7976
+ else if (isExpression(arg)) return arg.toSQL(ctx, quotedAs);
7977
7977
  else if ("pairs" in arg) {
7978
7978
  const args = [];
7979
7979
  const { pairs } = arg;
@@ -10946,6 +10946,7 @@ const processNestedSelectPathEntry = (batches, path, thisKey, thisReturnType, i,
10946
10946
  key: thisKey
10947
10947
  });
10948
10948
  else {
10949
+ if (!data) return;
10949
10950
  const { key, returnType } = path[++i];
10950
10951
  if (!thisReturnType || thisReturnType === "all") for (const row of data) processNestedSelectPathEntry(batches, path, key, returnType, i, last, row);
10951
10952
  else processNestedSelectPathEntry(batches, path, key, returnType, i, last, data);
@@ -10976,7 +10977,8 @@ const processSelectAsArg = (q, selectAs, as, key, arg, columnAlias, outerReturnT
10976
10977
  }
10977
10978
  if (isExpression(value)) column = value.result.value;
10978
10979
  else {
10979
- if (isRelationQuery(value) && value.q.subQuery !== 1) {
10980
+ const isSelfReferencingQuery = value.q.subQuery === 1;
10981
+ if (isRelationQuery(value) && !isSelfReferencingQuery) {
10980
10982
  joinQuery = true;
10981
10983
  setSelectRelation(query.q);
10982
10984
  value = value.joinQuery(value, q);
@@ -10997,17 +10999,17 @@ const processSelectAsArg = (q, selectAs, as, key, arg, columnAlias, outerReturnT
10997
10999
  if (as) value.q.joinedForSelect = _copyQueryAliasToQuery(value, q, as);
10998
11000
  }
10999
11001
  if (value.q.getColumn?.data.skipValueToArray) value.q.notFoundDefault ??= null;
11000
- else if (!value.q.type && (value.q.returnType === "value" || value.q.returnType === "valueOrThrow")) {
11002
+ else if (!value.q.type && (value.q.returnType === "value" || value.q.returnType === "valueOrThrow") && !isSelfReferencingQuery) {
11001
11003
  const column = Object.create(value.q.getColumn || UnknownColumn.instance);
11002
11004
  column.data = {
11003
11005
  ...column.data,
11004
- name: void 0,
11005
11006
  valueToArray: true
11006
11007
  };
11008
+ if (joinQuery) column.data.name = void 0;
11007
11009
  value.q.getColumn = column;
11008
11010
  if (value.q.expr) value.q.expr.q.getColumn = column;
11009
11011
  }
11010
- if (outerReturnType === "value" && value.q.returnType === "valueOrThrow") value.q.returnType = "value";
11012
+ if ((outerReturnType === "value" || isSelfReferencingQuery) && value.q.returnType === "valueOrThrow") value.q.returnType = "value";
11011
11013
  column = value.q.getColumn;
11012
11014
  value = prepareSubQueryForSql(q, value);
11013
11015
  }