pqb 0.56.11 → 0.56.13

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.d.ts CHANGED
@@ -400,6 +400,10 @@ interface QueryData extends QueryDataBase {
400
400
  distinct?: SelectableOrExpression[];
401
401
  only?: boolean;
402
402
  join?: JoinItem[];
403
+ joinValueDedup?: Map<string, {
404
+ q: Query;
405
+ a: string;
406
+ }>;
403
407
  group?: (string | Expression)[];
404
408
  having?: HavingItem[];
405
409
  window?: WindowItem[];
@@ -3259,8 +3263,8 @@ interface DefaultSchemaConfig extends ColumnSchemaConfig<ColumnType> {
3259
3263
  }) => Types): {
3260
3264
  [K in keyof T]: K extends keyof Types ? Types[K] : T[K];
3261
3265
  };
3262
- narrowType<T extends ColumnInputOutputQueryTypes, Types extends ColumnInputOutputQueryTypes>(this: T, _fn: (type: <Type extends T['inputType'] & T['outputType'] & T['queryType']>() => {
3263
- inputType: Type;
3266
+ narrowType<T extends ColumnInputOutputQueryTypes, Types extends ColumnInputOutputQueryTypes>(this: T, _fn: (type: <Type extends T['inputType'] extends T['outputType'] & T['queryType'] ? T['outputType'] & T['queryType'] : T['inputType'] & T['outputType'] & T['queryType']>() => {
3267
+ inputType: T['inputType'] extends never ? never : Type;
3264
3268
  outputType: Type;
3265
3269
  queryType: Type;
3266
3270
  }) => Types): {
package/dist/index.js CHANGED
@@ -1080,6 +1080,9 @@ const quoteValue = (arg, ctx, quotedAs, IN) => {
1080
1080
  if ("toSQL" in arg) {
1081
1081
  return `(${getSqlText(arg.toSQL(ctx))})`;
1082
1082
  }
1083
+ if (!(arg instanceof Date) && !Array.isArray(arg)) {
1084
+ arg = JSON.stringify(arg);
1085
+ }
1083
1086
  }
1084
1087
  return orchidCore.addValue(ctx.values, arg);
1085
1088
  };
@@ -2594,7 +2597,7 @@ const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
2594
2597
  );
2595
2598
  }
2596
2599
  if (!select && data.joinedShapes?.[column]) {
2597
- return `"${column}".r`;
2600
+ return `"${column}"."${column}"`;
2598
2601
  }
2599
2602
  return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
2600
2603
  };
@@ -2604,7 +2607,7 @@ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
2604
2607
  return columnWithDotToSql(ctx, data, data.shape, column, index, quotedAs);
2605
2608
  } else {
2606
2609
  if (data.joinedShapes?.[column]) {
2607
- return `"${column}".r`;
2610
+ return `"${column}"."${column}"`;
2608
2611
  }
2609
2612
  if (data.select) {
2610
2613
  for (const s of data.select) {
@@ -2909,7 +2912,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2909
2912
  quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
2910
2913
  } else {
2911
2914
  column = query.joinedShapes?.[key]?.value;
2912
- quotedColumn = `"${key}".r`;
2915
+ quotedColumn = `"${key}"."${key}"`;
2913
2916
  }
2914
2917
  if (!column || !quotedColumn) {
2915
2918
  throw new Error(`Unknown column ${key} provided to condition`);
@@ -2982,7 +2985,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
2982
2985
  query,
2983
2986
  args.a
2984
2987
  )}"`;
2985
- on = `${args.i ? `"${args.a}".r IS NOT NULL` : "true"}`;
2988
+ on = `${args.i ? `"${args.a}"."${args.a}" IS NOT NULL` : "true"}`;
2986
2989
  ctx.aliasValue = aliasValue;
2987
2990
  } else if ("j" in args) {
2988
2991
  const { j, s, r } = args;
@@ -4564,11 +4567,38 @@ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
4564
4567
  }
4565
4568
  as || (as = getQueryAs(arg));
4566
4569
  orchidCore.setObjectValueImmutable(q.q, "joinedComputeds", as, arg.q.runtimeComputeds);
4570
+ const joinArgs = {
4571
+ l: arg,
4572
+ a: as,
4573
+ i: innerJoinLateral
4574
+ };
4575
+ if (arg.q.returnType === "value" || arg.q.returnType === "valueOrThrow") {
4576
+ const map = q.q.joinValueDedup ? new Map(q.q.joinValueDedup) : /* @__PURE__ */ new Map();
4577
+ q.q.joinValueDedup = map;
4578
+ const select = arg.q.select[0];
4579
+ arg.q.select = [];
4580
+ const dedupKey = getSqlText(arg.toSQL());
4581
+ const existing = map.get(dedupKey);
4582
+ if (existing) {
4583
+ existing.q.q.select = [
4584
+ {
4585
+ selectAs: {
4586
+ ...existing.q.q.select[0].selectAs,
4587
+ [joinKey]: select
4588
+ }
4589
+ }
4590
+ ];
4591
+ return existing.a;
4592
+ } else {
4593
+ arg.q.select = [{ selectAs: { [joinKey]: select } }];
4594
+ map.set(dedupKey, { q: arg, a: as });
4595
+ }
4596
+ }
4567
4597
  orchidCore.pushQueryValueImmutable(q, "join", {
4568
4598
  type: `${type} LATERAL`,
4569
- args: { l: arg, a: as, i: innerJoinLateral }
4599
+ args: joinArgs
4570
4600
  });
4571
- return q;
4601
+ return joinKey;
4572
4602
  };
4573
4603
 
4574
4604
  const escape = (value, migration, nested) => {
@@ -6026,12 +6056,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
6026
6056
  subQuery = value;
6027
6057
  }
6028
6058
  }
6029
- value.q.joinedForSelect = orchidCore._copyQueryAliasToQuery(
6030
- value,
6031
- q,
6032
- key
6033
- );
6034
- _joinLateral(
6059
+ const as2 = _joinLateral(
6035
6060
  q,
6036
6061
  innerJoinLateral ? "JOIN" : "LEFT JOIN",
6037
6062
  subQuery,
@@ -6040,6 +6065,13 @@ const processSelectArg = (q, as, arg, columnAs) => {
6040
6065
  // `JOIN` will handle it on itself.
6041
6066
  innerJoinLateral && returnType !== "one" && returnType !== "oneOrThrow"
6042
6067
  );
6068
+ if (as2) {
6069
+ value.q.joinedForSelect = orchidCore._copyQueryAliasToQuery(
6070
+ value,
6071
+ q,
6072
+ as2
6073
+ );
6074
+ }
6043
6075
  }
6044
6076
  }
6045
6077
  selectAs[key] = addParserForSelectItem(
@@ -6164,7 +6196,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
6164
6196
  const { returnType } = it.q;
6165
6197
  if (returnType === "value" || returnType === "valueOrThrow") {
6166
6198
  const type = it.q.getColumn;
6167
- result[key] = type || UnknownColumn.instance;
6199
+ result[key] = type ? mapSubSelectColumn(type, isSubQuery) : UnknownColumn.instance;
6168
6200
  } else {
6169
6201
  result[key] = new JSONTextColumn(defaultSchemaConfig);
6170
6202
  }
@@ -7061,7 +7093,8 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7061
7093
  }
7062
7094
  }
7063
7095
  } else {
7064
- list.push(selectedObjectToSQL(ctx, quotedAs, item));
7096
+ const sql = item.toSQL(ctx, quotedAs);
7097
+ list.push(ctx.aliasValue ? `${sql} ${quotedAs}` : sql);
7065
7098
  aliases?.push("");
7066
7099
  }
7067
7100
  }
@@ -7119,10 +7152,6 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7119
7152
  }
7120
7153
  return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs, jsonList);
7121
7154
  };
7122
- function selectedObjectToSQL(ctx, quotedAs, item) {
7123
- const sql = item.toSQL(ctx, quotedAs);
7124
- return ctx.aliasValue ? `${sql} r` : sql;
7125
- }
7126
7155
  const selectAllSql = (query, quotedAs, jsonList) => {
7127
7156
  if (jsonList) {
7128
7157
  Object.assign(jsonList, query.selectAllShape);
@@ -7169,14 +7198,14 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
7169
7198
  break;
7170
7199
  }
7171
7200
  case "all":
7172
- case "pluck":
7173
7201
  case "value":
7202
+ case "pluck":
7174
7203
  case "rows":
7175
- sql = `"${query.q.joinedForSelect}".r`;
7204
+ sql = `"${query.q.joinedForSelect}"."${as}"`;
7176
7205
  break;
7177
7206
  case "valueOrThrow":
7178
7207
  if (query.q.returning) return;
7179
- sql = `"${query.q.joinedForSelect}".r`;
7208
+ sql = `"${query.q.joinedForSelect}"."${as}"`;
7180
7209
  break;
7181
7210
  case "void":
7182
7211
  return;
@@ -10762,11 +10791,12 @@ class Join {
10762
10791
  */
10763
10792
  joinLateral(arg, cb) {
10764
10793
  const q = _clone(this);
10765
- return _joinLateral(
10794
+ _joinLateral(
10766
10795
  q,
10767
10796
  "JOIN",
10768
10797
  _joinLateralProcessArg(q, arg, cb)
10769
10798
  );
10799
+ return q;
10770
10800
  }
10771
10801
  /**
10772
10802
  * The same as {@link joinLateral}, but when no records found for the join it will result in `null`:
@@ -10785,11 +10815,12 @@ class Join {
10785
10815
  */
10786
10816
  leftJoinLateral(arg, cb) {
10787
10817
  const q = _clone(this);
10788
- return _joinLateral(
10789
- _clone(this),
10818
+ _joinLateral(
10819
+ q,
10790
10820
  "LEFT JOIN",
10791
10821
  _joinLateralProcessArg(q, arg, cb)
10792
10822
  );
10823
+ return q;
10793
10824
  }
10794
10825
  /**
10795
10826
  * This method may be useful