pqb 0.34.0 → 0.35.1

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
@@ -90,7 +90,7 @@ class DynamicRawSQL extends orchidCore.Expression {
90
90
  super();
91
91
  this.fn = fn;
92
92
  this.result = orchidCore.emptyObject;
93
- this.q = {};
93
+ this.q = { expr: this };
94
94
  }
95
95
  // Calls the given function to get SQL from it.
96
96
  makeSQL(ctx, quotedAs) {
@@ -119,25 +119,25 @@ const sqlFn = (...args) => {
119
119
  return (...args2) => new RawSQL(args2, arg);
120
120
  };
121
121
 
122
- var __defProp$h = Object.defineProperty;
123
- var __defProps$9 = Object.defineProperties;
124
- var __getOwnPropDescs$9 = Object.getOwnPropertyDescriptors;
125
- var __getOwnPropSymbols$i = Object.getOwnPropertySymbols;
126
- var __hasOwnProp$i = Object.prototype.hasOwnProperty;
127
- var __propIsEnum$i = Object.prototype.propertyIsEnumerable;
128
- var __defNormalProp$h = (obj, key, value) => key in obj ? __defProp$h(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
129
- var __spreadValues$h = (a, b) => {
122
+ var __defProp$l = Object.defineProperty;
123
+ var __defProps$b = Object.defineProperties;
124
+ var __getOwnPropDescs$b = Object.getOwnPropertyDescriptors;
125
+ var __getOwnPropSymbols$m = Object.getOwnPropertySymbols;
126
+ var __hasOwnProp$m = Object.prototype.hasOwnProperty;
127
+ var __propIsEnum$m = Object.prototype.propertyIsEnumerable;
128
+ var __defNormalProp$l = (obj, key, value) => key in obj ? __defProp$l(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
129
+ var __spreadValues$l = (a, b) => {
130
130
  for (var prop in b || (b = {}))
131
- if (__hasOwnProp$i.call(b, prop))
132
- __defNormalProp$h(a, prop, b[prop]);
133
- if (__getOwnPropSymbols$i)
134
- for (var prop of __getOwnPropSymbols$i(b)) {
135
- if (__propIsEnum$i.call(b, prop))
136
- __defNormalProp$h(a, prop, b[prop]);
131
+ if (__hasOwnProp$m.call(b, prop))
132
+ __defNormalProp$l(a, prop, b[prop]);
133
+ if (__getOwnPropSymbols$m)
134
+ for (var prop of __getOwnPropSymbols$m(b)) {
135
+ if (__propIsEnum$m.call(b, prop))
136
+ __defNormalProp$l(a, prop, b[prop]);
137
137
  }
138
138
  return a;
139
139
  };
140
- var __spreadProps$9 = (a, b) => __defProps$9(a, __getOwnPropDescs$9(b));
140
+ var __spreadProps$b = (a, b) => __defProps$b(a, __getOwnPropDescs$b(b));
141
141
  class ColumnType extends orchidCore.ColumnTypeBase {
142
142
  /**
143
143
  * Mark the column as a primary key.
@@ -336,13 +336,13 @@ class ColumnType extends orchidCore.ColumnTypeBase {
336
336
  */
337
337
  searchIndex(...args) {
338
338
  return orchidCore.pushColumnData(this, "indexes", {
339
- options: __spreadValues$h(__spreadValues$h({}, typeof args[0] === "string" ? args[1] : args[0]), this.dataType === "tsvector" ? { using: "GIN" } : { tsVector: true }),
339
+ options: __spreadValues$l(__spreadValues$l({}, typeof args[0] === "string" ? args[1] : args[0]), this.dataType === "tsvector" ? { using: "GIN" } : { tsVector: true }),
340
340
  name: typeof args[0] === "string" ? args[0] : void 0
341
341
  });
342
342
  }
343
343
  unique(...args) {
344
344
  return orchidCore.pushColumnData(this, "indexes", {
345
- options: __spreadProps$9(__spreadValues$h({}, typeof args[0] === "string" ? args[1] : args[0]), {
345
+ options: __spreadProps$b(__spreadValues$l({}, typeof args[0] === "string" ? args[1] : args[0]), {
346
346
  unique: true
347
347
  }),
348
348
  name: typeof args[0] === "string" ? args[0] : void 0
@@ -384,25 +384,25 @@ class ColumnType extends orchidCore.ColumnTypeBase {
384
384
  }
385
385
  }
386
386
 
387
- var __defProp$g = Object.defineProperty;
388
- var __defProps$8 = Object.defineProperties;
389
- var __getOwnPropDescs$8 = Object.getOwnPropertyDescriptors;
390
- var __getOwnPropSymbols$h = Object.getOwnPropertySymbols;
391
- var __hasOwnProp$h = Object.prototype.hasOwnProperty;
392
- var __propIsEnum$h = Object.prototype.propertyIsEnumerable;
393
- var __defNormalProp$g = (obj, key, value) => key in obj ? __defProp$g(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
394
- var __spreadValues$g = (a, b) => {
387
+ var __defProp$k = Object.defineProperty;
388
+ var __defProps$a = Object.defineProperties;
389
+ var __getOwnPropDescs$a = Object.getOwnPropertyDescriptors;
390
+ var __getOwnPropSymbols$l = Object.getOwnPropertySymbols;
391
+ var __hasOwnProp$l = Object.prototype.hasOwnProperty;
392
+ var __propIsEnum$l = Object.prototype.propertyIsEnumerable;
393
+ var __defNormalProp$k = (obj, key, value) => key in obj ? __defProp$k(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
394
+ var __spreadValues$k = (a, b) => {
395
395
  for (var prop in b || (b = {}))
396
- if (__hasOwnProp$h.call(b, prop))
397
- __defNormalProp$g(a, prop, b[prop]);
398
- if (__getOwnPropSymbols$h)
399
- for (var prop of __getOwnPropSymbols$h(b)) {
400
- if (__propIsEnum$h.call(b, prop))
401
- __defNormalProp$g(a, prop, b[prop]);
396
+ if (__hasOwnProp$l.call(b, prop))
397
+ __defNormalProp$k(a, prop, b[prop]);
398
+ if (__getOwnPropSymbols$l)
399
+ for (var prop of __getOwnPropSymbols$l(b)) {
400
+ if (__propIsEnum$l.call(b, prop))
401
+ __defNormalProp$k(a, prop, b[prop]);
402
402
  }
403
403
  return a;
404
404
  };
405
- var __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
405
+ var __spreadProps$a = (a, b) => __defProps$a(a, __getOwnPropDescs$a(b));
406
406
  const knownDefaults = {
407
407
  current_timestamp: "now()",
408
408
  "transaction_timestamp()": "now()"
@@ -418,7 +418,7 @@ const simplifyColumnDefault = (value) => {
418
418
  const instantiateColumn = (typeFn, params) => {
419
419
  const column = typeFn();
420
420
  const { dateTimePrecision } = params;
421
- Object.assign(column.data, __spreadProps$8(__spreadValues$g({}, params), {
421
+ Object.assign(column.data, __spreadProps$a(__spreadValues$k({}, params), {
422
422
  dateTimePrecision: (
423
423
  // 0 is default for date, 6 is default for timestamp
424
424
  dateTimePrecision && dateTimePrecision !== 6 ? dateTimePrecision : void 0
@@ -842,25 +842,25 @@ const getSqlText = (sql) => {
842
842
  throw new Error(`Batch SQL is not supported in this query`);
843
843
  };
844
844
 
845
- var __defProp$f = Object.defineProperty;
846
- var __defProps$7 = Object.defineProperties;
847
- var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
848
- var __getOwnPropSymbols$g = Object.getOwnPropertySymbols;
849
- var __hasOwnProp$g = Object.prototype.hasOwnProperty;
850
- var __propIsEnum$g = Object.prototype.propertyIsEnumerable;
851
- var __defNormalProp$f = (obj, key, value) => key in obj ? __defProp$f(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
852
- var __spreadValues$f = (a, b) => {
845
+ var __defProp$j = Object.defineProperty;
846
+ var __defProps$9 = Object.defineProperties;
847
+ var __getOwnPropDescs$9 = Object.getOwnPropertyDescriptors;
848
+ var __getOwnPropSymbols$k = Object.getOwnPropertySymbols;
849
+ var __hasOwnProp$k = Object.prototype.hasOwnProperty;
850
+ var __propIsEnum$k = Object.prototype.propertyIsEnumerable;
851
+ var __defNormalProp$j = (obj, key, value) => key in obj ? __defProp$j(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
852
+ var __spreadValues$j = (a, b) => {
853
853
  for (var prop in b || (b = {}))
854
- if (__hasOwnProp$g.call(b, prop))
855
- __defNormalProp$f(a, prop, b[prop]);
856
- if (__getOwnPropSymbols$g)
857
- for (var prop of __getOwnPropSymbols$g(b)) {
858
- if (__propIsEnum$g.call(b, prop))
859
- __defNormalProp$f(a, prop, b[prop]);
854
+ if (__hasOwnProp$k.call(b, prop))
855
+ __defNormalProp$j(a, prop, b[prop]);
856
+ if (__getOwnPropSymbols$k)
857
+ for (var prop of __getOwnPropSymbols$k(b)) {
858
+ if (__propIsEnum$k.call(b, prop))
859
+ __defNormalProp$j(a, prop, b[prop]);
860
860
  }
861
861
  return a;
862
862
  };
863
- var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
863
+ var __spreadProps$9 = (a, b) => __defProps$9(a, __getOwnPropDescs$9(b));
864
864
  function setQueryOperators(query, operators) {
865
865
  const q = query.q;
866
866
  if (q.operators !== operators) {
@@ -933,7 +933,7 @@ const base = {
933
933
  (key, value, ctx, quotedAs) => `NOT ${key} IN ${quoteValue$1(value, ctx, quotedAs)}`
934
934
  )
935
935
  };
936
- const boolean = __spreadProps$7(__spreadValues$f({}, base), {
936
+ const boolean = __spreadProps$9(__spreadValues$j({}, base), {
937
937
  and: make(
938
938
  (key, value, ctx, quotedAs) => `${key} AND ${value.q.expr.toSQL(ctx, quotedAs)}`
939
939
  ),
@@ -941,7 +941,7 @@ const boolean = __spreadProps$7(__spreadValues$f({}, base), {
941
941
  (key, value, ctx, quotedAs) => `(${key}) OR (${value.q.expr.toSQL(ctx, quotedAs)})`
942
942
  )
943
943
  });
944
- const numeric = __spreadProps$7(__spreadValues$f({}, base), {
944
+ const numeric = __spreadProps$9(__spreadValues$j({}, base), {
945
945
  lt: make(
946
946
  (key, value, ctx, quotedAs) => `${key} < ${quoteValue$1(value, ctx, quotedAs)}`
947
947
  ),
@@ -962,7 +962,7 @@ const numeric = __spreadProps$7(__spreadValues$f({}, base), {
962
962
  )}`
963
963
  )
964
964
  });
965
- const text = __spreadProps$7(__spreadValues$f({}, base), {
965
+ const text = __spreadProps$9(__spreadValues$j({}, base), {
966
966
  contains: make(
967
967
  (key, value, ctx, quotedAs) => `${key} ILIKE '%' || ${quoteValue$1(value, ctx, quotedAs)} || '%'`
968
968
  ),
@@ -984,7 +984,7 @@ const text = __spreadProps$7(__spreadValues$f({}, base), {
984
984
  });
985
985
  const encodeJsonPath = (ctx, path) => orchidCore.addValue(ctx.values, `{${Array.isArray(path) ? path.join(", ") : path}}`);
986
986
  const jsonPathQueryOp = (key, [path, options], ctx) => `jsonb_path_query_first(${key}, ${orchidCore.addValue(ctx.values, path)}${(options == null ? void 0 : options.vars) ? `, ${orchidCore.addValue(ctx.values, JSON.stringify(options.vars))}${options.silent ? ", true" : ""}` : (options == null ? void 0 : options.silent) ? ", NULL, true" : ""})`;
987
- const json = __spreadProps$7(__spreadValues$f({}, base), {
987
+ const json = __spreadProps$9(__spreadValues$j({}, base), {
988
988
  jsonPathQueryFirst: Object.assign(
989
989
  function(path, options) {
990
990
  var _a, _b, _c, _d, _e;
@@ -1211,25 +1211,25 @@ class BigSerialColumn extends NumberAsStringBaseColumn {
1211
1211
  }
1212
1212
  }
1213
1213
 
1214
- var __defProp$e = Object.defineProperty;
1215
- var __defProps$6 = Object.defineProperties;
1216
- var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
1217
- var __getOwnPropSymbols$f = Object.getOwnPropertySymbols;
1218
- var __hasOwnProp$f = Object.prototype.hasOwnProperty;
1219
- var __propIsEnum$f = Object.prototype.propertyIsEnumerable;
1220
- var __defNormalProp$e = (obj, key, value) => key in obj ? __defProp$e(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1221
- var __spreadValues$e = (a, b) => {
1214
+ var __defProp$i = Object.defineProperty;
1215
+ var __defProps$8 = Object.defineProperties;
1216
+ var __getOwnPropDescs$8 = Object.getOwnPropertyDescriptors;
1217
+ var __getOwnPropSymbols$j = Object.getOwnPropertySymbols;
1218
+ var __hasOwnProp$j = Object.prototype.hasOwnProperty;
1219
+ var __propIsEnum$j = Object.prototype.propertyIsEnumerable;
1220
+ var __defNormalProp$i = (obj, key, value) => key in obj ? __defProp$i(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1221
+ var __spreadValues$i = (a, b) => {
1222
1222
  for (var prop in b || (b = {}))
1223
- if (__hasOwnProp$f.call(b, prop))
1224
- __defNormalProp$e(a, prop, b[prop]);
1225
- if (__getOwnPropSymbols$f)
1226
- for (var prop of __getOwnPropSymbols$f(b)) {
1227
- if (__propIsEnum$f.call(b, prop))
1228
- __defNormalProp$e(a, prop, b[prop]);
1223
+ if (__hasOwnProp$j.call(b, prop))
1224
+ __defNormalProp$i(a, prop, b[prop]);
1225
+ if (__getOwnPropSymbols$j)
1226
+ for (var prop of __getOwnPropSymbols$j(b)) {
1227
+ if (__propIsEnum$j.call(b, prop))
1228
+ __defNormalProp$i(a, prop, b[prop]);
1229
1229
  }
1230
1230
  return a;
1231
1231
  };
1232
- var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
1232
+ var __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
1233
1233
  class TextBaseColumn extends ColumnType {
1234
1234
  constructor(schema, schemaType = schema.stringSchema()) {
1235
1235
  super(schema, schemaType);
@@ -1277,7 +1277,7 @@ class StringColumn extends VarCharColumn {
1277
1277
  }
1278
1278
  }
1279
1279
  const textColumnToCode = (column, t, m) => {
1280
- const data = __spreadValues$e({}, column.data);
1280
+ const data = __spreadValues$i({}, column.data);
1281
1281
  let args = "";
1282
1282
  const hasMax = data.maxArg !== void 0 && data.max === data.maxArg;
1283
1283
  if (data.minArg !== void 0 && data.min === data.minArg || hasMax) {
@@ -1574,7 +1574,7 @@ class UUIDColumn extends ColumnType {
1574
1574
  `uuid()`,
1575
1575
  m,
1576
1576
  // don't output the default default
1577
- data.default instanceof orchidCore.RawSQLBase && data.default._sql === uuidDefaultSQL ? __spreadProps$6(__spreadValues$e({}, data), { default: void 0 }) : data
1577
+ data.default instanceof orchidCore.RawSQLBase && data.default._sql === uuidDefaultSQL ? __spreadProps$8(__spreadValues$i({}, data), { default: void 0 }) : data
1578
1578
  );
1579
1579
  }
1580
1580
  }
@@ -1870,31 +1870,41 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
1870
1870
  return `"${tableName}"."${key}"`;
1871
1871
  };
1872
1872
  const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1873
- var _a, _b, _c, _d;
1874
1873
  const index = column.indexOf(".");
1875
- if (index !== -1) {
1876
- const table = column.slice(0, index);
1877
- const key = column.slice(index + 1);
1878
- if (key === "*") {
1879
- if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1880
- return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
1881
- }
1882
- return column;
1874
+ return index !== -1 ? tableColumnToSqlWithAs(
1875
+ ctx,
1876
+ data,
1877
+ column,
1878
+ column.slice(0, index),
1879
+ column.slice(index + 1),
1880
+ quotedAs,
1881
+ select
1882
+ ) : ownColumnToSqlWithAs(ctx, data, column, quotedAs, select);
1883
+ };
1884
+ const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select) => {
1885
+ var _a, _b, _c;
1886
+ if (key === "*") {
1887
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1888
+ return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
1883
1889
  }
1884
- const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1885
- const quoted = `"${table}"`;
1886
- const col2 = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
1887
- if (col2) {
1888
- if (col2.data.name && col2.data.name !== key) {
1889
- return `"${tableName}"."${col2.data.name}" "${key}"`;
1890
- }
1891
- if (col2.data.computed) {
1892
- return `${col2.data.computed.toSQL(ctx, quoted)} "${key}"`;
1893
- }
1890
+ return column;
1891
+ }
1892
+ const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1893
+ const quoted = `"${table}"`;
1894
+ const col = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
1895
+ if (col) {
1896
+ if (col.data.name && col.data.name !== key) {
1897
+ return `"${tableName}"."${col.data.name}" "${key}"`;
1898
+ }
1899
+ if (col.data.computed) {
1900
+ return `${col.data.computed.toSQL(ctx, quoted)} "${key}"`;
1894
1901
  }
1895
- return `"${tableName}"."${key}"`;
1896
1902
  }
1897
- if (!select && ((_d = data.joinedShapes) == null ? void 0 : _d[column])) {
1903
+ return `"${tableName}"."${key}"`;
1904
+ };
1905
+ const ownColumnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1906
+ var _a;
1907
+ if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1898
1908
  return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
1899
1909
  }
1900
1910
  const col = data.shape[column];
@@ -1908,11 +1918,6 @@ const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1908
1918
  }
1909
1919
  return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
1910
1920
  };
1911
- const ownColumnToSql = (data, column, quotedAs) => {
1912
- var _a;
1913
- const name = (_a = data.shape[column]) == null ? void 0 : _a.data.name;
1914
- return `${quotedAs ? `${quotedAs}.` : ""}"${name || column}"${name && name !== column ? ` "${column}"` : ""}`;
1915
- };
1916
1921
  const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
1917
1922
  return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
1918
1923
  };
@@ -2314,25 +2319,25 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
2314
2319
  ands.push(`${multiple ? `(${columnsSql})` : columnsSql} IN ${value}`);
2315
2320
  };
2316
2321
 
2317
- var __defProp$d = Object.defineProperty;
2318
- var __defProps$5 = Object.defineProperties;
2319
- var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
2320
- var __getOwnPropSymbols$e = Object.getOwnPropertySymbols;
2321
- var __hasOwnProp$e = Object.prototype.hasOwnProperty;
2322
- var __propIsEnum$e = Object.prototype.propertyIsEnumerable;
2323
- var __defNormalProp$d = (obj, key, value) => key in obj ? __defProp$d(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2324
- var __spreadValues$d = (a, b) => {
2322
+ var __defProp$h = Object.defineProperty;
2323
+ var __defProps$7 = Object.defineProperties;
2324
+ var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
2325
+ var __getOwnPropSymbols$i = Object.getOwnPropertySymbols;
2326
+ var __hasOwnProp$i = Object.prototype.hasOwnProperty;
2327
+ var __propIsEnum$i = Object.prototype.propertyIsEnumerable;
2328
+ var __defNormalProp$h = (obj, key, value) => key in obj ? __defProp$h(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2329
+ var __spreadValues$h = (a, b) => {
2325
2330
  for (var prop in b || (b = {}))
2326
- if (__hasOwnProp$e.call(b, prop))
2327
- __defNormalProp$d(a, prop, b[prop]);
2328
- if (__getOwnPropSymbols$e)
2329
- for (var prop of __getOwnPropSymbols$e(b)) {
2330
- if (__propIsEnum$e.call(b, prop))
2331
- __defNormalProp$d(a, prop, b[prop]);
2331
+ if (__hasOwnProp$i.call(b, prop))
2332
+ __defNormalProp$h(a, prop, b[prop]);
2333
+ if (__getOwnPropSymbols$i)
2334
+ for (var prop of __getOwnPropSymbols$i(b)) {
2335
+ if (__propIsEnum$i.call(b, prop))
2336
+ __defNormalProp$h(a, prop, b[prop]);
2332
2337
  }
2333
2338
  return a;
2334
2339
  };
2335
- var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
2340
+ var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
2336
2341
  const processJoinItem = (ctx, table, query, args, quotedAs) => {
2337
2342
  let target;
2338
2343
  let on;
@@ -2394,8 +2399,8 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
2394
2399
  const whereSql = whereToSql(
2395
2400
  ctx,
2396
2401
  q,
2397
- __spreadProps$5(__spreadValues$d({}, q.q), {
2398
- joinedShapes: __spreadProps$5(__spreadValues$d(__spreadValues$d({}, query.joinedShapes), q.q.joinedShapes), {
2402
+ __spreadProps$7(__spreadValues$h({}, q.q), {
2403
+ joinedShapes: __spreadProps$7(__spreadValues$h(__spreadValues$h({}, query.joinedShapes), q.q.joinedShapes), {
2399
2404
  [table.q.as || table.table]: table.q.shape
2400
2405
  })
2401
2406
  }),
@@ -2542,25 +2547,25 @@ const getIsJoinSubQuery = (query) => {
2542
2547
  return false;
2543
2548
  };
2544
2549
 
2545
- var __defProp$c = Object.defineProperty;
2546
- var __defProps$4 = Object.defineProperties;
2547
- var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
2548
- var __getOwnPropSymbols$d = Object.getOwnPropertySymbols;
2549
- var __hasOwnProp$d = Object.prototype.hasOwnProperty;
2550
- var __propIsEnum$d = Object.prototype.propertyIsEnumerable;
2551
- var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2552
- var __spreadValues$c = (a, b) => {
2550
+ var __defProp$g = Object.defineProperty;
2551
+ var __defProps$6 = Object.defineProperties;
2552
+ var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
2553
+ var __getOwnPropSymbols$h = Object.getOwnPropertySymbols;
2554
+ var __hasOwnProp$h = Object.prototype.hasOwnProperty;
2555
+ var __propIsEnum$h = Object.prototype.propertyIsEnumerable;
2556
+ var __defNormalProp$g = (obj, key, value) => key in obj ? __defProp$g(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2557
+ var __spreadValues$g = (a, b) => {
2553
2558
  for (var prop in b || (b = {}))
2554
- if (__hasOwnProp$d.call(b, prop))
2555
- __defNormalProp$c(a, prop, b[prop]);
2556
- if (__getOwnPropSymbols$d)
2557
- for (var prop of __getOwnPropSymbols$d(b)) {
2558
- if (__propIsEnum$d.call(b, prop))
2559
- __defNormalProp$c(a, prop, b[prop]);
2559
+ if (__hasOwnProp$h.call(b, prop))
2560
+ __defNormalProp$g(a, prop, b[prop]);
2561
+ if (__getOwnPropSymbols$h)
2562
+ for (var prop of __getOwnPropSymbols$h(b)) {
2563
+ if (__propIsEnum$h.call(b, prop))
2564
+ __defNormalProp$g(a, prop, b[prop]);
2560
2565
  }
2561
2566
  return a;
2562
2567
  };
2563
- var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
2568
+ var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
2564
2569
  const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2565
2570
  var _a;
2566
2571
  if (typeof first === "string") {
@@ -2578,27 +2583,28 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2578
2583
  return { w: first, a: args };
2579
2584
  } else {
2580
2585
  const joinToQ = joinTo.q;
2581
- const shape = (_a = joinToQ.withShapes) == null ? void 0 : _a[first];
2582
- if (!shape) {
2583
- throw new Error("Cannot get shape of `with` statement");
2586
+ const w = (_a = joinToQ.withShapes) == null ? void 0 : _a[first];
2587
+ if (!w) {
2588
+ throw new Error("Cannot find a `with` statement");
2584
2589
  }
2585
2590
  const j = joinTo.queryBuilder.baseQuery.clone();
2586
2591
  j.table = first;
2587
2592
  j.q = {
2588
- shape,
2593
+ shape: w.shape,
2594
+ computeds: w.computeds,
2589
2595
  adapter: joinToQ.adapter,
2590
2596
  handleResult: joinToQ.handleResult,
2591
2597
  returnType: "all",
2592
2598
  logger: joinToQ.logger
2593
2599
  };
2594
2600
  j.baseQuery = j;
2595
- const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinToQ.joinedShapes), {
2601
+ const joinedShapes = __spreadProps$6(__spreadValues$g({}, joinToQ.joinedShapes), {
2596
2602
  [joinToQ.as || joinTo.table]: joinTo.shape
2597
2603
  });
2598
2604
  const r = args[0](
2599
2605
  makeJoinQueryBuilder(
2600
2606
  j,
2601
- j.q.joinedShapes ? __spreadValues$c(__spreadValues$c({}, j.q.joinedShapes), joinedShapes) : joinedShapes,
2607
+ j.q.joinedShapes ? __spreadValues$g(__spreadValues$g({}, j.q.joinedShapes), joinedShapes) : joinedShapes,
2602
2608
  joinTo
2603
2609
  )
2604
2610
  );
@@ -2621,16 +2627,16 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2621
2627
  pushQueryArray(q, "or", query.or);
2622
2628
  }
2623
2629
  if (query.scopes) {
2624
- q.q.scopes = __spreadValues$c(__spreadValues$c({}, q.q.scopes), query.scopes);
2630
+ q.q.scopes = __spreadValues$g(__spreadValues$g({}, q.q.scopes), query.scopes);
2625
2631
  }
2626
2632
  }
2627
- const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2633
+ const joinedShapes = __spreadProps$6(__spreadValues$g({}, joinTo.q.joinedShapes), {
2628
2634
  [joinTo.q.as || joinTo.table]: joinTo.shape
2629
2635
  });
2630
2636
  const r = args0(
2631
2637
  makeJoinQueryBuilder(
2632
2638
  q,
2633
- q.q.joinedShapes ? __spreadValues$c(__spreadValues$c({}, q.q.joinedShapes), joinedShapes) : joinedShapes,
2639
+ q.q.joinedShapes ? __spreadValues$g(__spreadValues$g({}, q.q.joinedShapes), joinedShapes) : joinedShapes,
2634
2640
  joinTo
2635
2641
  )
2636
2642
  );
@@ -2678,30 +2684,32 @@ const _queryNone = (q) => {
2678
2684
  };
2679
2685
  const isQueryNone = (q) => q.then === noneMethods.then;
2680
2686
 
2681
- var __defProp$b = Object.defineProperty;
2682
- var __defProps$3 = Object.defineProperties;
2683
- var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
2684
- var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
2685
- var __hasOwnProp$c = Object.prototype.hasOwnProperty;
2686
- var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
2687
- var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2688
- var __spreadValues$b = (a, b) => {
2687
+ var __defProp$f = Object.defineProperty;
2688
+ var __defProps$5 = Object.defineProperties;
2689
+ var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
2690
+ var __getOwnPropSymbols$g = Object.getOwnPropertySymbols;
2691
+ var __hasOwnProp$g = Object.prototype.hasOwnProperty;
2692
+ var __propIsEnum$g = Object.prototype.propertyIsEnumerable;
2693
+ var __defNormalProp$f = (obj, key, value) => key in obj ? __defProp$f(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2694
+ var __spreadValues$f = (a, b) => {
2689
2695
  for (var prop in b || (b = {}))
2690
- if (__hasOwnProp$c.call(b, prop))
2691
- __defNormalProp$b(a, prop, b[prop]);
2692
- if (__getOwnPropSymbols$c)
2693
- for (var prop of __getOwnPropSymbols$c(b)) {
2694
- if (__propIsEnum$c.call(b, prop))
2695
- __defNormalProp$b(a, prop, b[prop]);
2696
+ if (__hasOwnProp$g.call(b, prop))
2697
+ __defNormalProp$f(a, prop, b[prop]);
2698
+ if (__getOwnPropSymbols$g)
2699
+ for (var prop of __getOwnPropSymbols$g(b)) {
2700
+ if (__propIsEnum$g.call(b, prop))
2701
+ __defNormalProp$f(a, prop, b[prop]);
2696
2702
  }
2697
2703
  return a;
2698
2704
  };
2699
- var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
2705
+ var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
2700
2706
  const _join = (query, require2, type, first, args) => {
2701
- var _a, _b;
2707
+ var _a, _b, _c, _d;
2702
2708
  let joinKey;
2703
2709
  let shape;
2704
2710
  let parsers;
2711
+ let batchParsers;
2712
+ let computeds;
2705
2713
  let joinSubQuery = false;
2706
2714
  if (typeof first === "function") {
2707
2715
  first = first(query.relations);
@@ -2717,6 +2725,8 @@ const _join = (query, require2, type, first, args) => {
2717
2725
  if (joinKey) {
2718
2726
  shape = getShapeFromSelect(q, joinSubQuery);
2719
2727
  parsers = q.q.parsers;
2728
+ batchParsers = q.q.batchParsers;
2729
+ computeds = q.q.computeds;
2720
2730
  if (joinSubQuery) {
2721
2731
  first = q.clone();
2722
2732
  first.shape = shape;
@@ -2727,12 +2737,17 @@ const _join = (query, require2, type, first, args) => {
2727
2737
  const relation = query.relations[joinKey];
2728
2738
  if (relation) {
2729
2739
  shape = getShapeFromSelect(relation.relationConfig.query);
2730
- parsers = relation.relationConfig.query.q.parsers;
2740
+ const r = relation.relationConfig.query;
2741
+ parsers = r.q.parsers;
2742
+ batchParsers = r.q.batchParsers;
2743
+ computeds = r.q.computeds;
2731
2744
  } else {
2732
- shape = (_a = query.q.withShapes) == null ? void 0 : _a[joinKey];
2745
+ const w = (_a = query.q.withShapes) == null ? void 0 : _a[joinKey];
2746
+ shape = w == null ? void 0 : w.shape;
2747
+ computeds = w == null ? void 0 : w.computeds;
2733
2748
  if (shape) {
2734
2749
  if (!require2)
2735
- shape = __spreadValues$b({}, shape);
2750
+ shape = __spreadValues$f({}, shape);
2736
2751
  parsers = {};
2737
2752
  for (const key in shape) {
2738
2753
  const parser = shape[key].parseFn;
@@ -2743,20 +2758,6 @@ const _join = (query, require2, type, first, args) => {
2743
2758
  }
2744
2759
  }
2745
2760
  }
2746
- if (joinKey) {
2747
- setQueryObjectValue(
2748
- query,
2749
- "joinedShapes",
2750
- joinKey,
2751
- shape
2752
- );
2753
- setQueryObjectValue(
2754
- query,
2755
- "joinedParsers",
2756
- joinKey,
2757
- parsers
2758
- );
2759
- }
2760
2761
  const joinArgs = processJoinArgs(
2761
2762
  query,
2762
2763
  first,
@@ -2779,17 +2780,60 @@ const _join = (query, require2, type, first, args) => {
2779
2780
  joinKey,
2780
2781
  j.q.parsers
2781
2782
  );
2783
+ if (j.q.batchParsers) {
2784
+ ((_d = (_c = query.q).joinedBatchParsers) != null ? _d : _c.joinedBatchParsers = {})[joinKey] = j.q.batchParsers;
2785
+ }
2786
+ setQueryObjectValue(
2787
+ query,
2788
+ "joinedComputeds",
2789
+ joinKey,
2790
+ j.q.computeds
2791
+ );
2792
+ } else {
2793
+ addAllShapesAndParsers(
2794
+ query,
2795
+ joinKey,
2796
+ shape,
2797
+ parsers,
2798
+ batchParsers,
2799
+ computeds
2800
+ );
2782
2801
  }
2783
2802
  } else if (require2 && "r" in joinArgs && isQueryNone(joinArgs.r)) {
2784
2803
  return _queryNone(query);
2804
+ } else {
2805
+ addAllShapesAndParsers(
2806
+ query,
2807
+ joinKey,
2808
+ shape,
2809
+ parsers,
2810
+ batchParsers,
2811
+ computeds
2812
+ );
2785
2813
  }
2786
2814
  return pushQueryValue(query, "join", {
2787
2815
  type,
2788
2816
  args: joinArgs
2789
2817
  });
2790
2818
  };
2819
+ const addAllShapesAndParsers = (query, joinKey, shape, parsers, batchParsers, computeds) => {
2820
+ var _a, _b;
2821
+ if (!joinKey)
2822
+ return;
2823
+ setQueryObjectValue(query, "joinedShapes", joinKey, shape);
2824
+ setQueryObjectValue(query, "joinedParsers", joinKey, parsers);
2825
+ if (batchParsers) {
2826
+ ((_b = (_a = query.q).joinedBatchParsers) != null ? _b : _a.joinedBatchParsers = {})[joinKey] = batchParsers;
2827
+ }
2828
+ setQueryObjectValue(
2829
+ query,
2830
+ "joinedComputeds",
2831
+ joinKey,
2832
+ computeds
2833
+ );
2834
+ };
2791
2835
  const _joinLateral = (self, type, arg, cb, as) => {
2792
- var _a, _b, _c;
2836
+ var _a, _b, _c, _d, _e, _f, _g;
2793
2837
  const q = self;
2794
2838
  let relation;
2795
2839
  if (typeof arg === "string") {
@@ -2797,13 +2841,14 @@ const _joinLateral = (self, type, arg, cb, as) => {
2797
2841
  if (relation) {
2798
2842
  arg = relation.relationConfig.query.clone();
2799
2843
  } else {
2800
- const shape = (_a = q.q.withShapes) == null ? void 0 : _a[arg];
2801
- if (shape) {
2844
+ const w = (_a = q.q.withShapes) == null ? void 0 : _a[arg];
2845
+ if (w) {
2802
2846
  const t = Object.create(q.queryBuilder);
2803
2847
  t.table = arg;
2804
- t.shape = shape;
2805
- t.q = __spreadProps$3(__spreadValues$b({}, t.q), {
2806
- shape
2848
+ t.shape = w.shape;
2849
+ t.computeds = w.computeds;
2850
+ t.q = __spreadProps$5(__spreadValues$f({}, t.q), {
2851
+ shape: w.shape
2807
2852
  });
2808
2853
  t.baseQuery = t;
2809
2854
  arg = t;
@@ -2812,7 +2857,8 @@ const _joinLateral = (self, type, arg, cb, as) => {
2812
2857
  }
2813
2858
  const query = arg;
2814
2859
  query.q.joinTo = q;
2815
- ((_c = (_b = query.q).joinedShapes) != null ? _c : _b.joinedShapes = {})[getQueryAs(q)] = q.q.shape;
2860
+ const joinedAs = getQueryAs(q);
2861
+ ((_c = (_b = query.q).joinedShapes) != null ? _c : _b.joinedShapes = {})[joinedAs] = q.q.shape;
2816
2862
  let result = resolveSubQueryCallback(query, cb);
2817
2863
  if (relation) {
2818
2864
  result = relation.relationConfig.joinQuery(
@@ -2825,12 +2871,13 @@ const _joinLateral = (self, type, arg, cb, as) => {
2825
2871
  const shape = getShapeFromSelect(result, true);
2826
2872
  setQueryObjectValue(q, "joinedShapes", joinKey, shape);
2827
2873
  setQueryObjectValue(q, "joinedParsers", joinKey, result.q.parsers);
2874
+ if (result.q.batchParsers) {
2875
+ ((_e = (_d = q.q).joinedBatchParsers) != null ? _e : _d.joinedBatchParsers = {})[joinKey] = result.q.batchParsers;
2876
+ }
2828
2877
  }
2829
- return pushQueryValue(q, "join", [
2830
- type,
2831
- result,
2832
- as || getQueryAs(result)
2833
- ]);
2878
+ as || (as = getQueryAs(result));
2879
+ ((_g = (_f = q.q).joinedComputeds) != null ? _g : _f.joinedComputeds = {})[as] = result.q.computeds;
2880
+ return pushQueryValue(q, "join", [type, result, as]);
2834
2881
  };
2835
2882
 
2836
2883
  class EnumColumn extends ColumnType {
@@ -3119,9 +3166,221 @@ class Transaction {
3119
3166
  }
3120
3167
  }
3121
3168
  }
3122
- }
3169
+ }
3170
+
3171
+ const applyBatchTransforms = (query, batches) => {
3172
+ if (query.transform) {
3173
+ for (const item of batches) {
3174
+ item.parent[item.key] = orchidCore.applyTransforms(
3175
+ query.returnType,
3176
+ query.transform,
3177
+ item.data
3178
+ );
3179
+ }
3180
+ }
3181
+ };
3182
+ const finalizeNestedHookSelect = (batches, returnType, tempColumns, renames, key) => {
3183
+ var _a;
3184
+ if (renames) {
3185
+ for (const { data } of batches) {
3186
+ for (const record of data) {
3187
+ if (record) {
3188
+ for (const a in renames) {
3189
+ record[a] = record[renames[a]];
3190
+ }
3191
+ }
3192
+ }
3193
+ }
3194
+ }
3195
+ if (tempColumns == null ? void 0 : tempColumns.size) {
3196
+ for (const { data } of batches) {
3197
+ for (const record of data) {
3198
+ if (record) {
3199
+ for (const key2 of tempColumns) {
3200
+ delete record[key2];
3201
+ }
3202
+ }
3203
+ }
3204
+ }
3205
+ }
3206
+ if (returnType === "one" || returnType === "oneOrThrow") {
3207
+ for (const batch of batches) {
3208
+ batch.data = batch.data[0];
3209
+ }
3210
+ } else if (returnType === "pluck") {
3211
+ for (const { data } of batches) {
3212
+ for (let i = 0; i < data.length; i++) {
3213
+ data[i] = data[i][key];
3214
+ }
3215
+ }
3216
+ } else if (returnType === "value" || returnType === "valueOrThrow") {
3217
+ for (const item of batches) {
3218
+ item.parent[item.key] = (_a = item.data[0]) == null ? void 0 : _a[key];
3219
+ }
3220
+ }
3221
+ };
3222
+
3223
+ var __defProp$e = Object.defineProperty;
3224
+ var __defProps$4 = Object.defineProperties;
3225
+ var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
3226
+ var __getOwnPropSymbols$f = Object.getOwnPropertySymbols;
3227
+ var __hasOwnProp$f = Object.prototype.hasOwnProperty;
3228
+ var __propIsEnum$f = Object.prototype.propertyIsEnumerable;
3229
+ var __defNormalProp$e = (obj, key, value) => key in obj ? __defProp$e(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3230
+ var __spreadValues$e = (a, b) => {
3231
+ for (var prop in b || (b = {}))
3232
+ if (__hasOwnProp$f.call(b, prop))
3233
+ __defNormalProp$e(a, prop, b[prop]);
3234
+ if (__getOwnPropSymbols$f)
3235
+ for (var prop of __getOwnPropSymbols$f(b)) {
3236
+ if (__propIsEnum$f.call(b, prop))
3237
+ __defNormalProp$e(a, prop, b[prop]);
3238
+ }
3239
+ return a;
3240
+ };
3241
+ var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
3242
+ class ComputedColumn {
3243
+ constructor(kind, deps, fn) {
3244
+ this.kind = kind;
3245
+ this.deps = deps;
3246
+ this.fn = fn;
3247
+ }
3248
+ }
3249
+ const computeAtRuntime = (deps, fn) => new ComputedColumn("one", deps, fn);
3250
+ const computeBatchAtRuntime = (deps, fn) => new ComputedColumn("many", deps, fn);
3251
+ const applyComputedColumns = (q, fn) => {
3252
+ q.computeAtRuntime = computeAtRuntime;
3253
+ q.computeBatchAtRuntime = computeBatchAtRuntime;
3254
+ const computed = fn(q);
3255
+ for (const key in computed) {
3256
+ const item = computed[key];
3257
+ if (item instanceof ComputedColumn) {
3258
+ q.q.computeds = __spreadProps$4(__spreadValues$e({}, q.q.computeds), { [key]: item });
3259
+ } else {
3260
+ (q.shape[key] = item.result.value || UnknownColumn.instance).data.computed = item;
3261
+ }
3262
+ }
3263
+ q.computeAtRuntime = q.computeBatchAtRuntime = void 0;
3264
+ };
3265
+ const processComputedResult = (query, result) => {
3266
+ let promises;
3267
+ for (const key in query.selectedComputeds) {
3268
+ const computed = query.selectedComputeds[key];
3269
+ if (computed.kind === "one") {
3270
+ for (const record of result) {
3271
+ record[key] = computed.fn(record);
3272
+ }
3273
+ } else {
3274
+ const res = computed.fn(result);
3275
+ if (Array.isArray(res)) {
3276
+ saveBatchComputed(key, result, res);
3277
+ } else {
3278
+ (promises != null ? promises : promises = []).push(
3279
+ res.then(
3280
+ (res2) => saveBatchComputed(key, result, res2)
3281
+ )
3282
+ );
3283
+ }
3284
+ }
3285
+ }
3286
+ if (!promises)
3287
+ return;
3288
+ return Promise.all(promises);
3289
+ };
3290
+ const processComputedBatches = (query, batches, originalReturnType, returnType, tempColumns, renames, key) => {
3291
+ let promises;
3292
+ for (const key2 in query.selectedComputeds) {
3293
+ const computed = query.selectedComputeds[key2];
3294
+ if (computed.kind === "one") {
3295
+ for (const { data } of batches) {
3296
+ for (const record of data) {
3297
+ if (record) {
3298
+ record[key2] = computed.fn(record);
3299
+ }
3300
+ }
3301
+ }
3302
+ } else {
3303
+ for (const { data } of batches) {
3304
+ let present;
3305
+ let blanks;
3306
+ if (!returnType || returnType === "all") {
3307
+ present = data;
3308
+ } else {
3309
+ present = [];
3310
+ blanks = /* @__PURE__ */ new Set();
3311
+ for (let i = 0; i < data.length; i++) {
3312
+ if (data[i]) {
3313
+ present.push(data[i]);
3314
+ } else {
3315
+ blanks.add(i);
3316
+ }
3317
+ }
3318
+ }
3319
+ const res = computed.fn(present);
3320
+ if (Array.isArray(res)) {
3321
+ saveBatchComputed(key2, data, res, blanks);
3322
+ } else {
3323
+ (promises != null ? promises : promises = []).push(
3324
+ res.then(
3325
+ (res2) => saveBatchComputed(key2, data, res2, blanks)
3326
+ )
3327
+ );
3328
+ }
3329
+ }
3330
+ }
3331
+ }
3332
+ if (!promises)
3333
+ return;
3334
+ return Promise.all(promises).then(() => {
3335
+ finalizeNestedHookSelect(
3336
+ batches,
3337
+ originalReturnType,
3338
+ tempColumns,
3339
+ renames,
3340
+ key
3341
+ );
3342
+ applyBatchTransforms(query, batches);
3343
+ });
3344
+ };
3345
+ const saveBatchComputed = (key, result, res, blanks) => {
3346
+ const len = result.length;
3347
+ const actual = res.length + ((blanks == null ? void 0 : blanks.size) || 0);
3348
+ if (len !== actual) {
3349
+ throw new Error(
3350
+ `Incorrect length of batch computed result for column ${key}. Expected ${len}, received ${actual}.`
3351
+ );
3352
+ }
3353
+ if (blanks) {
3354
+ for (let i = 0, r = 0; i < len; i++) {
3355
+ if (!blanks.has(i)) {
3356
+ result[i][key] = res[r++];
3357
+ }
3358
+ }
3359
+ } else {
3360
+ for (let i = 0; i < len; i++) {
3361
+ result[i][key] = res[i];
3362
+ }
3363
+ }
3364
+ };
3123
3365
 
3366
+ var __defProp$d = Object.defineProperty;
3367
+ var __getOwnPropSymbols$e = Object.getOwnPropertySymbols;
3368
+ var __hasOwnProp$e = Object.prototype.hasOwnProperty;
3369
+ var __propIsEnum$e = Object.prototype.propertyIsEnumerable;
3370
+ var __defNormalProp$d = (obj, key, value) => key in obj ? __defProp$d(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3371
+ var __spreadValues$d = (a, b) => {
3372
+ for (var prop in b || (b = {}))
3373
+ if (__hasOwnProp$e.call(b, prop))
3374
+ __defNormalProp$d(a, prop, b[prop]);
3375
+ if (__getOwnPropSymbols$e)
3376
+ for (var prop of __getOwnPropSymbols$e(b)) {
3377
+ if (__propIsEnum$e.call(b, prop))
3378
+ __defNormalProp$d(a, prop, b[prop]);
3379
+ }
3380
+ return a;
3381
+ };
3124
3382
  const queryMethodByReturnType = {
3383
+ undefined: "query",
3125
3384
  all: "query",
3126
3385
  rows: "arrays",
3127
3386
  pluck: "arrays",
@@ -3169,9 +3428,6 @@ Object.defineProperty(Then.prototype, "then", {
3169
3428
  });
3170
3429
  }
3171
3430
  });
3172
- const handleResult = (q, returnType, result, isSubQuery) => {
3173
- return parseResult(q, q.q.parsers, returnType, result, isSubQuery);
3174
- };
3175
3431
  function maybeWrappedThen(resolve, reject) {
3176
3432
  const { q } = this;
3177
3433
  let beforeHooks;
@@ -3229,7 +3485,7 @@ const callAfterHook = function(cb) {
3229
3485
  };
3230
3486
  const beginSql = { text: "BEGIN" };
3231
3487
  const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks, resolve, reject) => {
3232
- var _a;
3488
+ var _a, _b;
3233
3489
  const { q: query } = q;
3234
3490
  let sql;
3235
3491
  let logData;
@@ -3246,7 +3502,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3246
3502
  sql = q.toSQL();
3247
3503
  const { hookSelect } = sql;
3248
3504
  const { returnType = "all" } = query;
3249
- const returns = hookSelect ? "all" : returnType;
3505
+ const tempReturnType = hookSelect || returnType === "rows" && q.q.batchParsers ? "all" : returnType;
3250
3506
  let result;
3251
3507
  let queryResult;
3252
3508
  if ("text" in sql) {
@@ -3256,7 +3512,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3256
3512
  if (query.log) {
3257
3513
  logData = query.log.beforeQuery(sql);
3258
3514
  }
3259
- queryResult = await adapter[hookSelect ? "query" : queryMethodByReturnType[returnType]](sql);
3515
+ queryResult = await adapter[queryMethodByReturnType[tempReturnType]](sql);
3260
3516
  if (query.patchResult) {
3261
3517
  await query.patchResult(q, queryResult);
3262
3518
  }
@@ -3264,9 +3520,9 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3264
3520
  query.log.afterQuery(sql, logData);
3265
3521
  sql = void 0;
3266
3522
  }
3267
- result = query.handleResult(q, returns, queryResult);
3523
+ result = query.handleResult(q, tempReturnType, queryResult);
3268
3524
  } else {
3269
- const queryMethod = hookSelect ? "query" : queryMethodByReturnType[returnType];
3525
+ const queryMethod = queryMethodByReturnType[tempReturnType];
3270
3526
  if (!trx) {
3271
3527
  if (query.log)
3272
3528
  logData = query.log.beforeQuery(beginSql);
@@ -3301,9 +3557,37 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3301
3557
  if (query.patchResult) {
3302
3558
  await query.patchResult(q, queryResult);
3303
3559
  }
3304
- result = query.handleResult(q, returns, queryResult);
3560
+ result = query.handleResult(q, tempReturnType, queryResult);
3561
+ }
3562
+ if (result && typeof result === "object" && typeof result.then === "function") {
3563
+ result = await result;
3564
+ }
3565
+ let tempColumns;
3566
+ let renames;
3567
+ if (hookSelect) {
3568
+ for (const column of hookSelect.keys()) {
3569
+ const as = hookSelect.get(column).as;
3570
+ if (as)
3571
+ (renames != null ? renames : renames = {})[column] = as;
3572
+ (_a = tempColumns != null ? tempColumns : tempColumns = /* @__PURE__ */ new Set()) == null ? void 0 : _a.add(as || column);
3573
+ }
3574
+ if (renames) {
3575
+ for (const record of result) {
3576
+ for (const a in renames) {
3577
+ const value = record[renames[a]];
3578
+ record[renames[a]] = record[a];
3579
+ record[a] = value;
3580
+ }
3581
+ }
3582
+ }
3583
+ if (query.selectedComputeds) {
3584
+ const promise = processComputedResult(query, result);
3585
+ if (promise)
3586
+ await promise;
3587
+ }
3305
3588
  }
3306
- if (afterHooks || afterCommitHooks || query.after) {
3589
+ const hasAfterHook = afterHooks || afterCommitHooks || query.after;
3590
+ if (hasAfterHook) {
3307
3591
  if (queryResult.rowCount) {
3308
3592
  if (afterHooks || query.after) {
3309
3593
  const args = [result, q];
@@ -3315,7 +3599,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3315
3599
  );
3316
3600
  }
3317
3601
  if (afterCommitHooks && trx) {
3318
- ((_a = trx.afterCommit) != null ? _a : trx.afterCommit = []).push(
3602
+ ((_b = trx.afterCommit) != null ? _b : trx.afterCommit = []).push(
3319
3603
  result,
3320
3604
  q,
3321
3605
  afterCommitHooks
@@ -3328,8 +3612,24 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3328
3612
  const args = [result, q];
3329
3613
  await Promise.all(query.after.map(callAfterHook, args));
3330
3614
  }
3331
- if (hookSelect)
3332
- result = filterResult(q, returnType, queryResult, hookSelect, result);
3615
+ }
3616
+ if (hookSelect || tempReturnType !== returnType) {
3617
+ if (renames) {
3618
+ for (const record of result) {
3619
+ for (const a in renames) {
3620
+ record[a] = record[renames[a]];
3621
+ }
3622
+ }
3623
+ }
3624
+ result = filterResult(
3625
+ q,
3626
+ returnType,
3627
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3628
+ queryResult,
3629
+ result,
3630
+ tempColumns,
3631
+ hasAfterHook
3632
+ );
3333
3633
  }
3334
3634
  if (query.transform) {
3335
3635
  result = orchidCore.applyTransforms(returnType, query.transform, result);
@@ -3375,57 +3675,91 @@ const assignError = (to, from) => {
3375
3675
  to.routine = from.routine;
3376
3676
  return to;
3377
3677
  };
3378
- const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
3678
+ const handleResult = (q, returnType, result, isSubQuery) => {
3379
3679
  var _a, _b;
3680
+ const { parsers } = q.q;
3380
3681
  switch (returnType) {
3381
3682
  case "all": {
3382
3683
  if (q.q.throwOnNotFound && result.rows.length === 0)
3383
3684
  throw new NotFoundError(q);
3685
+ const promise = parseBatch(q, result);
3384
3686
  const { rows } = result;
3385
3687
  if (parsers) {
3386
3688
  for (const row of rows) {
3387
3689
  parseRecord(parsers, row);
3388
3690
  }
3389
3691
  }
3390
- return rows;
3692
+ return promise ? promise.then(() => rows) : rows;
3391
3693
  }
3392
3694
  case "one": {
3393
- const row = result.rows[0];
3394
- if (!row)
3695
+ const { rows } = result;
3696
+ if (!rows.length)
3395
3697
  return;
3396
- return parsers ? parseRecord(parsers, row) : row;
3698
+ const promise = parseBatch(q, result);
3699
+ if (parsers)
3700
+ parseRecord(parsers, rows[0]);
3701
+ return promise ? promise.then(() => rows[0]) : rows[0];
3397
3702
  }
3398
3703
  case "oneOrThrow": {
3399
- const row = result.rows[0];
3400
- if (!row)
3704
+ const { rows } = result;
3705
+ if (!rows.length)
3401
3706
  throw new NotFoundError(q);
3402
- return parsers ? parseRecord(parsers, row) : row;
3707
+ const promise = parseBatch(q, result);
3708
+ if (parsers)
3709
+ parseRecord(parsers, rows[0]);
3710
+ return promise ? promise.then(() => rows[0]) : rows[0];
3403
3711
  }
3404
3712
  case "rows": {
3405
- return parsers ? parseRows(
3406
- parsers,
3407
- result.fields,
3408
- result.rows
3409
- ) : result.rows;
3713
+ const { rows } = result;
3714
+ const promise = parseBatch(q, result);
3715
+ if (promise) {
3716
+ return promise.then(() => {
3717
+ if (parsers)
3718
+ parseRows(parsers, result.fields, rows);
3719
+ return rows;
3720
+ });
3721
+ } else if (parsers) {
3722
+ parseRows(parsers, result.fields, rows);
3723
+ }
3724
+ return rows;
3410
3725
  }
3411
3726
  case "pluck": {
3412
- const pluck = parsers == null ? void 0 : parsers.pluck;
3413
- if (pluck) {
3414
- return result.rows.map(isSubQuery ? pluck : (row) => pluck(row[0]));
3415
- } else if (isSubQuery) {
3416
- return result.rows;
3727
+ const { rows } = result;
3728
+ const promise = parseBatch(q, result);
3729
+ if (promise) {
3730
+ return promise.then(() => {
3731
+ parsePluck(parsers, isSubQuery, rows);
3732
+ return rows;
3733
+ });
3417
3734
  }
3418
- return result.rows.map((row) => row[0]);
3735
+ parsePluck(parsers, isSubQuery, rows);
3736
+ return rows;
3419
3737
  }
3420
3738
  case "value": {
3421
- const value = (_a = result.rows[0]) == null ? void 0 : _a[0];
3422
- return value !== void 0 ? parseValue(value, parsers) : q.q.notFoundDefault;
3739
+ const { rows } = result;
3740
+ const promise = parseBatch(q, result);
3741
+ if (promise) {
3742
+ return promise.then(() => {
3743
+ var _a2;
3744
+ return ((_a2 = rows[0]) == null ? void 0 : _a2[0]) !== void 0 ? parseValue(rows[0][0], parsers) : q.q.notFoundDefault;
3745
+ });
3746
+ }
3747
+ return ((_a = rows[0]) == null ? void 0 : _a[0]) !== void 0 ? parseValue(rows[0][0], parsers) : q.q.notFoundDefault;
3423
3748
  }
3424
3749
  case "valueOrThrow": {
3425
- const value = (_b = result.rows[0]) == null ? void 0 : _b[0];
3426
- if (value === void 0)
3750
+ const { rows } = result;
3751
+ const promise = parseBatch(q, result);
3752
+ if (promise) {
3753
+ return promise.then(() => {
3754
+ var _a2;
3755
+ if (((_a2 = rows[0]) == null ? void 0 : _a2[0]) === void 0)
3756
+ throw new NotFoundError(q);
3757
+ return parseValue(rows[0][0], parsers);
3758
+ });
3759
+ }
3760
+ if (((_b = rows[0]) == null ? void 0 : _b[0]) === void 0)
3427
3761
  throw new NotFoundError(q);
3428
- return parseValue(value, parsers);
3762
+ return parseValue(rows[0][0], parsers);
3429
3763
  }
3430
3764
  case "rowCount": {
3431
3765
  if (q.q.throwOnNotFound && result.rowCount === 0) {
@@ -3438,6 +3772,17 @@ const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
3438
3772
  }
3439
3773
  }
3440
3774
  };
3775
+ const parseBatch = (q, queryResult) => {
3776
+ let promises;
3777
+ if (q.q.batchParsers) {
3778
+ for (const parser of q.q.batchParsers) {
3779
+ const res = parser.fn(parser.path, queryResult);
3780
+ if (res)
3781
+ (promises != null ? promises : promises = []).push(res);
3782
+ }
3783
+ }
3784
+ return promises && Promise.all(promises);
3785
+ };
3441
3786
  const parseRecord = (parsers, row) => {
3442
3787
  for (const key in parsers) {
3443
3788
  if (key in row) {
@@ -3455,38 +3800,43 @@ const parseRows = (parsers, fields, rows) => {
3455
3800
  }
3456
3801
  }
3457
3802
  }
3458
- return rows;
3803
+ };
3804
+ const parsePluck = (parsers, isSubQuery, rows) => {
3805
+ const pluck = parsers == null ? void 0 : parsers.pluck;
3806
+ if (pluck) {
3807
+ for (let i = 0; i < rows.length; i++) {
3808
+ rows[i] = pluck(isSubQuery ? rows[i] : rows[i][0]);
3809
+ }
3810
+ } else if (!isSubQuery) {
3811
+ for (let i = 0; i < rows.length; i++) {
3812
+ rows[i] = rows[i][0];
3813
+ }
3814
+ }
3459
3815
  };
3460
3816
  const parseValue = (value, parsers) => {
3461
3817
  const parser = parsers == null ? void 0 : parsers[orchidCore.getValueKey];
3462
3818
  return parser ? parser(value) : value;
3463
3819
  };
3464
- const filterResult = (q, returnType, queryResult, hookSelect, result) => {
3820
+ const filterResult = (q, returnType, queryResult, result, tempColumns, hasAfterHook) => {
3465
3821
  var _a;
3466
3822
  if (returnType === "all") {
3467
- const pick = getSelectPick(queryResult, hookSelect);
3468
- return result.map((full) => {
3469
- const filtered = {};
3470
- for (const key of pick) {
3471
- filtered[key] = full[key];
3472
- }
3473
- return filtered;
3474
- });
3823
+ return filterAllResult(result, tempColumns, hasAfterHook);
3475
3824
  }
3476
3825
  if (returnType === "oneOrThrow" || returnType === "one") {
3477
- const row = result[0];
3826
+ let row = result[0];
3478
3827
  if (!row) {
3479
3828
  if (returnType === "oneOrThrow")
3480
3829
  throw new NotFoundError(q);
3481
3830
  return void 0;
3831
+ } else if (!(tempColumns == null ? void 0 : tempColumns.size)) {
3832
+ return row;
3482
3833
  } else {
3483
- result = {};
3484
- for (const key in row) {
3485
- if (!hookSelect.includes(key)) {
3486
- result[key] = row[key];
3487
- }
3834
+ if (hasAfterHook)
3835
+ row = __spreadValues$d({}, row);
3836
+ for (const column of tempColumns) {
3837
+ delete row[column];
3488
3838
  }
3489
- return result;
3839
+ return row;
3490
3840
  }
3491
3841
  }
3492
3842
  if (returnType === "value") {
@@ -3506,79 +3856,270 @@ const filterResult = (q, returnType, queryResult, hookSelect, result) => {
3506
3856
  return result.map((row) => row[key]);
3507
3857
  }
3508
3858
  if (returnType === "rows") {
3509
- const pick = getSelectPick(queryResult, hookSelect);
3510
- return result.map(
3511
- (full) => pick.map((key) => full[key])
3512
- );
3859
+ result = filterAllResult(result, tempColumns, hasAfterHook);
3860
+ return result.map((record) => Object.values(record));
3513
3861
  }
3514
3862
  return;
3515
3863
  };
3516
- const getSelectPick = (queryResult, hookSelect) => {
3517
- const pick = [];
3518
- for (const field of queryResult.fields) {
3519
- if (!hookSelect.includes(field.name))
3520
- pick.push(field.name);
3864
+ const filterAllResult = (result, tempColumns, hasAfterHook) => {
3865
+ if (tempColumns == null ? void 0 : tempColumns.size) {
3866
+ if (hasAfterHook) {
3867
+ return result.map((data) => {
3868
+ const record = __spreadValues$d({}, data);
3869
+ for (const key of tempColumns) {
3870
+ delete record[key];
3871
+ }
3872
+ return record;
3873
+ });
3874
+ } else {
3875
+ for (const record of result) {
3876
+ for (const key of tempColumns) {
3877
+ delete record[key];
3878
+ }
3879
+ }
3880
+ }
3521
3881
  }
3522
- return pick;
3882
+ return result;
3523
3883
  };
3524
3884
 
3885
+ var __defProp$c = Object.defineProperty;
3886
+ var __defProps$3 = Object.defineProperties;
3887
+ var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
3888
+ var __getOwnPropSymbols$d = Object.getOwnPropertySymbols;
3889
+ var __hasOwnProp$d = Object.prototype.hasOwnProperty;
3890
+ var __propIsEnum$d = Object.prototype.propertyIsEnumerable;
3891
+ var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3892
+ var __spreadValues$c = (a, b) => {
3893
+ for (var prop in b || (b = {}))
3894
+ if (__hasOwnProp$d.call(b, prop))
3895
+ __defNormalProp$c(a, prop, b[prop]);
3896
+ if (__getOwnPropSymbols$d)
3897
+ for (var prop of __getOwnPropSymbols$d(b)) {
3898
+ if (__propIsEnum$d.call(b, prop))
3899
+ __defNormalProp$c(a, prop, b[prop]);
3900
+ }
3901
+ return a;
3902
+ };
3903
+ var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
3525
3904
  const addParserForRawExpression = (q, key, raw) => {
3526
3905
  const type = raw.result.value;
3527
3906
  if (type == null ? void 0 : type.parseFn)
3528
3907
  orchidCore.setParserToQuery(q.q, key, type.parseFn);
3529
3908
  };
3530
- const subQueryResult = {
3531
- // sub query can't return a rowCount, use -1 as for impossible case
3532
- rowCount: -1,
3533
- rows: orchidCore.emptyArray,
3534
- fields: orchidCore.emptyArray
3535
- };
3536
3909
  const addParsersForSelectJoined = (q, arg, as = arg) => {
3537
- var _a;
3910
+ var _a, _b, _c, _d;
3538
3911
  const parsers = (_a = q.q.joinedParsers) == null ? void 0 : _a[arg];
3539
3912
  if (parsers) {
3540
3913
  orchidCore.setParserToQuery(q.q, as, (row) => parseRecord(parsers, row));
3541
3914
  }
3915
+ const batchParsers = (_b = q.q.joinedBatchParsers) == null ? void 0 : _b[arg];
3916
+ if (batchParsers) {
3917
+ ((_d = (_c = q.q).batchParsers) != null ? _d : _c.batchParsers = []).push(
3918
+ ...batchParsers.map((x) => ({
3919
+ path: [as, ...x.path],
3920
+ fn: x.fn
3921
+ }))
3922
+ );
3923
+ }
3542
3924
  };
3543
3925
  const addParserForSelectItem = (q, as, key, arg) => {
3926
+ var _a, _b, _c, _d;
3544
3927
  if (typeof arg === "object" || typeof arg === "function") {
3545
3928
  if (orchidCore.isExpression(arg)) {
3546
3929
  addParserForRawExpression(q, key, arg);
3547
3930
  } else {
3548
3931
  const { q: query } = arg;
3549
- if (query.parsers || query.transform) {
3550
- orchidCore.setParserToQuery(q.q, key, (item) => {
3551
- const t = query.returnType || "all";
3552
- subQueryResult.rows = t === "value" || t === "valueOrThrow" ? [[item]] : t === "one" || t === "oneOrThrow" ? [item] : item;
3553
- const result = query.handleResult(arg, t, subQueryResult, true);
3554
- return query.transform ? orchidCore.applyTransforms(t, query.transform, result) : result;
3555
- });
3932
+ if (query.batchParsers) {
3933
+ const batchParsers = (_b = (_a = q.q).batchParsers) != null ? _b : _a.batchParsers = [];
3934
+ for (const bp of query.batchParsers) {
3935
+ batchParsers.push({ path: [key, ...bp.path], fn: bp.fn });
3936
+ }
3556
3937
  }
3557
- if (query.returnType === "valueOrThrow" || query.returnType === "oneOrThrow") {
3558
- pushQueryValue(
3559
- q,
3560
- "transform",
3561
- (data) => {
3562
- if (Array.isArray(data)) {
3563
- for (const item of data) {
3564
- if (item[key] === void 0) {
3565
- throw new NotFoundError(q);
3938
+ if (query.hookSelect || query.parsers || query.transform) {
3939
+ const batchParsers = (_d = (_c = q.q).batchParsers) != null ? _d : _c.batchParsers = [];
3940
+ batchParsers.push({
3941
+ path: [key],
3942
+ fn: (path, queryResult) => {
3943
+ var _a2, _b2, _c2;
3944
+ const { rows } = queryResult;
3945
+ const originalReturnType = query.returnType || "all";
3946
+ let returnType = originalReturnType;
3947
+ const { hookSelect } = query;
3948
+ const batches = [];
3949
+ let last = path.length;
3950
+ if (returnType === "value" || returnType === "valueOrThrow") {
3951
+ if (hookSelect) {
3952
+ batches.push = (item) => {
3953
+ if (!(key in item)) {
3954
+ returnType = returnType === "value" ? "one" : "oneOrThrow";
3955
+ }
3956
+ batches.push = Array.prototype.push;
3957
+ return batches.push(item);
3958
+ };
3959
+ } else {
3960
+ last--;
3961
+ }
3962
+ }
3963
+ collectNestedSelectBatches(batches, rows, path, last);
3964
+ switch (returnType) {
3965
+ case "all": {
3966
+ const { parsers } = query;
3967
+ if (parsers) {
3968
+ for (const { data } of batches) {
3969
+ for (const one of data) {
3970
+ parseRecord(parsers, one);
3971
+ }
3972
+ }
3566
3973
  }
3974
+ break;
3567
3975
  }
3568
- } else {
3569
- if (data[key] === void 0) {
3570
- throw new NotFoundError(q);
3976
+ case "one":
3977
+ case "oneOrThrow": {
3978
+ const { parsers } = query;
3979
+ if (parsers) {
3980
+ if (returnType === "one") {
3981
+ for (const { data } of batches) {
3982
+ if (data)
3983
+ parseRecord(parsers, data);
3984
+ }
3985
+ } else {
3986
+ for (const { data } of batches) {
3987
+ if (!data)
3988
+ throw new NotFoundError(arg);
3989
+ parseRecord(parsers, data);
3990
+ }
3991
+ }
3992
+ } else if (returnType !== "one") {
3993
+ for (const { data } of batches) {
3994
+ if (!data)
3995
+ throw new NotFoundError(arg);
3996
+ }
3997
+ }
3998
+ if (hookSelect) {
3999
+ for (const batch of batches) {
4000
+ batch.data = [batch.data];
4001
+ }
4002
+ }
4003
+ break;
4004
+ }
4005
+ case "pluck": {
4006
+ const parse = (_a2 = query.parsers) == null ? void 0 : _a2.pluck;
4007
+ if (parse) {
4008
+ for (const { data } of batches) {
4009
+ for (let i = 0; i < data.length; i++) {
4010
+ data[i] = parse(data[i]);
4011
+ }
4012
+ }
4013
+ }
4014
+ break;
4015
+ }
4016
+ case "value":
4017
+ case "valueOrThrow": {
4018
+ const parse = (_b2 = query.parsers) == null ? void 0 : _b2[orchidCore.getValueKey];
4019
+ if (parse) {
4020
+ if (returnType === "value") {
4021
+ for (const { data } of batches) {
4022
+ data[key] = data[key] === void 0 ? arg.q.notFoundDefault : parse(data[key]);
4023
+ }
4024
+ } else {
4025
+ for (const { data } of batches) {
4026
+ if (data[key] === void 0)
4027
+ throw new NotFoundError(arg);
4028
+ data[key] = parse(data[key]);
4029
+ }
4030
+ }
4031
+ } else if (returnType !== "value") {
4032
+ for (const { data } of batches) {
4033
+ if (data[key] === void 0)
4034
+ throw new NotFoundError(arg);
4035
+ }
4036
+ }
4037
+ if (hookSelect) {
4038
+ for (const batch of batches) {
4039
+ batch.data = [batch.data];
4040
+ }
4041
+ }
4042
+ break;
4043
+ }
4044
+ }
4045
+ if (hookSelect) {
4046
+ let tempColumns;
4047
+ let renames;
4048
+ for (const column of hookSelect.keys()) {
4049
+ const as2 = hookSelect.get(column).as;
4050
+ if (as2)
4051
+ (renames != null ? renames : renames = {})[column] = as2;
4052
+ (_c2 = tempColumns != null ? tempColumns : tempColumns = /* @__PURE__ */ new Set()) == null ? void 0 : _c2.add(as2 || column);
4053
+ }
4054
+ if (renames) {
4055
+ for (const { data } of batches) {
4056
+ for (const record of data) {
4057
+ if (record) {
4058
+ for (const a in renames) {
4059
+ const value = record[renames[a]];
4060
+ record[renames[a]] = record[a];
4061
+ record[a] = value;
4062
+ }
4063
+ }
4064
+ }
4065
+ }
4066
+ }
4067
+ if (query.selectedComputeds) {
4068
+ const maybePromise = processComputedBatches(
4069
+ query,
4070
+ batches,
4071
+ originalReturnType,
4072
+ returnType,
4073
+ tempColumns,
4074
+ renames,
4075
+ key
4076
+ );
4077
+ if (maybePromise)
4078
+ return maybePromise;
3571
4079
  }
4080
+ finalizeNestedHookSelect(
4081
+ batches,
4082
+ originalReturnType,
4083
+ tempColumns,
4084
+ renames,
4085
+ key
4086
+ );
3572
4087
  }
3573
- return data;
4088
+ applyBatchTransforms(query, batches);
4089
+ return;
3574
4090
  }
3575
- );
4091
+ });
3576
4092
  }
3577
4093
  }
3578
4094
  return arg;
3579
4095
  }
3580
4096
  return setParserForSelectedString(q, arg, as, key);
3581
4097
  };
4098
+ const collectNestedSelectBatches = (batches, rows, path, last) => {
4099
+ const stack = rows.map(
4100
+ (row) => ({
4101
+ data: row,
4102
+ i: 0
4103
+ })
4104
+ );
4105
+ while (stack.length > 0) {
4106
+ const item = stack.pop();
4107
+ const { i } = item;
4108
+ if (i === last) {
4109
+ batches.push(item);
4110
+ continue;
4111
+ }
4112
+ const { data } = item;
4113
+ const key = path[i];
4114
+ if (Array.isArray(data)) {
4115
+ for (let key2 = 0; key2 < data.length; key2++) {
4116
+ stack.push({ data: data[key2], parent: data, key: key2, i });
4117
+ }
4118
+ } else if (data && typeof data === "object") {
4119
+ stack.push({ data: data[key], parent: data, key, i: i + 1 });
4120
+ }
4121
+ }
4122
+ };
3582
4123
  const emptyArrSQL = new RawSQL("'[]'");
3583
4124
  const processSelectArg = (q, as, arg, columnAs) => {
3584
4125
  var _a, _b, _c, _d;
@@ -3592,7 +4133,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
3592
4133
  value = resolveSubQueryCallback(q, value);
3593
4134
  if (isQueryNone(value)) {
3594
4135
  if (value.q.innerJoinLateral) {
3595
- return;
4136
+ return false;
3596
4137
  }
3597
4138
  } else if (!orchidCore.isExpression(value) && value.joinQuery) {
3598
4139
  value = value.joinQuery(value, q);
@@ -3602,17 +4143,23 @@ const processSelectArg = (q, as, arg, columnAs) => {
3602
4143
  query = value.json(false);
3603
4144
  value.q.coalesceValue = emptyArrSQL;
3604
4145
  } else if (returnType === "pluck") {
3605
- query = value.wrap(value.baseQuery.clone()).jsonAgg(value.q.select[0]);
4146
+ query = value.q.select ? value.wrap(value.baseQuery.clone()).jsonAgg(value.q.select[0]) : value.json(false);
3606
4147
  value.q.coalesceValue = emptyArrSQL;
3607
4148
  } else {
3608
- if ((returnType === "value" || returnType === "valueOrThrow") && value.q.select) {
3609
- if (typeof value.q.select[0] === "string") {
3610
- value.q.select[0] = {
3611
- selectAs: { r: value.q.select[0] }
3612
- };
4149
+ if (returnType === "value" || returnType === "valueOrThrow") {
4150
+ if (value.q.select) {
4151
+ if (typeof value.q.select[0] === "string") {
4152
+ value.q.select[0] = {
4153
+ selectAs: { r: value.q.select[0] }
4154
+ };
4155
+ }
4156
+ query = value;
4157
+ } else {
4158
+ query = value.json(false);
3613
4159
  }
4160
+ } else {
4161
+ query = value;
3614
4162
  }
3615
- query = value;
3616
4163
  }
3617
4164
  let asOverride = key;
3618
4165
  if ((_a = value.q.joinedShapes) == null ? void 0 : _a[key]) {
@@ -3646,7 +4193,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
3646
4193
  return { selectAs };
3647
4194
  };
3648
4195
  const setParserForSelectedString = (q, arg, as, columnAs) => {
3649
- var _a, _b, _c, _d;
4196
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
3650
4197
  const index = arg.indexOf(".");
3651
4198
  if (index !== -1) {
3652
4199
  const table = arg.slice(0, index);
@@ -3654,25 +4201,60 @@ const setParserForSelectedString = (q, arg, as, columnAs) => {
3654
4201
  if (column === "*") {
3655
4202
  addParsersForSelectJoined(q, table, columnAs);
3656
4203
  return table === as ? column : arg;
3657
- } else {
3658
- if (table === as) {
4204
+ } else if (table === as) {
4205
+ if (columnAs) {
3659
4206
  const parser = (_a = q.q.parsers) == null ? void 0 : _a[column];
3660
4207
  if (parser)
3661
- orchidCore.setParserToQuery(q.q, columnAs || column, parser);
3662
- return column;
3663
- } else {
3664
- const parser = (_c = (_b = q.q.joinedParsers) == null ? void 0 : _b[table]) == null ? void 0 : _c[column];
3665
- if (parser)
3666
- orchidCore.setParserToQuery(q.q, columnAs || column, parser);
3667
- return arg;
4208
+ q.q.parsers[columnAs] = parser;
3668
4209
  }
4210
+ return handleComputed(q, q.q.computeds, column);
4211
+ } else {
4212
+ const parser = (_c = (_b = q.q.joinedParsers) == null ? void 0 : _b[table]) == null ? void 0 : _c[column];
4213
+ if (parser)
4214
+ orchidCore.setParserToQuery(q.q, columnAs || column, parser);
4215
+ const batchParsers = (_d = q.q.joinedBatchParsers) == null ? void 0 : _d[table];
4216
+ if (batchParsers) {
4217
+ for (const bp of batchParsers) {
4218
+ if (bp.path[0] === column) {
4219
+ ((_f = (_e = q.q).batchParsers) != null ? _f : _e.batchParsers = []).push(bp);
4220
+ }
4221
+ }
4222
+ }
4223
+ const computeds = (_g = q.q.joinedComputeds) == null ? void 0 : _g[table];
4224
+ if (computeds == null ? void 0 : computeds[column]) {
4225
+ const computed = computeds[column];
4226
+ const map = (_i = (_h = q.q).hookSelect) != null ? _i : _h.hookSelect = /* @__PURE__ */ new Map();
4227
+ for (const column2 of computed.deps) {
4228
+ map.set(column2, { select: `${table}.${column2}` });
4229
+ }
4230
+ q.q.selectedComputeds = __spreadProps$3(__spreadValues$c({}, q.q.selectedComputeds), {
4231
+ [column]: computed
4232
+ });
4233
+ return;
4234
+ }
4235
+ return arg;
3669
4236
  }
3670
4237
  } else {
3671
- const parser = (_d = q.q.parsers) == null ? void 0 : _d[arg];
3672
- if (parser)
3673
- orchidCore.setParserToQuery(q.q, columnAs || arg, parser);
3674
- return arg;
4238
+ if (columnAs) {
4239
+ const parser = (_j = q.q.parsers) == null ? void 0 : _j[arg];
4240
+ if (parser)
4241
+ q.q.parsers[columnAs] = parser;
4242
+ }
4243
+ return handleComputed(q, q.q.computeds, arg);
4244
+ }
4245
+ };
4246
+ const handleComputed = (q, computeds, column) => {
4247
+ var _a, _b;
4248
+ if (computeds == null ? void 0 : computeds[column]) {
4249
+ const computed = computeds[column];
4250
+ const map = (_b = (_a = q.q).hookSelect) != null ? _b : _a.hookSelect = /* @__PURE__ */ new Map();
4251
+ for (const column2 of computed.deps) {
4252
+ map.set(column2, { select: column2 });
4253
+ }
4254
+ q.q.selectedComputeds = __spreadProps$3(__spreadValues$c({}, q.q.selectedComputeds), { [column]: computed });
4255
+ return;
3675
4256
  }
4257
+ return column;
3676
4258
  };
3677
4259
  const getShapeFromSelect = (q, isSubQuery) => {
3678
4260
  const query = q.q;
@@ -3693,7 +4275,9 @@ const getShapeFromSelect = (q, isSubQuery) => {
3693
4275
  for (const item of select) {
3694
4276
  if (typeof item === "string") {
3695
4277
  addColumnToShapeFromSelect(q, item, shape, query, result, isSubQuery);
3696
- } else if ("selectAs" in item) {
4278
+ } else if (orchidCore.isExpression(item)) {
4279
+ result.value = item.result.value;
4280
+ } else if (item && "selectAs" in item) {
3697
4281
  for (const key in item.selectAs) {
3698
4282
  const it = item.selectAs[key];
3699
4283
  if (typeof it === "string") {
@@ -3708,7 +4292,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
3708
4292
  );
3709
4293
  } else if (orchidCore.isExpression(it)) {
3710
4294
  result[key] = it.result.value;
3711
- } else {
4295
+ } else if (it) {
3712
4296
  const { returnType } = it.q;
3713
4297
  if (returnType === "value" || returnType === "valueOrThrow") {
3714
4298
  const type = it.q[orchidCore.getValueKey];
@@ -3719,8 +4303,6 @@ const getShapeFromSelect = (q, isSubQuery) => {
3719
4303
  }
3720
4304
  }
3721
4305
  }
3722
- } else if (orchidCore.isExpression(item)) {
3723
- result.value = item.result.value;
3724
4306
  }
3725
4307
  }
3726
4308
  }
@@ -3756,12 +4338,13 @@ function _querySelect(q, args) {
3756
4338
  return q;
3757
4339
  }
3758
4340
  const as = q.q.as || q.table;
3759
- const selectArgs = new Array(len);
3760
- for (let i = 0; i < len; i++) {
3761
- selectArgs[i] = processSelectArg(q, as, args[i]);
3762
- if (!selectArgs[i]) {
4341
+ const selectArgs = [];
4342
+ for (const arg of args) {
4343
+ const item = processSelectArg(q, as, arg);
4344
+ if (item)
4345
+ selectArgs.push(item);
4346
+ else if (item === false)
3763
4347
  return _queryNone(q);
3764
- }
3765
4348
  }
3766
4349
  return pushQueryArray(q, "select", selectArgs);
3767
4350
  }
@@ -3831,20 +4414,25 @@ const _get = (query, returnType, arg) => {
3831
4414
  }
3832
4415
  }
3833
4416
  q[orchidCore.getValueKey] = type;
3834
- setParserForSelectedString(
4417
+ const selected = setParserForSelectedString(
3835
4418
  query,
3836
4419
  arg,
3837
4420
  getQueryAs(query),
3838
4421
  orchidCore.getValueKey
3839
4422
  );
3840
- q.expr = new SelectItemExpression(query, arg, type);
4423
+ q.select = selected ? [
4424
+ q.expr = new SelectItemExpression(
4425
+ query,
4426
+ selected,
4427
+ type
4428
+ )
4429
+ ] : void 0;
3841
4430
  } else {
3842
4431
  type = arg.result.value;
3843
4432
  q[orchidCore.getValueKey] = type;
3844
4433
  addParserForRawExpression(query, orchidCore.getValueKey, arg);
3845
- q.expr = arg;
4434
+ q.select = [q.expr = arg];
3846
4435
  }
3847
- q.select = [q.expr];
3848
4436
  return setQueryOperators(
3849
4437
  query,
3850
4438
  (type == null ? void 0 : type.operators) || Operators.any
@@ -3879,24 +4467,44 @@ class AsMethods {
3879
4467
  }
3880
4468
  }
3881
4469
 
4470
+ var __defProp$b = Object.defineProperty;
4471
+ var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
4472
+ var __hasOwnProp$c = Object.prototype.hasOwnProperty;
4473
+ var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
4474
+ var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4475
+ var __spreadValues$b = (a, b) => {
4476
+ for (var prop in b || (b = {}))
4477
+ if (__hasOwnProp$c.call(b, prop))
4478
+ __defNormalProp$b(a, prop, b[prop]);
4479
+ if (__getOwnPropSymbols$c)
4480
+ for (var prop of __getOwnPropSymbols$c(b)) {
4481
+ if (__propIsEnum$c.call(b, prop))
4482
+ __defNormalProp$b(a, prop, b[prop]);
4483
+ }
4484
+ return a;
4485
+ };
3882
4486
  function queryFrom(self, arg) {
3883
- var _a, _b;
4487
+ var _a, _b, _c;
3884
4488
  const data = self.q;
3885
4489
  if (typeof arg === "string") {
3886
4490
  data.as || (data.as = arg);
3887
- data.shape = (_a = data.withShapes) == null ? void 0 : _a[arg];
4491
+ const w = (_a = data.withShapes) == null ? void 0 : _a[arg];
4492
+ data.shape = (_b = w == null ? void 0 : w.shape) != null ? _b : orchidCore.emptyObject;
4493
+ data.computeds = w == null ? void 0 : w.computeds;
3888
4494
  } else if (orchidCore.isExpression(arg)) {
3889
4495
  data.as || (data.as = "t");
3890
4496
  } else if (Array.isArray(arg)) {
3891
4497
  const { shape } = data;
3892
- const parsers = (_b = data.parsers) != null ? _b : data.parsers = {};
4498
+ const parsers = (_c = data.parsers) != null ? _c : data.parsers = {};
3893
4499
  for (const item of arg) {
3894
4500
  if (typeof item === "string") {
3895
- const withShape = data.withShapes[item];
3896
- Object.assign(shape, withShape);
3897
- for (const key in withShape) {
3898
- if (withShape[key].parseFn) {
3899
- parsers[key] = withShape[key].parseFn;
4501
+ const w = data.withShapes[item];
4502
+ Object.assign(shape, w.shape);
4503
+ if (w.computeds)
4504
+ data.computeds = __spreadValues$b(__spreadValues$b({}, data.computeds), w.computeds);
4505
+ for (const key in w.shape) {
4506
+ if (w.shape[key].parseFn) {
4507
+ parsers[key] = w.shape[key].parseFn;
3900
4508
  }
3901
4509
  }
3902
4510
  } else if (!orchidCore.isExpression(item)) {
@@ -3909,6 +4517,7 @@ function queryFrom(self, arg) {
3909
4517
  data.as || (data.as = q.q.as || q.table || "t");
3910
4518
  data.shape = getShapeFromSelect(arg, true);
3911
4519
  data.parsers = q.q.parsers;
4520
+ data.batchParsers = q.q.batchParsers;
3912
4521
  }
3913
4522
  data.from = arg;
3914
4523
  return self;
@@ -4018,44 +4627,117 @@ function queryJson(self, coalesce) {
4018
4627
  const pushSelectSql = (ctx, table, query, quotedAs) => {
4019
4628
  ctx.sql.push(selectToSql(ctx, table, query, quotedAs));
4020
4629
  };
4021
- const selectToSql = (ctx, table, query, quotedAs) => {
4630
+ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect) => {
4631
+ var _a, _b;
4632
+ let selected;
4633
+ const list = [];
4022
4634
  if (query.select) {
4023
- const list = [];
4024
4635
  for (const item of query.select) {
4025
4636
  if (typeof item === "string") {
4026
- list.push(selectedStringToSQL(ctx, table, query, quotedAs, item));
4027
- } else if ("selectAs" in item) {
4028
- const obj = item.selectAs;
4029
- for (const as in obj) {
4030
- const value = obj[as];
4031
- if (typeof value === "object" || typeof value === "function") {
4032
- if (orchidCore.isExpression(value)) {
4033
- list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
4034
- } else {
4035
- pushSubQuerySql(ctx, value, as, list, quotedAs);
4637
+ let sql;
4638
+ if (item === "*") {
4639
+ if (hookSelect) {
4640
+ selected != null ? selected : selected = {};
4641
+ for (const key in table.internal.columnsKeysForSelectAll || query.shape) {
4642
+ selected[key] = quotedAs;
4036
4643
  }
4037
- } else {
4038
- list.push(
4039
- `${columnToSql(
4040
- ctx,
4041
- table.q,
4042
- table.q.shape,
4043
- value,
4044
- quotedAs,
4045
- true
4046
- )} "${as}"`
4644
+ }
4645
+ sql = selectAllSql(table, query, quotedAs);
4646
+ } else {
4647
+ const index = item.indexOf(".");
4648
+ if (index !== -1) {
4649
+ const tableName = item.slice(0, index);
4650
+ const key = item.slice(index + 1);
4651
+ if (hookSelect == null ? void 0 : hookSelect.get(key))
4652
+ (selected != null ? selected : selected = {})[key] = `"${tableName}"`;
4653
+ sql = tableColumnToSqlWithAs(
4654
+ ctx,
4655
+ table.q,
4656
+ item,
4657
+ tableName,
4658
+ key,
4659
+ quotedAs,
4660
+ true
4047
4661
  );
4662
+ } else {
4663
+ if (hookSelect == null ? void 0 : hookSelect.get(item))
4664
+ (selected != null ? selected : selected = {})[item] = quotedAs;
4665
+ sql = ownColumnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4666
+ }
4667
+ }
4668
+ list.push(sql);
4669
+ } else if (item) {
4670
+ if ("selectAs" in item) {
4671
+ const obj = item.selectAs;
4672
+ for (const as in obj) {
4673
+ if (hookSelect)
4674
+ (selected != null ? selected : selected = {})[as] = true;
4675
+ const value = obj[as];
4676
+ if (typeof value === "object") {
4677
+ if (orchidCore.isExpression(value)) {
4678
+ list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
4679
+ } else {
4680
+ pushSubQuerySql(ctx, value, as, list, quotedAs);
4681
+ }
4682
+ } else if (value) {
4683
+ list.push(
4684
+ `${columnToSql(
4685
+ ctx,
4686
+ table.q,
4687
+ table.q.shape,
4688
+ value,
4689
+ quotedAs,
4690
+ true
4691
+ )} "${as}"`
4692
+ );
4693
+ }
4048
4694
  }
4695
+ } else {
4696
+ list.push(selectedObjectToSQL(ctx, quotedAs, item));
4049
4697
  }
4698
+ }
4699
+ }
4700
+ }
4701
+ if (hookSelect) {
4702
+ for (const column of hookSelect.keys()) {
4703
+ const item = hookSelect.get(column);
4704
+ const { select } = item;
4705
+ let sql;
4706
+ let quotedTable;
4707
+ let columnName;
4708
+ let col;
4709
+ const index = select.indexOf(".");
4710
+ if (index !== -1) {
4711
+ const tableName = select.slice(0, index);
4712
+ quotedTable = `"${tableName}"`;
4713
+ columnName = select.slice(index + 1);
4714
+ col = (_b = (_a = table.q.joinedShapes) == null ? void 0 : _a[tableName]) == null ? void 0 : _b[columnName];
4715
+ sql = (col == null ? void 0 : col.data.computed) ? col.data.computed.toSQL(ctx, `"${tableName}"`) : `"${tableName}"."${(col == null ? void 0 : col.data.name) || columnName}"`;
4050
4716
  } else {
4051
- list.push(selectedObjectToSQL(ctx, quotedAs, item));
4717
+ quotedTable = quotedAs;
4718
+ columnName = select;
4719
+ col = query.shape[select];
4720
+ sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4721
+ }
4722
+ if (selected == null ? void 0 : selected[columnName]) {
4723
+ if ((selected == null ? void 0 : selected[columnName]) === quotedTable) {
4724
+ hookSelect.delete(column);
4725
+ continue;
4726
+ }
4727
+ let i = 2;
4728
+ let name;
4729
+ while (selected[name = `${column}${i}`])
4730
+ i++;
4731
+ item.as = name;
4732
+ sql += ` "${name}"`;
4733
+ } else if (col == null ? void 0 : col.data.name) {
4734
+ sql += ` "${columnName}"`;
4052
4735
  }
4736
+ list.push(sql);
4053
4737
  }
4054
- return list.join(", ");
4055
4738
  }
4056
- return selectAllSql(table, query, quotedAs);
4739
+ return list.length ? list.join(", ") : selectAllSql(table, query, quotedAs);
4057
4740
  };
4058
- const selectedStringToSQL = (ctx, table, query, quotedAs, item) => item === "*" ? selectAllSql(table, query, quotedAs) : columnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4059
4741
  function selectedObjectToSQL(ctx, quotedAs, item) {
4060
4742
  const sql = item.toSQL(ctx, quotedAs);
4061
4743
  return ctx.aliasValue ? `${sql} r` : sql;
@@ -4065,7 +4747,7 @@ const selectAllSql = (table, query, quotedAs) => {
4065
4747
  return ((_a = query.join) == null ? void 0 : _a.length) ? ((_b = table.internal.columnsForSelectAll) == null ? void 0 : _b.map((item) => `${quotedAs}.${item}`).join(", ")) || `${quotedAs}.*` : ((_c = table.internal.columnsForSelectAll) == null ? void 0 : _c.join(", ")) || "*";
4066
4748
  };
4067
4749
  const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
4068
- var _a;
4750
+ var _a, _b, _c;
4069
4751
  const { returnType = "all" } = query.q;
4070
4752
  if (isQueryNone(query)) {
4071
4753
  let sql;
@@ -4129,20 +4811,27 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
4129
4811
  case "pluck": {
4130
4812
  const { select } = query.q;
4131
4813
  const first = select == null ? void 0 : select[0];
4132
- if (!select || !first) {
4814
+ if (!first && ((_b = query.q.computeds) == null ? void 0 : _b[as])) {
4815
+ query = queryJson(query);
4816
+ } else if (!first) {
4133
4817
  throw new OrchidOrmInternalError(
4134
4818
  query,
4135
4819
  `Nothing was selected for pluck`
4136
4820
  );
4821
+ } else {
4822
+ const cloned = query.clone();
4823
+ cloned.q.select = [{ selectAs: { c: first } }];
4824
+ query = queryWrap(cloned, cloned.baseQuery.clone());
4825
+ _queryGetOptional(query, new RawSQL(`COALESCE(json_agg("c"), '[]')`));
4137
4826
  }
4138
- const cloned = query.clone();
4139
- cloned.q.select = [{ selectAs: { c: first } }];
4140
- query = queryWrap(cloned, cloned.baseQuery.clone());
4141
- _queryGetOptional(query, new RawSQL(`COALESCE(json_agg("c"), '[]')`));
4142
4827
  break;
4143
4828
  }
4144
4829
  case "value":
4145
4830
  case "valueOrThrow":
4831
+ if ((_c = query.q.computeds) == null ? void 0 : _c[as]) {
4832
+ query = queryJson(query);
4833
+ }
4834
+ break;
4146
4835
  case "rows":
4147
4836
  case "rowCount":
4148
4837
  case "void":
@@ -4644,42 +5333,22 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs) =
4644
5333
  };
4645
5334
  const pushReturningSql = (ctx, q, data, quotedAs, hookSelect, keyword = "RETURNING") => {
4646
5335
  const { select } = data;
4647
- if (!(hookSelect == null ? void 0 : hookSelect.length) && !select)
4648
- return hookSelect;
4649
- let selected;
4650
- let hookFiltered;
4651
- if (select) {
4652
- selected = selectToSql(ctx, q, data, quotedAs);
5336
+ if (!(hookSelect == null ? void 0 : hookSelect.size) && !select)
5337
+ return hookSelect && /* @__PURE__ */ new Map();
5338
+ ctx.sql.push(keyword);
5339
+ if (q.q.hookSelect || hookSelect) {
5340
+ const tempSelect = new Map(q.q.hookSelect);
4653
5341
  if (hookSelect) {
4654
- if (select.includes("*")) {
4655
- hookFiltered = orchidCore.emptyArray;
4656
- } else {
4657
- hookFiltered = [];
4658
- for (const column of hookSelect) {
4659
- if (!hookFiltered.includes(column) && !(select == null ? void 0 : select.includes(column)) && !(select == null ? void 0 : select.includes(`${quotedAs}.${column}`))) {
4660
- hookFiltered.push(column);
4661
- }
4662
- }
5342
+ for (const column of hookSelect) {
5343
+ tempSelect.set(column, { select: column });
4663
5344
  }
4664
5345
  }
4665
- } else {
4666
- hookFiltered = [];
4667
- for (const column of hookSelect) {
4668
- if (!hookFiltered.includes(column))
4669
- hookFiltered.push(column);
4670
- }
4671
- }
4672
- ctx.sql.push(keyword);
4673
- if (hookFiltered == null ? void 0 : hookFiltered.length) {
4674
- if (selected)
4675
- ctx.sql.push(`${selected},`);
4676
- ctx.sql.push(
4677
- hookFiltered.map((column) => ownColumnToSql(data, column, quotedAs)).join(", ")
4678
- );
4679
- } else {
4680
- ctx.sql.push(selected);
5346
+ ctx.sql.push(selectToSql(ctx, q, data, quotedAs, tempSelect));
5347
+ return tempSelect;
5348
+ } else if (select) {
5349
+ ctx.sql.push(selectToSql(ctx, q, data, quotedAs));
4681
5350
  }
4682
- return hookFiltered;
5351
+ return;
4683
5352
  };
4684
5353
 
4685
5354
  const pushUpdateSql = (ctx, table, query, quotedAs) => {
@@ -5045,7 +5714,7 @@ const makeSQL = (table, options) => {
5045
5714
  if (query.for.mode)
5046
5715
  sql.push(query.for.mode);
5047
5716
  }
5048
- return { text: sql.join(" "), values };
5717
+ return { text: sql.join(" "), values, hookSelect: query.hookSelect };
5049
5718
  };
5050
5719
  function pushLimitSQL(sql, values, q) {
5051
5720
  if (!q.returnsOne) {
@@ -5078,6 +5747,8 @@ const cloneQuery = (q) => {
5078
5747
  q.with = q.with.slice(0);
5079
5748
  if (q.select)
5080
5749
  q.select = q.select.slice(0);
5750
+ if (q.hookSelect)
5751
+ q.hookSelect = new Map(q.hookSelect);
5081
5752
  if (q.and)
5082
5753
  q.and = q.and.slice(0);
5083
5754
  if (q.or)
@@ -5088,8 +5759,16 @@ const cloneQuery = (q) => {
5088
5759
  q.after = q.after.slice(0);
5089
5760
  if (q.joinedShapes)
5090
5761
  q.joinedShapes = __spreadValues$a({}, q.joinedShapes);
5762
+ if (q.joinedComputeds)
5763
+ q.joinedComputeds = __spreadValues$a({}, q.joinedComputeds);
5764
+ if (q.batchParsers)
5765
+ q.batchParsers = [...q.batchParsers];
5766
+ if (q.joinedBatchParsers)
5767
+ q.joinedBatchParsers = __spreadValues$a({}, q.joinedBatchParsers);
5091
5768
  if (q.scopes)
5092
5769
  q.scopes = __spreadValues$a({}, q.scopes);
5770
+ if (q.parsers)
5771
+ q.parsers = __spreadValues$a({}, q.parsers);
5093
5772
  if (q.updateData) {
5094
5773
  q.updateData = q.updateData.slice(
5095
5774
  0
@@ -5122,7 +5801,7 @@ const cloneQuery = (q) => {
5122
5801
  if (q.afterCreate) {
5123
5802
  q.afterCreate = q.afterCreate.slice(0);
5124
5803
  if (q.afterCreateSelect) {
5125
- q.afterCreateSelect = q.afterCreateSelect.slice(0);
5804
+ q.afterCreateSelect = new Set(q.afterCreateSelect);
5126
5805
  }
5127
5806
  }
5128
5807
  } else if (q.type === "update") {
@@ -5131,7 +5810,7 @@ const cloneQuery = (q) => {
5131
5810
  if (q.afterUpdate) {
5132
5811
  q.afterUpdate = q.afterUpdate.slice(0);
5133
5812
  if (q.afterUpdateSelect) {
5134
- q.afterUpdateSelect = q.afterUpdateSelect.slice(0);
5813
+ q.afterUpdateSelect = new Set(q.afterUpdateSelect);
5135
5814
  }
5136
5815
  }
5137
5816
  } else if (q.type === "delete") {
@@ -5140,7 +5819,7 @@ const cloneQuery = (q) => {
5140
5819
  if (q.afterDelete) {
5141
5820
  q.afterDelete = q.afterDelete.slice(0);
5142
5821
  if (q.afterDeleteSelect) {
5143
- q.afterDeleteSelect = q.afterDeleteSelect.slice(0);
5822
+ q.afterDeleteSelect = new Set(q.afterDeleteSelect);
5144
5823
  }
5145
5824
  }
5146
5825
  }
@@ -5165,8 +5844,6 @@ var __spreadValues$9 = (a, b) => {
5165
5844
  const getClonedQueryData = (query) => {
5166
5845
  const cloned = __spreadValues$9({}, query);
5167
5846
  delete cloned[toSQLCacheKey];
5168
- if (cloned.parsers)
5169
- cloned.parsers = __spreadValues$9({}, cloned.parsers);
5170
5847
  cloneQuery(cloned);
5171
5848
  return cloned;
5172
5849
  };
@@ -5371,12 +6048,14 @@ class VirtualColumn extends ColumnType {
5371
6048
  }
5372
6049
  }
5373
6050
 
5374
- class UnknownColumn extends VirtualColumn {
6051
+ const _UnknownColumn = class extends VirtualColumn {
5375
6052
  constructor(schema) {
5376
6053
  super(schema, schema.unknown());
5377
6054
  }
5378
- }
5379
- RawSQL.prototype.result = { value: new UnknownColumn(defaultSchemaConfig) };
6055
+ };
6056
+ let UnknownColumn = _UnknownColumn;
6057
+ UnknownColumn.instance = new _UnknownColumn(defaultSchemaConfig);
6058
+ RawSQL.prototype.result = { value: UnknownColumn.instance };
5380
6059
 
5381
6060
  const makeColumnsByType = (schema) => {
5382
6061
  const t = makeColumnTypes(schema);
@@ -6425,7 +7104,7 @@ const insert = (self, {
6425
7104
  q.values = values;
6426
7105
  if (!q.kind)
6427
7106
  q.kind = kind;
6428
- const { select, returnType = "all" } = q;
7107
+ const { select, returnType } = q;
6429
7108
  if (!select) {
6430
7109
  if (returnType !== "void")
6431
7110
  q.returnType = "rowCount";
@@ -6435,7 +7114,7 @@ const insert = (self, {
6435
7114
  } else if (returnType === "value" || returnType === "valueOrThrow") {
6436
7115
  q.returnType = "pluck";
6437
7116
  }
6438
- } else if (returnType === "all") {
7117
+ } else if (!returnType || returnType === "all") {
6439
7118
  q.returnType = "from" in values ? values.from.q.returnType : "one";
6440
7119
  } else if (returnType === "pluck") {
6441
7120
  q.returnType = "valueOrThrow";
@@ -6454,7 +7133,7 @@ const getFromSelectColumns = (from, obj, many) => {
6454
7133
  if (typeof item === "string") {
6455
7134
  const index = item.indexOf(".");
6456
7135
  queryColumns.push(index === -1 ? item : item.slice(index + 1));
6457
- } else if ("selectAs" in item) {
7136
+ } else if (item && "selectAs" in item) {
6458
7137
  queryColumns.push(...Object.keys(item.selectAs));
6459
7138
  }
6460
7139
  });
@@ -6498,12 +7177,7 @@ const _queryCreateMany = (q, data) => {
6498
7177
  };
6499
7178
  const _queryInsertMany = (q, data) => {
6500
7179
  const ctx = createCtx();
6501
- let result = insert(
6502
- q,
6503
- handleManyData(q, data, ctx),
6504
- "object",
6505
- true
6506
- );
7180
+ let result = insert(q, handleManyData(q, data, ctx), "object", true);
6507
7181
  if (!data.length)
6508
7182
  result = result.none();
6509
7183
  return result;
@@ -7316,11 +7990,15 @@ class Having {
7316
7990
  }
7317
7991
 
7318
7992
  const before = (q, key, cb) => pushQueryValue(q, `before${key}`, cb);
7319
- const after = (q, key, select, cb, commit) => pushQueryArray(
7320
- pushQueryValue(q, `after${key}${commit ? "Commit" : ""}`, cb),
7321
- `after${key}Select`,
7322
- select
7323
- );
7993
+ const after = (q, key, select, cb, commit) => {
7994
+ var _a, _b, _c;
7995
+ pushQueryValue(q, `after${key}${commit ? "Commit" : ""}`, cb);
7996
+ const set = (_c = (_a = q.q)[_b = `after${key}Select`]) != null ? _c : _a[_b] = /* @__PURE__ */ new Set();
7997
+ for (const column of select) {
7998
+ set.add(column);
7999
+ }
8000
+ return q;
8001
+ };
7324
8002
  const _queryHookBeforeQuery = (q, cb) => {
7325
8003
  return pushQueryValue(q, "before", cb);
7326
8004
  };
@@ -8381,7 +9059,9 @@ const mergableObjects = {
8381
9059
  parsers: true,
8382
9060
  defaults: true,
8383
9061
  joinedShapes: true,
8384
- joinedParsers: true
9062
+ joinedParsers: true,
9063
+ joinedBatchParsers: true,
9064
+ selectedComputeds: true
8385
9065
  };
8386
9066
  class MergeQueryMethods {
8387
9067
  merge(q) {
@@ -8406,6 +9086,8 @@ class MergeQueryMethods {
8406
9086
  b: a[key].b,
8407
9087
  u: [...a[key].u, ...value.u]
8408
9088
  } : value;
9089
+ } else if (value instanceof Set) {
9090
+ a[key] = a[key] ? /* @__PURE__ */ new Set([...a[key], ...value]) : value;
8409
9091
  } else {
8410
9092
  a[key] = value;
8411
9093
  }
@@ -8600,7 +9282,10 @@ class WithMethods {
8600
9282
  }
8601
9283
  pushQueryValue(q, "with", { n: name, o: options, q: query });
8602
9284
  const shape = getShapeFromSelect(query, true);
8603
- return setQueryObjectValue(q, "withShapes", name, shape);
9285
+ return setQueryObjectValue(q, "withShapes", name, {
9286
+ shape,
9287
+ computeds: query.q.computeds
9288
+ });
8604
9289
  }
8605
9290
  withRecursive(name, ...args) {
8606
9291
  var _a, _b, _c;
@@ -8609,10 +9294,9 @@ class WithMethods {
8609
9294
  const arg = q.queryBuilder.clone();
8610
9295
  arg.q.withShapes = q.q.withShapes;
8611
9296
  let query = typeof baseFn === "function" ? baseFn(arg) : baseFn;
8612
- const shape = ((_b = (_a = arg.q).withShapes) != null ? _b : _a.withShapes = {})[name] = getShapeFromSelect(
8613
- query,
8614
- true
8615
- );
9297
+ const shape = getShapeFromSelect(query, true);
9298
+ const withConfig = { shape, computeds: query.q.computeds };
9299
+ ((_b = (_a = arg.q).withShapes) != null ? _b : _a.withShapes = {})[name] = withConfig;
8616
9300
  const recursive = recursiveFn(arg);
8617
9301
  query = _queryUnion(query, [recursive], (_c = options.union) != null ? _c : "UNION ALL");
8618
9302
  options.recursive = true;
@@ -8622,7 +9306,7 @@ class WithMethods {
8622
9306
  });
8623
9307
  }
8624
9308
  pushQueryValue(q, "with", { n: name, o: options, q: query });
8625
- return setQueryObjectValue(q, "withShapes", name, shape);
9309
+ return setQueryObjectValue(q, "withShapes", name, withConfig);
8626
9310
  }
8627
9311
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8628
9312
  withSql(name, ...args) {
@@ -8633,12 +9317,9 @@ class WithMethods {
8633
9317
  o: options,
8634
9318
  s: sql(q)
8635
9319
  });
8636
- return setQueryObjectValue(
8637
- q,
8638
- "withShapes",
8639
- name,
8640
- shape(this.columnTypes)
8641
- );
9320
+ return setQueryObjectValue(q, "withShapes", name, {
9321
+ shape: shape(this.columnTypes)
9322
+ });
8642
9323
  }
8643
9324
  }
8644
9325
 
@@ -10156,12 +10837,9 @@ function orCreate(q, data, updateData, mergeData) {
10156
10837
  if (mergeData)
10157
10838
  data = __spreadValues$1(__spreadValues$1({}, mergeData), data);
10158
10839
  const inner = q2.create(data);
10159
- const { handleResult: handleResult2 } = inner.q;
10160
10840
  inner.q.handleResult = (q3, t, r, s) => {
10161
- queryResult = r;
10162
- const res = handleResult2(q3, t, r, s);
10163
- result = res;
10164
- return res;
10841
+ result = handleResult(q3, t, r, s);
10842
+ return inner.q.hookSelect ? result.map((row) => __spreadValues$1({}, row)) : result;
10165
10843
  };
10166
10844
  await inner;
10167
10845
  created = true;
@@ -10809,8 +11487,13 @@ class QueryMethods {
10809
11487
  pluck(select) {
10810
11488
  const q = this.clone();
10811
11489
  q.q.returnType = "pluck";
10812
- q.q.select = [select];
10813
- addParserForSelectItem(q, q.q.as || q.table, "pluck", select);
11490
+ const selected = addParserForSelectItem(
11491
+ q,
11492
+ q.q.as || q.table,
11493
+ "pluck",
11494
+ select
11495
+ );
11496
+ q.q.select = selected ? [selected] : void 0;
10814
11497
  return q;
10815
11498
  }
10816
11499
  /**
@@ -11562,6 +12245,7 @@ class Db {
11562
12245
  tableData
11563
12246
  };
11564
12247
  this.baseQuery = this;
12248
+ this.relations = {};
11565
12249
  const logger = options.logger || console;
11566
12250
  const parsers = {};
11567
12251
  let hasParsers = false;
@@ -11612,6 +12296,7 @@ class Db {
11612
12296
  );
11613
12297
  }
11614
12298
  this.internal.columnsForSelectAll = list;
12299
+ this.internal.columnsKeysForSelectAll = __spreadValues({}, shape);
11615
12300
  }
11616
12301
  this.q = {
11617
12302
  adapter,
@@ -11650,6 +12335,8 @@ class Db {
11650
12335
  this.defaultSelectColumns = columns.filter(
11651
12336
  (column) => !shape[column].data.isHidden
11652
12337
  );
12338
+ if (options.computed)
12339
+ applyComputedColumns(this, options.computed);
11653
12340
  const defaultSelect = this.defaultSelectColumns.length === columns.length ? void 0 : this.defaultSelectColumns;
11654
12341
  this.toSQL = defaultSelect ? function(options2) {
11655
12342
  const q = this.clone();
@@ -11658,7 +12345,6 @@ class Db {
11658
12345
  }
11659
12346
  return toSQL.call(q, options2);
11660
12347
  } : toSQL;
11661
- this.relations = {};
11662
12348
  if (modifyQuery) {
11663
12349
  for (const cb of modifyQuery) {
11664
12350
  cb(this);
@@ -11956,16 +12642,6 @@ const testTransaction = {
11956
12642
  }
11957
12643
  };
11958
12644
 
11959
- function addComputedColumns(q, computed) {
11960
- const { shape } = q;
11961
- for (const key in computed) {
11962
- const expr = computed[key](q);
11963
- shape[key] = expr.result.value;
11964
- expr.result.value.data.computed = expr;
11965
- }
11966
- return q;
11967
- }
11968
-
11969
12645
  const rowToColumnInfo = (row) => {
11970
12646
  const typed = row;
11971
12647
  return {
@@ -12022,6 +12698,7 @@ exports.CitextColumn = CitextColumn;
12022
12698
  exports.Clear = Clear;
12023
12699
  exports.ColumnRefExpression = ColumnRefExpression;
12024
12700
  exports.ColumnType = ColumnType;
12701
+ exports.ComputedColumn = ComputedColumn;
12025
12702
  exports.Create = Create;
12026
12703
  exports.CustomTypeColumn = CustomTypeColumn;
12027
12704
  exports.DateBaseColumn = DateBaseColumn;
@@ -12162,11 +12839,11 @@ exports._queryWhereIn = _queryWhereIn;
12162
12839
  exports._queryWhereNot = _queryWhereNot;
12163
12840
  exports._queryWhereNotSql = _queryWhereNotSql;
12164
12841
  exports._queryWhereSql = _queryWhereSql;
12165
- exports.addComputedColumns = addComputedColumns;
12166
12842
  exports.addParserForRawExpression = addParserForRawExpression;
12167
12843
  exports.addParserForSelectItem = addParserForSelectItem;
12168
12844
  exports.addQueryOn = addQueryOn;
12169
12845
  exports.anyShape = anyShape;
12846
+ exports.applyComputedColumns = applyComputedColumns;
12170
12847
  exports.checkIfASimpleQuery = checkIfASimpleQuery;
12171
12848
  exports.cloneQuery = cloneQuery;
12172
12849
  exports.cloneQueryBaseUnscoped = cloneQueryBaseUnscoped;
@@ -12183,6 +12860,7 @@ exports.countSelect = countSelect;
12183
12860
  exports.createDb = createDb;
12184
12861
  exports.defaultSchemaConfig = defaultSchemaConfig;
12185
12862
  exports.extendQuery = extendQuery;
12863
+ exports.filterResult = filterResult;
12186
12864
  exports.foreignKeyArgumentToCode = foreignKeyArgumentToCode;
12187
12865
  exports.getClonedQueryData = getClonedQueryData;
12188
12866
  exports.getColumnInfo = getColumnInfo;
@@ -12208,10 +12886,11 @@ exports.makeFnExpression = makeFnExpression;
12208
12886
  exports.makeRegexToFindInSql = makeRegexToFindInSql;
12209
12887
  exports.makeSQL = makeSQL;
12210
12888
  exports.parseRecord = parseRecord;
12211
- exports.parseResult = parseResult;
12212
12889
  exports.parseTableData = parseTableData;
12213
12890
  exports.parseTableDataInput = parseTableDataInput;
12214
12891
  exports.primaryKeyInnerToCode = primaryKeyInnerToCode;
12892
+ exports.processComputedBatches = processComputedBatches;
12893
+ exports.processComputedResult = processComputedResult;
12215
12894
  exports.processSelectArg = processSelectArg;
12216
12895
  exports.pushLimitSQL = pushLimitSQL;
12217
12896
  exports.pushQueryArray = pushQueryArray;