pqb 0.56.11 → 0.56.12

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
  }
@@ -7121,7 +7153,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7121
7153
  };
7122
7154
  function selectedObjectToSQL(ctx, quotedAs, item) {
7123
7155
  const sql = item.toSQL(ctx, quotedAs);
7124
- return ctx.aliasValue ? `${sql} r` : sql;
7156
+ return ctx.aliasValue ? `${sql} "${quotedAs}"` : sql;
7125
7157
  }
7126
7158
  const selectAllSql = (query, quotedAs, jsonList) => {
7127
7159
  if (jsonList) {
@@ -7169,14 +7201,14 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
7169
7201
  break;
7170
7202
  }
7171
7203
  case "all":
7172
- case "pluck":
7173
7204
  case "value":
7205
+ case "pluck":
7174
7206
  case "rows":
7175
- sql = `"${query.q.joinedForSelect}".r`;
7207
+ sql = `"${query.q.joinedForSelect}"."${as}"`;
7176
7208
  break;
7177
7209
  case "valueOrThrow":
7178
7210
  if (query.q.returning) return;
7179
- sql = `"${query.q.joinedForSelect}".r`;
7211
+ sql = `"${query.q.joinedForSelect}"."${as}"`;
7180
7212
  break;
7181
7213
  case "void":
7182
7214
  return;
@@ -10762,11 +10794,12 @@ class Join {
10762
10794
  */
10763
10795
  joinLateral(arg, cb) {
10764
10796
  const q = _clone(this);
10765
- return _joinLateral(
10797
+ _joinLateral(
10766
10798
  q,
10767
10799
  "JOIN",
10768
10800
  _joinLateralProcessArg(q, arg, cb)
10769
10801
  );
10802
+ return q;
10770
10803
  }
10771
10804
  /**
10772
10805
  * The same as {@link joinLateral}, but when no records found for the join it will result in `null`:
@@ -10785,11 +10818,12 @@ class Join {
10785
10818
  */
10786
10819
  leftJoinLateral(arg, cb) {
10787
10820
  const q = _clone(this);
10788
- return _joinLateral(
10789
- _clone(this),
10821
+ _joinLateral(
10822
+ q,
10790
10823
  "LEFT JOIN",
10791
10824
  _joinLateralProcessArg(q, arg, cb)
10792
10825
  );
10826
+ return q;
10793
10827
  }
10794
10828
  /**
10795
10829
  * This method may be useful