pqb 0.34.0 → 0.35.0

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,21 +119,21 @@ const sqlFn = (...args) => {
119
119
  return (...args2) => new RawSQL(args2, arg);
120
120
  };
121
121
 
122
- var __defProp$h = Object.defineProperty;
122
+ var __defProp$i = Object.defineProperty;
123
123
  var __defProps$9 = Object.defineProperties;
124
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) => {
125
+ var __getOwnPropSymbols$j = Object.getOwnPropertySymbols;
126
+ var __hasOwnProp$j = Object.prototype.hasOwnProperty;
127
+ var __propIsEnum$j = Object.prototype.propertyIsEnumerable;
128
+ var __defNormalProp$i = (obj, key, value) => key in obj ? __defProp$i(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
129
+ var __spreadValues$i = (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$j.call(b, prop))
132
+ __defNormalProp$i(a, prop, b[prop]);
133
+ if (__getOwnPropSymbols$j)
134
+ for (var prop of __getOwnPropSymbols$j(b)) {
135
+ if (__propIsEnum$j.call(b, prop))
136
+ __defNormalProp$i(a, prop, b[prop]);
137
137
  }
138
138
  return a;
139
139
  };
@@ -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$i(__spreadValues$i({}, 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$9(__spreadValues$i({}, 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,21 +384,21 @@ class ColumnType extends orchidCore.ColumnTypeBase {
384
384
  }
385
385
  }
386
386
 
387
- var __defProp$g = Object.defineProperty;
387
+ var __defProp$h = Object.defineProperty;
388
388
  var __defProps$8 = Object.defineProperties;
389
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) => {
390
+ var __getOwnPropSymbols$i = Object.getOwnPropertySymbols;
391
+ var __hasOwnProp$i = Object.prototype.hasOwnProperty;
392
+ var __propIsEnum$i = Object.prototype.propertyIsEnumerable;
393
+ var __defNormalProp$h = (obj, key, value) => key in obj ? __defProp$h(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
394
+ var __spreadValues$h = (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$i.call(b, prop))
397
+ __defNormalProp$h(a, prop, b[prop]);
398
+ if (__getOwnPropSymbols$i)
399
+ for (var prop of __getOwnPropSymbols$i(b)) {
400
+ if (__propIsEnum$i.call(b, prop))
401
+ __defNormalProp$h(a, prop, b[prop]);
402
402
  }
403
403
  return a;
404
404
  };
@@ -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$8(__spreadValues$h({}, 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,21 +842,21 @@ 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;
845
+ var __defProp$g = Object.defineProperty;
846
846
  var __defProps$7 = Object.defineProperties;
847
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) => {
848
+ var __getOwnPropSymbols$h = Object.getOwnPropertySymbols;
849
+ var __hasOwnProp$h = Object.prototype.hasOwnProperty;
850
+ var __propIsEnum$h = Object.prototype.propertyIsEnumerable;
851
+ var __defNormalProp$g = (obj, key, value) => key in obj ? __defProp$g(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
852
+ var __spreadValues$g = (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$h.call(b, prop))
855
+ __defNormalProp$g(a, prop, b[prop]);
856
+ if (__getOwnPropSymbols$h)
857
+ for (var prop of __getOwnPropSymbols$h(b)) {
858
+ if (__propIsEnum$h.call(b, prop))
859
+ __defNormalProp$g(a, prop, b[prop]);
860
860
  }
861
861
  return a;
862
862
  };
@@ -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$7(__spreadValues$g({}, 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$7(__spreadValues$g({}, 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$7(__spreadValues$g({}, 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$7(__spreadValues$g({}, base), {
988
988
  jsonPathQueryFirst: Object.assign(
989
989
  function(path, options) {
990
990
  var _a, _b, _c, _d, _e;
@@ -1211,21 +1211,21 @@ class BigSerialColumn extends NumberAsStringBaseColumn {
1211
1211
  }
1212
1212
  }
1213
1213
 
1214
- var __defProp$e = Object.defineProperty;
1214
+ var __defProp$f = Object.defineProperty;
1215
1215
  var __defProps$6 = Object.defineProperties;
1216
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) => {
1217
+ var __getOwnPropSymbols$g = Object.getOwnPropertySymbols;
1218
+ var __hasOwnProp$g = Object.prototype.hasOwnProperty;
1219
+ var __propIsEnum$g = Object.prototype.propertyIsEnumerable;
1220
+ var __defNormalProp$f = (obj, key, value) => key in obj ? __defProp$f(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1221
+ var __spreadValues$f = (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$g.call(b, prop))
1224
+ __defNormalProp$f(a, prop, b[prop]);
1225
+ if (__getOwnPropSymbols$g)
1226
+ for (var prop of __getOwnPropSymbols$g(b)) {
1227
+ if (__propIsEnum$g.call(b, prop))
1228
+ __defNormalProp$f(a, prop, b[prop]);
1229
1229
  }
1230
1230
  return a;
1231
1231
  };
@@ -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$f({}, 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$6(__spreadValues$f({}, 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,21 +2319,21 @@ 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;
2322
+ var __defProp$e = Object.defineProperty;
2318
2323
  var __defProps$5 = Object.defineProperties;
2319
2324
  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) => {
2325
+ var __getOwnPropSymbols$f = Object.getOwnPropertySymbols;
2326
+ var __hasOwnProp$f = Object.prototype.hasOwnProperty;
2327
+ var __propIsEnum$f = Object.prototype.propertyIsEnumerable;
2328
+ var __defNormalProp$e = (obj, key, value) => key in obj ? __defProp$e(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2329
+ var __spreadValues$e = (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$f.call(b, prop))
2332
+ __defNormalProp$e(a, prop, b[prop]);
2333
+ if (__getOwnPropSymbols$f)
2334
+ for (var prop of __getOwnPropSymbols$f(b)) {
2335
+ if (__propIsEnum$f.call(b, prop))
2336
+ __defNormalProp$e(a, prop, b[prop]);
2332
2337
  }
2333
2338
  return a;
2334
2339
  };
@@ -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$5(__spreadValues$e({}, q.q), {
2403
+ joinedShapes: __spreadProps$5(__spreadValues$e(__spreadValues$e({}, query.joinedShapes), q.q.joinedShapes), {
2399
2404
  [table.q.as || table.table]: table.q.shape
2400
2405
  })
2401
2406
  }),
@@ -2542,21 +2547,21 @@ const getIsJoinSubQuery = (query) => {
2542
2547
  return false;
2543
2548
  };
2544
2549
 
2545
- var __defProp$c = Object.defineProperty;
2550
+ var __defProp$d = Object.defineProperty;
2546
2551
  var __defProps$4 = Object.defineProperties;
2547
2552
  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) => {
2553
+ var __getOwnPropSymbols$e = Object.getOwnPropertySymbols;
2554
+ var __hasOwnProp$e = Object.prototype.hasOwnProperty;
2555
+ var __propIsEnum$e = Object.prototype.propertyIsEnumerable;
2556
+ var __defNormalProp$d = (obj, key, value) => key in obj ? __defProp$d(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2557
+ var __spreadValues$d = (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$e.call(b, prop))
2560
+ __defNormalProp$d(a, prop, b[prop]);
2561
+ if (__getOwnPropSymbols$e)
2562
+ for (var prop of __getOwnPropSymbols$e(b)) {
2563
+ if (__propIsEnum$e.call(b, prop))
2564
+ __defNormalProp$d(a, prop, b[prop]);
2560
2565
  }
2561
2566
  return a;
2562
2567
  };
@@ -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$4(__spreadValues$d({}, 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$d(__spreadValues$d({}, 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$d(__spreadValues$d({}, q.q.scopes), query.scopes);
2625
2631
  }
2626
2632
  }
2627
- const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2633
+ const joinedShapes = __spreadProps$4(__spreadValues$d({}, 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$d(__spreadValues$d({}, 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;
2687
+ var __defProp$c = Object.defineProperty;
2682
2688
  var __defProps$3 = Object.defineProperties;
2683
2689
  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) => {
2690
+ var __getOwnPropSymbols$d = Object.getOwnPropertySymbols;
2691
+ var __hasOwnProp$d = Object.prototype.hasOwnProperty;
2692
+ var __propIsEnum$d = Object.prototype.propertyIsEnumerable;
2693
+ var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2694
+ var __spreadValues$c = (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$d.call(b, prop))
2697
+ __defNormalProp$c(a, prop, b[prop]);
2698
+ if (__getOwnPropSymbols$d)
2699
+ for (var prop of __getOwnPropSymbols$d(b)) {
2700
+ if (__propIsEnum$d.call(b, prop))
2701
+ __defNormalProp$c(a, prop, b[prop]);
2696
2702
  }
2697
2703
  return a;
2698
2704
  };
2699
2705
  var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(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$c({}, 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$3(__spreadValues$c({}, 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 {
@@ -3121,7 +3168,201 @@ class Transaction {
3121
3168
  }
3122
3169
  }
3123
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
+ class ComputedColumn {
3224
+ constructor(kind, deps, fn) {
3225
+ this.kind = kind;
3226
+ this.deps = deps;
3227
+ this.fn = fn;
3228
+ }
3229
+ }
3230
+ const computeAtRuntime = (deps, fn) => new ComputedColumn("one", deps, fn);
3231
+ const computeBatchAtRuntime = (deps, fn) => new ComputedColumn("many", deps, fn);
3232
+ const applyComputedColumns = (q, fn) => {
3233
+ var _a, _b;
3234
+ q.computeAtRuntime = computeAtRuntime;
3235
+ q.computeBatchAtRuntime = computeBatchAtRuntime;
3236
+ const computed = fn(q);
3237
+ for (const key in computed) {
3238
+ const item = computed[key];
3239
+ if (item instanceof ComputedColumn) {
3240
+ ((_b = (_a = q.q).computeds) != null ? _b : _a.computeds = {})[key] = item;
3241
+ } else {
3242
+ (q.shape[key] = item.result.value || UnknownColumn.instance).data.computed = item;
3243
+ }
3244
+ }
3245
+ q.computeAtRuntime = q.computeBatchAtRuntime = void 0;
3246
+ };
3247
+ const processComputedResult = (query, result) => {
3248
+ let promises;
3249
+ for (const key in query.selectedComputeds) {
3250
+ const computed = query.selectedComputeds[key];
3251
+ if (computed.kind === "one") {
3252
+ for (const record of result) {
3253
+ record[key] = computed.fn(record);
3254
+ }
3255
+ } else {
3256
+ const res = computed.fn(result);
3257
+ if (Array.isArray(res)) {
3258
+ saveBatchComputed(key, result, res);
3259
+ } else {
3260
+ (promises != null ? promises : promises = []).push(
3261
+ res.then(
3262
+ (res2) => saveBatchComputed(key, result, res2)
3263
+ )
3264
+ );
3265
+ }
3266
+ }
3267
+ }
3268
+ if (!promises)
3269
+ return;
3270
+ return Promise.all(promises);
3271
+ };
3272
+ const processComputedBatches = (query, batches, originalReturnType, returnType, tempColumns, renames, key) => {
3273
+ let promises;
3274
+ for (const key2 in query.selectedComputeds) {
3275
+ const computed = query.selectedComputeds[key2];
3276
+ if (computed.kind === "one") {
3277
+ for (const { data } of batches) {
3278
+ for (const record of data) {
3279
+ if (record) {
3280
+ record[key2] = computed.fn(record);
3281
+ }
3282
+ }
3283
+ }
3284
+ } else {
3285
+ for (const { data } of batches) {
3286
+ let present;
3287
+ let blanks;
3288
+ if (!returnType || returnType === "all") {
3289
+ present = data;
3290
+ } else {
3291
+ present = [];
3292
+ blanks = /* @__PURE__ */ new Set();
3293
+ for (let i = 0; i < data.length; i++) {
3294
+ if (data[i]) {
3295
+ present.push(data[i]);
3296
+ } else {
3297
+ blanks.add(i);
3298
+ }
3299
+ }
3300
+ }
3301
+ const res = computed.fn(present);
3302
+ if (Array.isArray(res)) {
3303
+ saveBatchComputed(key2, data, res, blanks);
3304
+ } else {
3305
+ (promises != null ? promises : promises = []).push(
3306
+ res.then(
3307
+ (res2) => saveBatchComputed(key2, data, res2, blanks)
3308
+ )
3309
+ );
3310
+ }
3311
+ }
3312
+ }
3313
+ }
3314
+ if (!promises)
3315
+ return;
3316
+ return Promise.all(promises).then(() => {
3317
+ finalizeNestedHookSelect(
3318
+ batches,
3319
+ originalReturnType,
3320
+ tempColumns,
3321
+ renames,
3322
+ key
3323
+ );
3324
+ applyBatchTransforms(query, batches);
3325
+ });
3326
+ };
3327
+ const saveBatchComputed = (key, result, res, blanks) => {
3328
+ const len = result.length;
3329
+ const actual = res.length + ((blanks == null ? void 0 : blanks.size) || 0);
3330
+ if (len !== actual) {
3331
+ throw new Error(
3332
+ `Incorrect length of batch computed result for column ${key}. Expected ${len}, received ${actual}.`
3333
+ );
3334
+ }
3335
+ if (blanks) {
3336
+ for (let i = 0, r = 0; i < len; i++) {
3337
+ if (!blanks.has(i)) {
3338
+ result[i][key] = res[r++];
3339
+ }
3340
+ }
3341
+ } else {
3342
+ for (let i = 0; i < len; i++) {
3343
+ result[i][key] = res[i];
3344
+ }
3345
+ }
3346
+ };
3347
+
3348
+ var __defProp$b = Object.defineProperty;
3349
+ var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
3350
+ var __hasOwnProp$c = Object.prototype.hasOwnProperty;
3351
+ var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
3352
+ var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3353
+ var __spreadValues$b = (a, b) => {
3354
+ for (var prop in b || (b = {}))
3355
+ if (__hasOwnProp$c.call(b, prop))
3356
+ __defNormalProp$b(a, prop, b[prop]);
3357
+ if (__getOwnPropSymbols$c)
3358
+ for (var prop of __getOwnPropSymbols$c(b)) {
3359
+ if (__propIsEnum$c.call(b, prop))
3360
+ __defNormalProp$b(a, prop, b[prop]);
3361
+ }
3362
+ return a;
3363
+ };
3124
3364
  const queryMethodByReturnType = {
3365
+ undefined: "query",
3125
3366
  all: "query",
3126
3367
  rows: "arrays",
3127
3368
  pluck: "arrays",
@@ -3169,9 +3410,6 @@ Object.defineProperty(Then.prototype, "then", {
3169
3410
  });
3170
3411
  }
3171
3412
  });
3172
- const handleResult = (q, returnType, result, isSubQuery) => {
3173
- return parseResult(q, q.q.parsers, returnType, result, isSubQuery);
3174
- };
3175
3413
  function maybeWrappedThen(resolve, reject) {
3176
3414
  const { q } = this;
3177
3415
  let beforeHooks;
@@ -3229,7 +3467,7 @@ const callAfterHook = function(cb) {
3229
3467
  };
3230
3468
  const beginSql = { text: "BEGIN" };
3231
3469
  const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks, resolve, reject) => {
3232
- var _a;
3470
+ var _a, _b;
3233
3471
  const { q: query } = q;
3234
3472
  let sql;
3235
3473
  let logData;
@@ -3246,7 +3484,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3246
3484
  sql = q.toSQL();
3247
3485
  const { hookSelect } = sql;
3248
3486
  const { returnType = "all" } = query;
3249
- const returns = hookSelect ? "all" : returnType;
3487
+ const tempReturnType = hookSelect || returnType === "rows" && q.q.batchParsers ? "all" : returnType;
3250
3488
  let result;
3251
3489
  let queryResult;
3252
3490
  if ("text" in sql) {
@@ -3256,7 +3494,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3256
3494
  if (query.log) {
3257
3495
  logData = query.log.beforeQuery(sql);
3258
3496
  }
3259
- queryResult = await adapter[hookSelect ? "query" : queryMethodByReturnType[returnType]](sql);
3497
+ queryResult = await adapter[queryMethodByReturnType[tempReturnType]](sql);
3260
3498
  if (query.patchResult) {
3261
3499
  await query.patchResult(q, queryResult);
3262
3500
  }
@@ -3264,9 +3502,9 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3264
3502
  query.log.afterQuery(sql, logData);
3265
3503
  sql = void 0;
3266
3504
  }
3267
- result = query.handleResult(q, returns, queryResult);
3505
+ result = query.handleResult(q, tempReturnType, queryResult);
3268
3506
  } else {
3269
- const queryMethod = hookSelect ? "query" : queryMethodByReturnType[returnType];
3507
+ const queryMethod = queryMethodByReturnType[tempReturnType];
3270
3508
  if (!trx) {
3271
3509
  if (query.log)
3272
3510
  logData = query.log.beforeQuery(beginSql);
@@ -3301,9 +3539,37 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3301
3539
  if (query.patchResult) {
3302
3540
  await query.patchResult(q, queryResult);
3303
3541
  }
3304
- result = query.handleResult(q, returns, queryResult);
3542
+ result = query.handleResult(q, tempReturnType, queryResult);
3305
3543
  }
3306
- if (afterHooks || afterCommitHooks || query.after) {
3544
+ if (result && typeof result === "object" && typeof result.then === "function") {
3545
+ result = await result;
3546
+ }
3547
+ let tempColumns;
3548
+ let renames;
3549
+ if (hookSelect) {
3550
+ for (const column of hookSelect.keys()) {
3551
+ const as = hookSelect.get(column).as;
3552
+ if (as)
3553
+ (renames != null ? renames : renames = {})[column] = as;
3554
+ (_a = tempColumns != null ? tempColumns : tempColumns = /* @__PURE__ */ new Set()) == null ? void 0 : _a.add(as || column);
3555
+ }
3556
+ if (renames) {
3557
+ for (const record of result) {
3558
+ for (const a in renames) {
3559
+ const value = record[renames[a]];
3560
+ record[renames[a]] = record[a];
3561
+ record[a] = value;
3562
+ }
3563
+ }
3564
+ }
3565
+ if (query.selectedComputeds) {
3566
+ const promise = processComputedResult(query, result);
3567
+ if (promise)
3568
+ await promise;
3569
+ }
3570
+ }
3571
+ const hasAfterHook = afterHooks || afterCommitHooks || query.after;
3572
+ if (hasAfterHook) {
3307
3573
  if (queryResult.rowCount) {
3308
3574
  if (afterHooks || query.after) {
3309
3575
  const args = [result, q];
@@ -3315,7 +3581,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3315
3581
  );
3316
3582
  }
3317
3583
  if (afterCommitHooks && trx) {
3318
- ((_a = trx.afterCommit) != null ? _a : trx.afterCommit = []).push(
3584
+ ((_b = trx.afterCommit) != null ? _b : trx.afterCommit = []).push(
3319
3585
  result,
3320
3586
  q,
3321
3587
  afterCommitHooks
@@ -3328,8 +3594,24 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3328
3594
  const args = [result, q];
3329
3595
  await Promise.all(query.after.map(callAfterHook, args));
3330
3596
  }
3331
- if (hookSelect)
3332
- result = filterResult(q, returnType, queryResult, hookSelect, result);
3597
+ }
3598
+ if (hookSelect || tempReturnType !== returnType) {
3599
+ if (renames) {
3600
+ for (const record of result) {
3601
+ for (const a in renames) {
3602
+ record[a] = record[renames[a]];
3603
+ }
3604
+ }
3605
+ }
3606
+ result = filterResult(
3607
+ q,
3608
+ returnType,
3609
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3610
+ queryResult,
3611
+ result,
3612
+ tempColumns,
3613
+ hasAfterHook
3614
+ );
3333
3615
  }
3334
3616
  if (query.transform) {
3335
3617
  result = orchidCore.applyTransforms(returnType, query.transform, result);
@@ -3375,57 +3657,91 @@ const assignError = (to, from) => {
3375
3657
  to.routine = from.routine;
3376
3658
  return to;
3377
3659
  };
3378
- const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
3660
+ const handleResult = (q, returnType, result, isSubQuery) => {
3379
3661
  var _a, _b;
3662
+ const { parsers } = q.q;
3380
3663
  switch (returnType) {
3381
3664
  case "all": {
3382
3665
  if (q.q.throwOnNotFound && result.rows.length === 0)
3383
3666
  throw new NotFoundError(q);
3667
+ const promise = parseBatch(q, result);
3384
3668
  const { rows } = result;
3385
3669
  if (parsers) {
3386
3670
  for (const row of rows) {
3387
3671
  parseRecord(parsers, row);
3388
3672
  }
3389
3673
  }
3390
- return rows;
3674
+ return promise ? promise.then(() => rows) : rows;
3391
3675
  }
3392
3676
  case "one": {
3393
- const row = result.rows[0];
3394
- if (!row)
3677
+ const { rows } = result;
3678
+ if (!rows.length)
3395
3679
  return;
3396
- return parsers ? parseRecord(parsers, row) : row;
3680
+ const promise = parseBatch(q, result);
3681
+ if (parsers)
3682
+ parseRecord(parsers, rows[0]);
3683
+ return promise ? promise.then(() => rows[0]) : rows[0];
3397
3684
  }
3398
3685
  case "oneOrThrow": {
3399
- const row = result.rows[0];
3400
- if (!row)
3686
+ const { rows } = result;
3687
+ if (!rows.length)
3401
3688
  throw new NotFoundError(q);
3402
- return parsers ? parseRecord(parsers, row) : row;
3689
+ const promise = parseBatch(q, result);
3690
+ if (parsers)
3691
+ parseRecord(parsers, rows[0]);
3692
+ return promise ? promise.then(() => rows[0]) : rows[0];
3403
3693
  }
3404
3694
  case "rows": {
3405
- return parsers ? parseRows(
3406
- parsers,
3407
- result.fields,
3408
- result.rows
3409
- ) : result.rows;
3695
+ const { rows } = result;
3696
+ const promise = parseBatch(q, result);
3697
+ if (promise) {
3698
+ return promise.then(() => {
3699
+ if (parsers)
3700
+ parseRows(parsers, result.fields, rows);
3701
+ return rows;
3702
+ });
3703
+ } else if (parsers) {
3704
+ parseRows(parsers, result.fields, rows);
3705
+ }
3706
+ return rows;
3410
3707
  }
3411
3708
  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;
3709
+ const { rows } = result;
3710
+ const promise = parseBatch(q, result);
3711
+ if (promise) {
3712
+ return promise.then(() => {
3713
+ parsePluck(parsers, isSubQuery, rows);
3714
+ return rows;
3715
+ });
3417
3716
  }
3418
- return result.rows.map((row) => row[0]);
3717
+ parsePluck(parsers, isSubQuery, rows);
3718
+ return rows;
3419
3719
  }
3420
3720
  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;
3721
+ const { rows } = result;
3722
+ const promise = parseBatch(q, result);
3723
+ if (promise) {
3724
+ return promise.then(() => {
3725
+ var _a2;
3726
+ return ((_a2 = rows[0]) == null ? void 0 : _a2[0]) !== void 0 ? parseValue(rows[0][0], parsers) : q.q.notFoundDefault;
3727
+ });
3728
+ }
3729
+ return ((_a = rows[0]) == null ? void 0 : _a[0]) !== void 0 ? parseValue(rows[0][0], parsers) : q.q.notFoundDefault;
3423
3730
  }
3424
3731
  case "valueOrThrow": {
3425
- const value = (_b = result.rows[0]) == null ? void 0 : _b[0];
3426
- if (value === void 0)
3732
+ const { rows } = result;
3733
+ const promise = parseBatch(q, result);
3734
+ if (promise) {
3735
+ return promise.then(() => {
3736
+ var _a2;
3737
+ if (((_a2 = rows[0]) == null ? void 0 : _a2[0]) === void 0)
3738
+ throw new NotFoundError(q);
3739
+ return parseValue(rows[0][0], parsers);
3740
+ });
3741
+ }
3742
+ if (((_b = rows[0]) == null ? void 0 : _b[0]) === void 0)
3427
3743
  throw new NotFoundError(q);
3428
- return parseValue(value, parsers);
3744
+ return parseValue(rows[0][0], parsers);
3429
3745
  }
3430
3746
  case "rowCount": {
3431
3747
  if (q.q.throwOnNotFound && result.rowCount === 0) {
@@ -3438,6 +3754,17 @@ const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
3438
3754
  }
3439
3755
  }
3440
3756
  };
3757
+ const parseBatch = (q, queryResult) => {
3758
+ let promises;
3759
+ if (q.q.batchParsers) {
3760
+ for (const parser of q.q.batchParsers) {
3761
+ const res = parser.fn(parser.path, queryResult);
3762
+ if (res)
3763
+ (promises != null ? promises : promises = []).push(res);
3764
+ }
3765
+ }
3766
+ return promises && Promise.all(promises);
3767
+ };
3441
3768
  const parseRecord = (parsers, row) => {
3442
3769
  for (const key in parsers) {
3443
3770
  if (key in row) {
@@ -3455,38 +3782,43 @@ const parseRows = (parsers, fields, rows) => {
3455
3782
  }
3456
3783
  }
3457
3784
  }
3458
- return rows;
3785
+ };
3786
+ const parsePluck = (parsers, isSubQuery, rows) => {
3787
+ const pluck = parsers == null ? void 0 : parsers.pluck;
3788
+ if (pluck) {
3789
+ for (let i = 0; i < rows.length; i++) {
3790
+ rows[i] = pluck(isSubQuery ? rows[i] : rows[i][0]);
3791
+ }
3792
+ } else if (!isSubQuery) {
3793
+ for (let i = 0; i < rows.length; i++) {
3794
+ rows[i] = rows[i][0];
3795
+ }
3796
+ }
3459
3797
  };
3460
3798
  const parseValue = (value, parsers) => {
3461
3799
  const parser = parsers == null ? void 0 : parsers[orchidCore.getValueKey];
3462
3800
  return parser ? parser(value) : value;
3463
3801
  };
3464
- const filterResult = (q, returnType, queryResult, hookSelect, result) => {
3802
+ const filterResult = (q, returnType, queryResult, result, tempColumns, hasAfterHook) => {
3465
3803
  var _a;
3466
3804
  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
- });
3805
+ return filterAllResult(result, tempColumns, hasAfterHook);
3475
3806
  }
3476
3807
  if (returnType === "oneOrThrow" || returnType === "one") {
3477
- const row = result[0];
3808
+ let row = result[0];
3478
3809
  if (!row) {
3479
3810
  if (returnType === "oneOrThrow")
3480
3811
  throw new NotFoundError(q);
3481
3812
  return void 0;
3813
+ } else if (!(tempColumns == null ? void 0 : tempColumns.size)) {
3814
+ return row;
3482
3815
  } else {
3483
- result = {};
3484
- for (const key in row) {
3485
- if (!hookSelect.includes(key)) {
3486
- result[key] = row[key];
3487
- }
3816
+ if (hasAfterHook)
3817
+ row = __spreadValues$b({}, row);
3818
+ for (const column of tempColumns) {
3819
+ delete row[column];
3488
3820
  }
3489
- return result;
3821
+ return row;
3490
3822
  }
3491
3823
  }
3492
3824
  if (returnType === "value") {
@@ -3506,20 +3838,30 @@ const filterResult = (q, returnType, queryResult, hookSelect, result) => {
3506
3838
  return result.map((row) => row[key]);
3507
3839
  }
3508
3840
  if (returnType === "rows") {
3509
- const pick = getSelectPick(queryResult, hookSelect);
3510
- return result.map(
3511
- (full) => pick.map((key) => full[key])
3512
- );
3841
+ result = filterAllResult(result, tempColumns, hasAfterHook);
3842
+ return result.map((record) => Object.values(record));
3513
3843
  }
3514
3844
  return;
3515
3845
  };
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);
3846
+ const filterAllResult = (result, tempColumns, hasAfterHook) => {
3847
+ if (tempColumns == null ? void 0 : tempColumns.size) {
3848
+ if (hasAfterHook) {
3849
+ return result.map((data) => {
3850
+ const record = __spreadValues$b({}, data);
3851
+ for (const key of tempColumns) {
3852
+ delete record[key];
3853
+ }
3854
+ return record;
3855
+ });
3856
+ } else {
3857
+ for (const record of result) {
3858
+ for (const key of tempColumns) {
3859
+ delete record[key];
3860
+ }
3861
+ }
3862
+ }
3521
3863
  }
3522
- return pick;
3864
+ return result;
3523
3865
  };
3524
3866
 
3525
3867
  const addParserForRawExpression = (q, key, raw) => {
@@ -3527,58 +3869,220 @@ const addParserForRawExpression = (q, key, raw) => {
3527
3869
  if (type == null ? void 0 : type.parseFn)
3528
3870
  orchidCore.setParserToQuery(q.q, key, type.parseFn);
3529
3871
  };
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
3872
  const addParsersForSelectJoined = (q, arg, as = arg) => {
3537
- var _a;
3873
+ var _a, _b, _c, _d;
3538
3874
  const parsers = (_a = q.q.joinedParsers) == null ? void 0 : _a[arg];
3539
3875
  if (parsers) {
3540
3876
  orchidCore.setParserToQuery(q.q, as, (row) => parseRecord(parsers, row));
3541
3877
  }
3878
+ const batchParsers = (_b = q.q.joinedBatchParsers) == null ? void 0 : _b[arg];
3879
+ if (batchParsers) {
3880
+ ((_d = (_c = q.q).batchParsers) != null ? _d : _c.batchParsers = []).push(
3881
+ ...batchParsers.map((x) => ({
3882
+ path: [as, ...x.path],
3883
+ fn: x.fn
3884
+ }))
3885
+ );
3886
+ }
3542
3887
  };
3543
3888
  const addParserForSelectItem = (q, as, key, arg) => {
3889
+ var _a, _b, _c, _d;
3544
3890
  if (typeof arg === "object" || typeof arg === "function") {
3545
3891
  if (orchidCore.isExpression(arg)) {
3546
3892
  addParserForRawExpression(q, key, arg);
3547
3893
  } else {
3548
3894
  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
- });
3895
+ if (query.batchParsers) {
3896
+ const batchParsers = (_b = (_a = q.q).batchParsers) != null ? _b : _a.batchParsers = [];
3897
+ for (const bp of query.batchParsers) {
3898
+ batchParsers.push({ path: [key, ...bp.path], fn: bp.fn });
3899
+ }
3556
3900
  }
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);
3901
+ if (query.hookSelect || query.parsers || query.transform) {
3902
+ const batchParsers = (_d = (_c = q.q).batchParsers) != null ? _d : _c.batchParsers = [];
3903
+ batchParsers.push({
3904
+ path: [key],
3905
+ fn: (path, queryResult) => {
3906
+ var _a2, _b2, _c2;
3907
+ const { rows } = queryResult;
3908
+ const originalReturnType = query.returnType || "all";
3909
+ let returnType = originalReturnType;
3910
+ const { hookSelect } = query;
3911
+ const batches = [];
3912
+ let last = path.length;
3913
+ if (returnType === "value" || returnType === "valueOrThrow") {
3914
+ if (hookSelect) {
3915
+ batches.push = (item) => {
3916
+ if (!(key in item)) {
3917
+ returnType = returnType === "value" ? "one" : "oneOrThrow";
3918
+ }
3919
+ batches.push = Array.prototype.push;
3920
+ return batches.push(item);
3921
+ };
3922
+ } else {
3923
+ last--;
3924
+ }
3925
+ }
3926
+ collectNestedSelectBatches(batches, rows, path, last);
3927
+ switch (returnType) {
3928
+ case "all": {
3929
+ const { parsers } = query;
3930
+ if (parsers) {
3931
+ for (const { data } of batches) {
3932
+ for (const one of data) {
3933
+ parseRecord(parsers, one);
3934
+ }
3935
+ }
3566
3936
  }
3937
+ break;
3567
3938
  }
3568
- } else {
3569
- if (data[key] === void 0) {
3570
- throw new NotFoundError(q);
3939
+ case "one":
3940
+ case "oneOrThrow": {
3941
+ const { parsers } = query;
3942
+ if (parsers) {
3943
+ if (returnType === "one") {
3944
+ for (const { data } of batches) {
3945
+ if (data)
3946
+ parseRecord(parsers, data);
3947
+ }
3948
+ } else {
3949
+ for (const { data } of batches) {
3950
+ if (!data)
3951
+ throw new NotFoundError(arg);
3952
+ parseRecord(parsers, data);
3953
+ }
3954
+ }
3955
+ } else if (returnType !== "one") {
3956
+ for (const { data } of batches) {
3957
+ if (!data)
3958
+ throw new NotFoundError(arg);
3959
+ }
3960
+ }
3961
+ if (hookSelect) {
3962
+ for (const batch of batches) {
3963
+ batch.data = [batch.data];
3964
+ }
3965
+ }
3966
+ break;
3967
+ }
3968
+ case "pluck": {
3969
+ const parse = (_a2 = query.parsers) == null ? void 0 : _a2.pluck;
3970
+ if (parse) {
3971
+ for (const { data } of batches) {
3972
+ for (let i = 0; i < data.length; i++) {
3973
+ data[i] = parse(data[i]);
3974
+ }
3975
+ }
3976
+ }
3977
+ break;
3978
+ }
3979
+ case "value":
3980
+ case "valueOrThrow": {
3981
+ const parse = (_b2 = query.parsers) == null ? void 0 : _b2[orchidCore.getValueKey];
3982
+ if (parse) {
3983
+ if (returnType === "value") {
3984
+ for (const { data } of batches) {
3985
+ data[key] = data[key] === void 0 ? arg.q.notFoundDefault : parse(data[key]);
3986
+ }
3987
+ } else {
3988
+ for (const { data } of batches) {
3989
+ if (data[key] === void 0)
3990
+ throw new NotFoundError(arg);
3991
+ data[key] = parse(data[key]);
3992
+ }
3993
+ }
3994
+ } else if (returnType !== "value") {
3995
+ for (const { data } of batches) {
3996
+ if (data[key] === void 0)
3997
+ throw new NotFoundError(arg);
3998
+ }
3999
+ }
4000
+ if (hookSelect) {
4001
+ for (const batch of batches) {
4002
+ batch.data = [batch.data];
4003
+ }
4004
+ }
4005
+ break;
3571
4006
  }
3572
4007
  }
3573
- return data;
4008
+ if (hookSelect) {
4009
+ let tempColumns;
4010
+ let renames;
4011
+ for (const column of hookSelect.keys()) {
4012
+ const as2 = hookSelect.get(column).as;
4013
+ if (as2)
4014
+ (renames != null ? renames : renames = {})[column] = as2;
4015
+ (_c2 = tempColumns != null ? tempColumns : tempColumns = /* @__PURE__ */ new Set()) == null ? void 0 : _c2.add(as2 || column);
4016
+ }
4017
+ if (renames) {
4018
+ for (const { data } of batches) {
4019
+ for (const record of data) {
4020
+ if (record) {
4021
+ for (const a in renames) {
4022
+ const value = record[renames[a]];
4023
+ record[renames[a]] = record[a];
4024
+ record[a] = value;
4025
+ }
4026
+ }
4027
+ }
4028
+ }
4029
+ }
4030
+ if (query.selectedComputeds) {
4031
+ const maybePromise = processComputedBatches(
4032
+ query,
4033
+ batches,
4034
+ originalReturnType,
4035
+ returnType,
4036
+ tempColumns,
4037
+ renames,
4038
+ key
4039
+ );
4040
+ if (maybePromise)
4041
+ return maybePromise;
4042
+ }
4043
+ finalizeNestedHookSelect(
4044
+ batches,
4045
+ originalReturnType,
4046
+ tempColumns,
4047
+ renames,
4048
+ key
4049
+ );
4050
+ }
4051
+ applyBatchTransforms(query, batches);
4052
+ return;
3574
4053
  }
3575
- );
4054
+ });
3576
4055
  }
3577
4056
  }
3578
4057
  return arg;
3579
4058
  }
3580
4059
  return setParserForSelectedString(q, arg, as, key);
3581
4060
  };
4061
+ const collectNestedSelectBatches = (batches, rows, path, last) => {
4062
+ const stack = rows.map(
4063
+ (row) => ({
4064
+ data: row,
4065
+ i: 0
4066
+ })
4067
+ );
4068
+ while (stack.length > 0) {
4069
+ const item = stack.pop();
4070
+ const { i } = item;
4071
+ if (i === last) {
4072
+ batches.push(item);
4073
+ continue;
4074
+ }
4075
+ const { data } = item;
4076
+ const key = path[i];
4077
+ if (Array.isArray(data)) {
4078
+ for (let key2 = 0; key2 < data.length; key2++) {
4079
+ stack.push({ data: data[key2], parent: data, key: key2, i });
4080
+ }
4081
+ } else if (data && typeof data === "object") {
4082
+ stack.push({ data: data[key], parent: data, key, i: i + 1 });
4083
+ }
4084
+ }
4085
+ };
3582
4086
  const emptyArrSQL = new RawSQL("'[]'");
3583
4087
  const processSelectArg = (q, as, arg, columnAs) => {
3584
4088
  var _a, _b, _c, _d;
@@ -3592,7 +4096,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
3592
4096
  value = resolveSubQueryCallback(q, value);
3593
4097
  if (isQueryNone(value)) {
3594
4098
  if (value.q.innerJoinLateral) {
3595
- return;
4099
+ return false;
3596
4100
  }
3597
4101
  } else if (!orchidCore.isExpression(value) && value.joinQuery) {
3598
4102
  value = value.joinQuery(value, q);
@@ -3602,17 +4106,23 @@ const processSelectArg = (q, as, arg, columnAs) => {
3602
4106
  query = value.json(false);
3603
4107
  value.q.coalesceValue = emptyArrSQL;
3604
4108
  } else if (returnType === "pluck") {
3605
- query = value.wrap(value.baseQuery.clone()).jsonAgg(value.q.select[0]);
4109
+ query = value.q.select ? value.wrap(value.baseQuery.clone()).jsonAgg(value.q.select[0]) : value.json(false);
3606
4110
  value.q.coalesceValue = emptyArrSQL;
3607
4111
  } 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
- };
4112
+ if (returnType === "value" || returnType === "valueOrThrow") {
4113
+ if (value.q.select) {
4114
+ if (typeof value.q.select[0] === "string") {
4115
+ value.q.select[0] = {
4116
+ selectAs: { r: value.q.select[0] }
4117
+ };
4118
+ }
4119
+ query = value;
4120
+ } else {
4121
+ query = value.json(false);
3613
4122
  }
4123
+ } else {
4124
+ query = value;
3614
4125
  }
3615
- query = value;
3616
4126
  }
3617
4127
  let asOverride = key;
3618
4128
  if ((_a = value.q.joinedShapes) == null ? void 0 : _a[key]) {
@@ -3646,7 +4156,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
3646
4156
  return { selectAs };
3647
4157
  };
3648
4158
  const setParserForSelectedString = (q, arg, as, columnAs) => {
3649
- var _a, _b, _c, _d;
4159
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
3650
4160
  const index = arg.indexOf(".");
3651
4161
  if (index !== -1) {
3652
4162
  const table = arg.slice(0, index);
@@ -3654,26 +4164,59 @@ const setParserForSelectedString = (q, arg, as, columnAs) => {
3654
4164
  if (column === "*") {
3655
4165
  addParsersForSelectJoined(q, table, columnAs);
3656
4166
  return table === as ? column : arg;
3657
- } else {
3658
- if (table === as) {
4167
+ } else if (table === as) {
4168
+ if (columnAs) {
3659
4169
  const parser = (_a = q.q.parsers) == null ? void 0 : _a[column];
3660
4170
  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;
4171
+ q.q.parsers[columnAs] = parser;
3668
4172
  }
4173
+ return handleComputed(q, q.q.computeds, column);
4174
+ } else {
4175
+ const parser = (_c = (_b = q.q.joinedParsers) == null ? void 0 : _b[table]) == null ? void 0 : _c[column];
4176
+ if (parser)
4177
+ orchidCore.setParserToQuery(q.q, columnAs || column, parser);
4178
+ const batchParsers = (_d = q.q.joinedBatchParsers) == null ? void 0 : _d[table];
4179
+ if (batchParsers) {
4180
+ for (const bp of batchParsers) {
4181
+ if (bp.path[0] === column) {
4182
+ ((_f = (_e = q.q).batchParsers) != null ? _f : _e.batchParsers = []).push(bp);
4183
+ }
4184
+ }
4185
+ }
4186
+ const computeds = (_g = q.q.joinedComputeds) == null ? void 0 : _g[table];
4187
+ if (computeds == null ? void 0 : computeds[column]) {
4188
+ const computed = computeds[column];
4189
+ const map = (_i = (_h = q.q).hookSelect) != null ? _i : _h.hookSelect = /* @__PURE__ */ new Map();
4190
+ for (const column2 of computed.deps) {
4191
+ map.set(column2, { select: `${table}.${column2}` });
4192
+ }
4193
+ ((_k = (_j = q.q).selectedComputeds) != null ? _k : _j.selectedComputeds = {})[column] = computed;
4194
+ return;
4195
+ }
4196
+ return arg;
3669
4197
  }
3670
4198
  } 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;
4199
+ if (columnAs) {
4200
+ const parser = (_l = q.q.parsers) == null ? void 0 : _l[arg];
4201
+ if (parser)
4202
+ q.q.parsers[columnAs] = parser;
4203
+ }
4204
+ return handleComputed(q, q.q.computeds, arg);
3675
4205
  }
3676
4206
  };
4207
+ const handleComputed = (q, computeds, column) => {
4208
+ var _a, _b, _c, _d;
4209
+ if (computeds == null ? void 0 : computeds[column]) {
4210
+ const computed = computeds[column];
4211
+ const map = (_b = (_a = q.q).hookSelect) != null ? _b : _a.hookSelect = /* @__PURE__ */ new Map();
4212
+ for (const column2 of computed.deps) {
4213
+ map.set(column2, { select: column2 });
4214
+ }
4215
+ ((_d = (_c = q.q).selectedComputeds) != null ? _d : _c.selectedComputeds = {})[column] = computed;
4216
+ return;
4217
+ }
4218
+ return column;
4219
+ };
3677
4220
  const getShapeFromSelect = (q, isSubQuery) => {
3678
4221
  const query = q.q;
3679
4222
  const { select, shape } = query;
@@ -3693,7 +4236,9 @@ const getShapeFromSelect = (q, isSubQuery) => {
3693
4236
  for (const item of select) {
3694
4237
  if (typeof item === "string") {
3695
4238
  addColumnToShapeFromSelect(q, item, shape, query, result, isSubQuery);
3696
- } else if ("selectAs" in item) {
4239
+ } else if (orchidCore.isExpression(item)) {
4240
+ result.value = item.result.value;
4241
+ } else if (item && "selectAs" in item) {
3697
4242
  for (const key in item.selectAs) {
3698
4243
  const it = item.selectAs[key];
3699
4244
  if (typeof it === "string") {
@@ -3708,7 +4253,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
3708
4253
  );
3709
4254
  } else if (orchidCore.isExpression(it)) {
3710
4255
  result[key] = it.result.value;
3711
- } else {
4256
+ } else if (it) {
3712
4257
  const { returnType } = it.q;
3713
4258
  if (returnType === "value" || returnType === "valueOrThrow") {
3714
4259
  const type = it.q[orchidCore.getValueKey];
@@ -3719,8 +4264,6 @@ const getShapeFromSelect = (q, isSubQuery) => {
3719
4264
  }
3720
4265
  }
3721
4266
  }
3722
- } else if (orchidCore.isExpression(item)) {
3723
- result.value = item.result.value;
3724
4267
  }
3725
4268
  }
3726
4269
  }
@@ -3756,12 +4299,13 @@ function _querySelect(q, args) {
3756
4299
  return q;
3757
4300
  }
3758
4301
  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]) {
4302
+ const selectArgs = [];
4303
+ for (const arg of args) {
4304
+ const item = processSelectArg(q, as, arg);
4305
+ if (item)
4306
+ selectArgs.push(item);
4307
+ else if (item === false)
3763
4308
  return _queryNone(q);
3764
- }
3765
4309
  }
3766
4310
  return pushQueryArray(q, "select", selectArgs);
3767
4311
  }
@@ -3831,20 +4375,25 @@ const _get = (query, returnType, arg) => {
3831
4375
  }
3832
4376
  }
3833
4377
  q[orchidCore.getValueKey] = type;
3834
- setParserForSelectedString(
4378
+ const selected = setParserForSelectedString(
3835
4379
  query,
3836
4380
  arg,
3837
4381
  getQueryAs(query),
3838
4382
  orchidCore.getValueKey
3839
4383
  );
3840
- q.expr = new SelectItemExpression(query, arg, type);
4384
+ q.select = selected ? [
4385
+ q.expr = new SelectItemExpression(
4386
+ query,
4387
+ selected,
4388
+ type
4389
+ )
4390
+ ] : void 0;
3841
4391
  } else {
3842
4392
  type = arg.result.value;
3843
4393
  q[orchidCore.getValueKey] = type;
3844
4394
  addParserForRawExpression(query, orchidCore.getValueKey, arg);
3845
- q.expr = arg;
4395
+ q.select = [q.expr = arg];
3846
4396
  }
3847
- q.select = [q.expr];
3848
4397
  return setQueryOperators(
3849
4398
  query,
3850
4399
  (type == null ? void 0 : type.operators) || Operators.any
@@ -3880,23 +4429,27 @@ class AsMethods {
3880
4429
  }
3881
4430
 
3882
4431
  function queryFrom(self, arg) {
3883
- var _a, _b;
4432
+ var _a, _b, _c, _d;
3884
4433
  const data = self.q;
3885
4434
  if (typeof arg === "string") {
3886
4435
  data.as || (data.as = arg);
3887
- data.shape = (_a = data.withShapes) == null ? void 0 : _a[arg];
4436
+ const w = (_a = data.withShapes) == null ? void 0 : _a[arg];
4437
+ data.shape = (_b = w == null ? void 0 : w.shape) != null ? _b : orchidCore.emptyObject;
4438
+ data.computeds = w == null ? void 0 : w.computeds;
3888
4439
  } else if (orchidCore.isExpression(arg)) {
3889
4440
  data.as || (data.as = "t");
3890
4441
  } else if (Array.isArray(arg)) {
3891
4442
  const { shape } = data;
3892
- const parsers = (_b = data.parsers) != null ? _b : data.parsers = {};
4443
+ const parsers = (_c = data.parsers) != null ? _c : data.parsers = {};
4444
+ const computeds = (_d = data.computeds) != null ? _d : data.computeds = {};
3893
4445
  for (const item of arg) {
3894
4446
  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;
4447
+ const w = data.withShapes[item];
4448
+ Object.assign(shape, w.shape);
4449
+ Object.assign(computeds, w.computeds);
4450
+ for (const key in w.shape) {
4451
+ if (w.shape[key].parseFn) {
4452
+ parsers[key] = w.shape[key].parseFn;
3900
4453
  }
3901
4454
  }
3902
4455
  } else if (!orchidCore.isExpression(item)) {
@@ -3909,6 +4462,7 @@ function queryFrom(self, arg) {
3909
4462
  data.as || (data.as = q.q.as || q.table || "t");
3910
4463
  data.shape = getShapeFromSelect(arg, true);
3911
4464
  data.parsers = q.q.parsers;
4465
+ data.batchParsers = q.q.batchParsers;
3912
4466
  }
3913
4467
  data.from = arg;
3914
4468
  return self;
@@ -4018,44 +4572,117 @@ function queryJson(self, coalesce) {
4018
4572
  const pushSelectSql = (ctx, table, query, quotedAs) => {
4019
4573
  ctx.sql.push(selectToSql(ctx, table, query, quotedAs));
4020
4574
  };
4021
- const selectToSql = (ctx, table, query, quotedAs) => {
4575
+ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect) => {
4576
+ var _a, _b;
4577
+ let selected;
4578
+ const list = [];
4022
4579
  if (query.select) {
4023
- const list = [];
4024
4580
  for (const item of query.select) {
4025
4581
  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);
4582
+ let sql;
4583
+ if (item === "*") {
4584
+ if (hookSelect) {
4585
+ selected != null ? selected : selected = {};
4586
+ for (const key in table.internal.columnsKeysForSelectAll || query.shape) {
4587
+ selected[key] = quotedAs;
4036
4588
  }
4037
- } else {
4038
- list.push(
4039
- `${columnToSql(
4040
- ctx,
4041
- table.q,
4042
- table.q.shape,
4043
- value,
4044
- quotedAs,
4045
- true
4046
- )} "${as}"`
4589
+ }
4590
+ sql = selectAllSql(table, query, quotedAs);
4591
+ } else {
4592
+ const index = item.indexOf(".");
4593
+ if (index !== -1) {
4594
+ const tableName = item.slice(0, index);
4595
+ const key = item.slice(index + 1);
4596
+ if (hookSelect == null ? void 0 : hookSelect.get(key))
4597
+ (selected != null ? selected : selected = {})[key] = `"${tableName}"`;
4598
+ sql = tableColumnToSqlWithAs(
4599
+ ctx,
4600
+ table.q,
4601
+ item,
4602
+ tableName,
4603
+ key,
4604
+ quotedAs,
4605
+ true
4047
4606
  );
4607
+ } else {
4608
+ if (hookSelect == null ? void 0 : hookSelect.get(item))
4609
+ (selected != null ? selected : selected = {})[item] = quotedAs;
4610
+ sql = ownColumnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4611
+ }
4612
+ }
4613
+ list.push(sql);
4614
+ } else if (item) {
4615
+ if ("selectAs" in item) {
4616
+ const obj = item.selectAs;
4617
+ for (const as in obj) {
4618
+ if (hookSelect)
4619
+ (selected != null ? selected : selected = {})[as] = true;
4620
+ const value = obj[as];
4621
+ if (typeof value === "object") {
4622
+ if (orchidCore.isExpression(value)) {
4623
+ list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
4624
+ } else {
4625
+ pushSubQuerySql(ctx, value, as, list, quotedAs);
4626
+ }
4627
+ } else if (value) {
4628
+ list.push(
4629
+ `${columnToSql(
4630
+ ctx,
4631
+ table.q,
4632
+ table.q.shape,
4633
+ value,
4634
+ quotedAs,
4635
+ true
4636
+ )} "${as}"`
4637
+ );
4638
+ }
4048
4639
  }
4640
+ } else {
4641
+ list.push(selectedObjectToSQL(ctx, quotedAs, item));
4049
4642
  }
4643
+ }
4644
+ }
4645
+ }
4646
+ if (hookSelect) {
4647
+ for (const column of hookSelect.keys()) {
4648
+ const item = hookSelect.get(column);
4649
+ const { select } = item;
4650
+ let sql;
4651
+ let quotedTable;
4652
+ let columnName;
4653
+ let col;
4654
+ const index = select.indexOf(".");
4655
+ if (index !== -1) {
4656
+ const tableName = select.slice(0, index);
4657
+ quotedTable = `"${tableName}"`;
4658
+ columnName = select.slice(index + 1);
4659
+ col = (_b = (_a = table.q.joinedShapes) == null ? void 0 : _a[tableName]) == null ? void 0 : _b[columnName];
4660
+ sql = (col == null ? void 0 : col.data.computed) ? col.data.computed.toSQL(ctx, `"${tableName}"`) : `"${tableName}"."${(col == null ? void 0 : col.data.name) || columnName}"`;
4050
4661
  } else {
4051
- list.push(selectedObjectToSQL(ctx, quotedAs, item));
4662
+ quotedTable = quotedAs;
4663
+ columnName = select;
4664
+ col = query.shape[select];
4665
+ sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4052
4666
  }
4667
+ if (selected == null ? void 0 : selected[columnName]) {
4668
+ if ((selected == null ? void 0 : selected[columnName]) === quotedTable) {
4669
+ hookSelect.delete(column);
4670
+ continue;
4671
+ }
4672
+ let i = 2;
4673
+ let name;
4674
+ while (selected[name = `${column}${i}`])
4675
+ i++;
4676
+ item.as = name;
4677
+ sql += ` "${name}"`;
4678
+ } else if (col == null ? void 0 : col.data.name) {
4679
+ sql += ` "${columnName}"`;
4680
+ }
4681
+ list.push(sql);
4053
4682
  }
4054
- return list.join(", ");
4055
4683
  }
4056
- return selectAllSql(table, query, quotedAs);
4684
+ return list.length ? list.join(", ") : selectAllSql(table, query, quotedAs);
4057
4685
  };
4058
- const selectedStringToSQL = (ctx, table, query, quotedAs, item) => item === "*" ? selectAllSql(table, query, quotedAs) : columnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4059
4686
  function selectedObjectToSQL(ctx, quotedAs, item) {
4060
4687
  const sql = item.toSQL(ctx, quotedAs);
4061
4688
  return ctx.aliasValue ? `${sql} r` : sql;
@@ -4065,7 +4692,7 @@ const selectAllSql = (table, query, quotedAs) => {
4065
4692
  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
4693
  };
4067
4694
  const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
4068
- var _a;
4695
+ var _a, _b, _c;
4069
4696
  const { returnType = "all" } = query.q;
4070
4697
  if (isQueryNone(query)) {
4071
4698
  let sql;
@@ -4129,20 +4756,27 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
4129
4756
  case "pluck": {
4130
4757
  const { select } = query.q;
4131
4758
  const first = select == null ? void 0 : select[0];
4132
- if (!select || !first) {
4759
+ if (!first && ((_b = query.q.computeds) == null ? void 0 : _b[as])) {
4760
+ query = queryJson(query);
4761
+ } else if (!first) {
4133
4762
  throw new OrchidOrmInternalError(
4134
4763
  query,
4135
4764
  `Nothing was selected for pluck`
4136
4765
  );
4766
+ } else {
4767
+ const cloned = query.clone();
4768
+ cloned.q.select = [{ selectAs: { c: first } }];
4769
+ query = queryWrap(cloned, cloned.baseQuery.clone());
4770
+ _queryGetOptional(query, new RawSQL(`COALESCE(json_agg("c"), '[]')`));
4137
4771
  }
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
4772
  break;
4143
4773
  }
4144
4774
  case "value":
4145
4775
  case "valueOrThrow":
4776
+ if ((_c = query.q.computeds) == null ? void 0 : _c[as]) {
4777
+ query = queryJson(query);
4778
+ }
4779
+ break;
4146
4780
  case "rows":
4147
4781
  case "rowCount":
4148
4782
  case "void":
@@ -4644,42 +5278,22 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs) =
4644
5278
  };
4645
5279
  const pushReturningSql = (ctx, q, data, quotedAs, hookSelect, keyword = "RETURNING") => {
4646
5280
  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);
5281
+ if (!(hookSelect == null ? void 0 : hookSelect.size) && !select)
5282
+ return hookSelect && /* @__PURE__ */ new Map();
5283
+ ctx.sql.push(keyword);
5284
+ if (q.q.hookSelect || hookSelect) {
5285
+ const tempSelect = new Map(q.q.hookSelect);
4653
5286
  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
- }
5287
+ for (const column of hookSelect) {
5288
+ tempSelect.set(column, { select: column });
4663
5289
  }
4664
5290
  }
4665
- } else {
4666
- hookFiltered = [];
4667
- for (const column of hookSelect) {
4668
- if (!hookFiltered.includes(column))
4669
- hookFiltered.push(column);
4670
- }
5291
+ ctx.sql.push(selectToSql(ctx, q, data, quotedAs, tempSelect));
5292
+ return tempSelect;
5293
+ } else if (select) {
5294
+ ctx.sql.push(selectToSql(ctx, q, data, quotedAs));
4671
5295
  }
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);
4681
- }
4682
- return hookFiltered;
5296
+ return;
4683
5297
  };
4684
5298
 
4685
5299
  const pushUpdateSql = (ctx, table, query, quotedAs) => {
@@ -5045,7 +5659,7 @@ const makeSQL = (table, options) => {
5045
5659
  if (query.for.mode)
5046
5660
  sql.push(query.for.mode);
5047
5661
  }
5048
- return { text: sql.join(" "), values };
5662
+ return { text: sql.join(" "), values, hookSelect: query.hookSelect };
5049
5663
  };
5050
5664
  function pushLimitSQL(sql, values, q) {
5051
5665
  if (!q.returnsOne) {
@@ -5078,6 +5692,8 @@ const cloneQuery = (q) => {
5078
5692
  q.with = q.with.slice(0);
5079
5693
  if (q.select)
5080
5694
  q.select = q.select.slice(0);
5695
+ if (q.hookSelect)
5696
+ q.hookSelect = new Map(q.hookSelect);
5081
5697
  if (q.and)
5082
5698
  q.and = q.and.slice(0);
5083
5699
  if (q.or)
@@ -5090,6 +5706,8 @@ const cloneQuery = (q) => {
5090
5706
  q.joinedShapes = __spreadValues$a({}, q.joinedShapes);
5091
5707
  if (q.scopes)
5092
5708
  q.scopes = __spreadValues$a({}, q.scopes);
5709
+ if (q.parsers)
5710
+ q.parsers = __spreadValues$a({}, q.parsers);
5093
5711
  if (q.updateData) {
5094
5712
  q.updateData = q.updateData.slice(
5095
5713
  0
@@ -5122,7 +5740,7 @@ const cloneQuery = (q) => {
5122
5740
  if (q.afterCreate) {
5123
5741
  q.afterCreate = q.afterCreate.slice(0);
5124
5742
  if (q.afterCreateSelect) {
5125
- q.afterCreateSelect = q.afterCreateSelect.slice(0);
5743
+ q.afterCreateSelect = new Set(q.afterCreateSelect);
5126
5744
  }
5127
5745
  }
5128
5746
  } else if (q.type === "update") {
@@ -5131,7 +5749,7 @@ const cloneQuery = (q) => {
5131
5749
  if (q.afterUpdate) {
5132
5750
  q.afterUpdate = q.afterUpdate.slice(0);
5133
5751
  if (q.afterUpdateSelect) {
5134
- q.afterUpdateSelect = q.afterUpdateSelect.slice(0);
5752
+ q.afterUpdateSelect = new Set(q.afterUpdateSelect);
5135
5753
  }
5136
5754
  }
5137
5755
  } else if (q.type === "delete") {
@@ -5140,7 +5758,7 @@ const cloneQuery = (q) => {
5140
5758
  if (q.afterDelete) {
5141
5759
  q.afterDelete = q.afterDelete.slice(0);
5142
5760
  if (q.afterDeleteSelect) {
5143
- q.afterDeleteSelect = q.afterDeleteSelect.slice(0);
5761
+ q.afterDeleteSelect = new Set(q.afterDeleteSelect);
5144
5762
  }
5145
5763
  }
5146
5764
  }
@@ -5165,8 +5783,6 @@ var __spreadValues$9 = (a, b) => {
5165
5783
  const getClonedQueryData = (query) => {
5166
5784
  const cloned = __spreadValues$9({}, query);
5167
5785
  delete cloned[toSQLCacheKey];
5168
- if (cloned.parsers)
5169
- cloned.parsers = __spreadValues$9({}, cloned.parsers);
5170
5786
  cloneQuery(cloned);
5171
5787
  return cloned;
5172
5788
  };
@@ -5371,12 +5987,14 @@ class VirtualColumn extends ColumnType {
5371
5987
  }
5372
5988
  }
5373
5989
 
5374
- class UnknownColumn extends VirtualColumn {
5990
+ const _UnknownColumn = class extends VirtualColumn {
5375
5991
  constructor(schema) {
5376
5992
  super(schema, schema.unknown());
5377
5993
  }
5378
- }
5379
- RawSQL.prototype.result = { value: new UnknownColumn(defaultSchemaConfig) };
5994
+ };
5995
+ let UnknownColumn = _UnknownColumn;
5996
+ UnknownColumn.instance = new _UnknownColumn(defaultSchemaConfig);
5997
+ RawSQL.prototype.result = { value: UnknownColumn.instance };
5380
5998
 
5381
5999
  const makeColumnsByType = (schema) => {
5382
6000
  const t = makeColumnTypes(schema);
@@ -6425,7 +7043,7 @@ const insert = (self, {
6425
7043
  q.values = values;
6426
7044
  if (!q.kind)
6427
7045
  q.kind = kind;
6428
- const { select, returnType = "all" } = q;
7046
+ const { select, returnType } = q;
6429
7047
  if (!select) {
6430
7048
  if (returnType !== "void")
6431
7049
  q.returnType = "rowCount";
@@ -6435,7 +7053,7 @@ const insert = (self, {
6435
7053
  } else if (returnType === "value" || returnType === "valueOrThrow") {
6436
7054
  q.returnType = "pluck";
6437
7055
  }
6438
- } else if (returnType === "all") {
7056
+ } else if (!returnType || returnType === "all") {
6439
7057
  q.returnType = "from" in values ? values.from.q.returnType : "one";
6440
7058
  } else if (returnType === "pluck") {
6441
7059
  q.returnType = "valueOrThrow";
@@ -6454,7 +7072,7 @@ const getFromSelectColumns = (from, obj, many) => {
6454
7072
  if (typeof item === "string") {
6455
7073
  const index = item.indexOf(".");
6456
7074
  queryColumns.push(index === -1 ? item : item.slice(index + 1));
6457
- } else if ("selectAs" in item) {
7075
+ } else if (item && "selectAs" in item) {
6458
7076
  queryColumns.push(...Object.keys(item.selectAs));
6459
7077
  }
6460
7078
  });
@@ -6498,12 +7116,7 @@ const _queryCreateMany = (q, data) => {
6498
7116
  };
6499
7117
  const _queryInsertMany = (q, data) => {
6500
7118
  const ctx = createCtx();
6501
- let result = insert(
6502
- q,
6503
- handleManyData(q, data, ctx),
6504
- "object",
6505
- true
6506
- );
7119
+ let result = insert(q, handleManyData(q, data, ctx), "object", true);
6507
7120
  if (!data.length)
6508
7121
  result = result.none();
6509
7122
  return result;
@@ -7316,11 +7929,15 @@ class Having {
7316
7929
  }
7317
7930
 
7318
7931
  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
- );
7932
+ const after = (q, key, select, cb, commit) => {
7933
+ var _a, _b, _c;
7934
+ pushQueryValue(q, `after${key}${commit ? "Commit" : ""}`, cb);
7935
+ const set = (_c = (_a = q.q)[_b = `after${key}Select`]) != null ? _c : _a[_b] = /* @__PURE__ */ new Set();
7936
+ for (const column of select) {
7937
+ set.add(column);
7938
+ }
7939
+ return q;
7940
+ };
7324
7941
  const _queryHookBeforeQuery = (q, cb) => {
7325
7942
  return pushQueryValue(q, "before", cb);
7326
7943
  };
@@ -8381,7 +8998,9 @@ const mergableObjects = {
8381
8998
  parsers: true,
8382
8999
  defaults: true,
8383
9000
  joinedShapes: true,
8384
- joinedParsers: true
9001
+ joinedParsers: true,
9002
+ joinedBatchParsers: true,
9003
+ selectedComputeds: true
8385
9004
  };
8386
9005
  class MergeQueryMethods {
8387
9006
  merge(q) {
@@ -8406,6 +9025,8 @@ class MergeQueryMethods {
8406
9025
  b: a[key].b,
8407
9026
  u: [...a[key].u, ...value.u]
8408
9027
  } : value;
9028
+ } else if (value instanceof Set) {
9029
+ a[key] = a[key] ? /* @__PURE__ */ new Set([...a[key], ...value]) : value;
8409
9030
  } else {
8410
9031
  a[key] = value;
8411
9032
  }
@@ -8600,7 +9221,10 @@ class WithMethods {
8600
9221
  }
8601
9222
  pushQueryValue(q, "with", { n: name, o: options, q: query });
8602
9223
  const shape = getShapeFromSelect(query, true);
8603
- return setQueryObjectValue(q, "withShapes", name, shape);
9224
+ return setQueryObjectValue(q, "withShapes", name, {
9225
+ shape,
9226
+ computeds: query.q.computeds
9227
+ });
8604
9228
  }
8605
9229
  withRecursive(name, ...args) {
8606
9230
  var _a, _b, _c;
@@ -8609,10 +9233,9 @@ class WithMethods {
8609
9233
  const arg = q.queryBuilder.clone();
8610
9234
  arg.q.withShapes = q.q.withShapes;
8611
9235
  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
- );
9236
+ const shape = getShapeFromSelect(query, true);
9237
+ const withConfig = { shape, computeds: query.q.computeds };
9238
+ ((_b = (_a = arg.q).withShapes) != null ? _b : _a.withShapes = {})[name] = withConfig;
8616
9239
  const recursive = recursiveFn(arg);
8617
9240
  query = _queryUnion(query, [recursive], (_c = options.union) != null ? _c : "UNION ALL");
8618
9241
  options.recursive = true;
@@ -8622,7 +9245,7 @@ class WithMethods {
8622
9245
  });
8623
9246
  }
8624
9247
  pushQueryValue(q, "with", { n: name, o: options, q: query });
8625
- return setQueryObjectValue(q, "withShapes", name, shape);
9248
+ return setQueryObjectValue(q, "withShapes", name, withConfig);
8626
9249
  }
8627
9250
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8628
9251
  withSql(name, ...args) {
@@ -8633,12 +9256,9 @@ class WithMethods {
8633
9256
  o: options,
8634
9257
  s: sql(q)
8635
9258
  });
8636
- return setQueryObjectValue(
8637
- q,
8638
- "withShapes",
8639
- name,
8640
- shape(this.columnTypes)
8641
- );
9259
+ return setQueryObjectValue(q, "withShapes", name, {
9260
+ shape: shape(this.columnTypes)
9261
+ });
8642
9262
  }
8643
9263
  }
8644
9264
 
@@ -10156,12 +10776,9 @@ function orCreate(q, data, updateData, mergeData) {
10156
10776
  if (mergeData)
10157
10777
  data = __spreadValues$1(__spreadValues$1({}, mergeData), data);
10158
10778
  const inner = q2.create(data);
10159
- const { handleResult: handleResult2 } = inner.q;
10160
10779
  inner.q.handleResult = (q3, t, r, s) => {
10161
- queryResult = r;
10162
- const res = handleResult2(q3, t, r, s);
10163
- result = res;
10164
- return res;
10780
+ result = handleResult(q3, t, r, s);
10781
+ return inner.q.hookSelect ? result.map((row) => __spreadValues$1({}, row)) : result;
10165
10782
  };
10166
10783
  await inner;
10167
10784
  created = true;
@@ -10809,8 +11426,13 @@ class QueryMethods {
10809
11426
  pluck(select) {
10810
11427
  const q = this.clone();
10811
11428
  q.q.returnType = "pluck";
10812
- q.q.select = [select];
10813
- addParserForSelectItem(q, q.q.as || q.table, "pluck", select);
11429
+ const selected = addParserForSelectItem(
11430
+ q,
11431
+ q.q.as || q.table,
11432
+ "pluck",
11433
+ select
11434
+ );
11435
+ q.q.select = selected ? [selected] : void 0;
10814
11436
  return q;
10815
11437
  }
10816
11438
  /**
@@ -11562,6 +12184,7 @@ class Db {
11562
12184
  tableData
11563
12185
  };
11564
12186
  this.baseQuery = this;
12187
+ this.relations = {};
11565
12188
  const logger = options.logger || console;
11566
12189
  const parsers = {};
11567
12190
  let hasParsers = false;
@@ -11612,6 +12235,7 @@ class Db {
11612
12235
  );
11613
12236
  }
11614
12237
  this.internal.columnsForSelectAll = list;
12238
+ this.internal.columnsKeysForSelectAll = __spreadValues({}, shape);
11615
12239
  }
11616
12240
  this.q = {
11617
12241
  adapter,
@@ -11650,6 +12274,8 @@ class Db {
11650
12274
  this.defaultSelectColumns = columns.filter(
11651
12275
  (column) => !shape[column].data.isHidden
11652
12276
  );
12277
+ if (options.computed)
12278
+ applyComputedColumns(this, options.computed);
11653
12279
  const defaultSelect = this.defaultSelectColumns.length === columns.length ? void 0 : this.defaultSelectColumns;
11654
12280
  this.toSQL = defaultSelect ? function(options2) {
11655
12281
  const q = this.clone();
@@ -11658,7 +12284,6 @@ class Db {
11658
12284
  }
11659
12285
  return toSQL.call(q, options2);
11660
12286
  } : toSQL;
11661
- this.relations = {};
11662
12287
  if (modifyQuery) {
11663
12288
  for (const cb of modifyQuery) {
11664
12289
  cb(this);
@@ -11956,16 +12581,6 @@ const testTransaction = {
11956
12581
  }
11957
12582
  };
11958
12583
 
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
12584
  const rowToColumnInfo = (row) => {
11970
12585
  const typed = row;
11971
12586
  return {
@@ -12022,6 +12637,7 @@ exports.CitextColumn = CitextColumn;
12022
12637
  exports.Clear = Clear;
12023
12638
  exports.ColumnRefExpression = ColumnRefExpression;
12024
12639
  exports.ColumnType = ColumnType;
12640
+ exports.ComputedColumn = ComputedColumn;
12025
12641
  exports.Create = Create;
12026
12642
  exports.CustomTypeColumn = CustomTypeColumn;
12027
12643
  exports.DateBaseColumn = DateBaseColumn;
@@ -12162,11 +12778,11 @@ exports._queryWhereIn = _queryWhereIn;
12162
12778
  exports._queryWhereNot = _queryWhereNot;
12163
12779
  exports._queryWhereNotSql = _queryWhereNotSql;
12164
12780
  exports._queryWhereSql = _queryWhereSql;
12165
- exports.addComputedColumns = addComputedColumns;
12166
12781
  exports.addParserForRawExpression = addParserForRawExpression;
12167
12782
  exports.addParserForSelectItem = addParserForSelectItem;
12168
12783
  exports.addQueryOn = addQueryOn;
12169
12784
  exports.anyShape = anyShape;
12785
+ exports.applyComputedColumns = applyComputedColumns;
12170
12786
  exports.checkIfASimpleQuery = checkIfASimpleQuery;
12171
12787
  exports.cloneQuery = cloneQuery;
12172
12788
  exports.cloneQueryBaseUnscoped = cloneQueryBaseUnscoped;
@@ -12183,6 +12799,7 @@ exports.countSelect = countSelect;
12183
12799
  exports.createDb = createDb;
12184
12800
  exports.defaultSchemaConfig = defaultSchemaConfig;
12185
12801
  exports.extendQuery = extendQuery;
12802
+ exports.filterResult = filterResult;
12186
12803
  exports.foreignKeyArgumentToCode = foreignKeyArgumentToCode;
12187
12804
  exports.getClonedQueryData = getClonedQueryData;
12188
12805
  exports.getColumnInfo = getColumnInfo;
@@ -12208,10 +12825,11 @@ exports.makeFnExpression = makeFnExpression;
12208
12825
  exports.makeRegexToFindInSql = makeRegexToFindInSql;
12209
12826
  exports.makeSQL = makeSQL;
12210
12827
  exports.parseRecord = parseRecord;
12211
- exports.parseResult = parseResult;
12212
12828
  exports.parseTableData = parseTableData;
12213
12829
  exports.parseTableDataInput = parseTableDataInput;
12214
12830
  exports.primaryKeyInnerToCode = primaryKeyInnerToCode;
12831
+ exports.processComputedBatches = processComputedBatches;
12832
+ exports.processComputedResult = processComputedResult;
12215
12833
  exports.processSelectArg = processSelectArg;
12216
12834
  exports.pushLimitSQL = pushLimitSQL;
12217
12835
  exports.pushQueryArray = pushQueryArray;