pqb 0.71.1 → 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);
@@ -7643,7 +7654,7 @@ const encodeRow = (ctx, values, QueryClass, row, runtimeDefaults, quotedAs, hook
7643
7654
  };
7644
7655
  const encodeValue = (ctx, values, QueryClass, value, quotedAs) => {
7645
7656
  if (value && typeof value === "object") {
7646
- if (value instanceof Expression) return value.toSQL(ctx, quotedAs);
7657
+ if (isExpression(value)) return value.toSQL(ctx, quotedAs);
7647
7658
  else if (value instanceof QueryClass) return `(${moveMutativeQueryToCte(ctx, value)})`;
7648
7659
  else if ("fromHook" in value) return value.fromHook;
7649
7660
  }
@@ -7962,7 +7973,7 @@ var FnExpression = class extends Expression {
7962
7973
  const q = this.q;
7963
7974
  sql.push(this.args.map((arg) => {
7964
7975
  if (typeof arg === "string") return arg === "*" ? "*" : fnArgToSql(ctx, q, arg, quotedAs);
7965
- else if (arg instanceof Expression) return arg.toSQL(ctx, quotedAs);
7976
+ else if (isExpression(arg)) return arg.toSQL(ctx, quotedAs);
7966
7977
  else if ("pairs" in arg) {
7967
7978
  const args = [];
7968
7979
  const { pairs } = arg;
@@ -10966,7 +10977,8 @@ const processSelectAsArg = (q, selectAs, as, key, arg, columnAlias, outerReturnT
10966
10977
  }
10967
10978
  if (isExpression(value)) column = value.result.value;
10968
10979
  else {
10969
- if (isRelationQuery(value) && value.q.subQuery !== 1) {
10980
+ const isSelfReferencingQuery = value.q.subQuery === 1;
10981
+ if (isRelationQuery(value) && !isSelfReferencingQuery) {
10970
10982
  joinQuery = true;
10971
10983
  setSelectRelation(query.q);
10972
10984
  value = value.joinQuery(value, q);
@@ -10987,17 +10999,17 @@ const processSelectAsArg = (q, selectAs, as, key, arg, columnAlias, outerReturnT
10987
10999
  if (as) value.q.joinedForSelect = _copyQueryAliasToQuery(value, q, as);
10988
11000
  }
10989
11001
  if (value.q.getColumn?.data.skipValueToArray) value.q.notFoundDefault ??= null;
10990
- 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) {
10991
11003
  const column = Object.create(value.q.getColumn || UnknownColumn.instance);
10992
11004
  column.data = {
10993
11005
  ...column.data,
10994
- name: void 0,
10995
11006
  valueToArray: true
10996
11007
  };
11008
+ if (joinQuery) column.data.name = void 0;
10997
11009
  value.q.getColumn = column;
10998
11010
  if (value.q.expr) value.q.expr.q.getColumn = column;
10999
11011
  }
11000
- if (outerReturnType === "value" && value.q.returnType === "valueOrThrow") value.q.returnType = "value";
11012
+ if ((outerReturnType === "value" || isSelfReferencingQuery) && value.q.returnType === "valueOrThrow") value.q.returnType = "value";
11001
11013
  column = value.q.getColumn;
11002
11014
  value = prepareSubQueryForSql(q, value);
11003
11015
  }