pqb 0.33.2 → 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
  };
@@ -1238,17 +1238,11 @@ class TextBaseColumn extends ColumnType {
1238
1238
  }
1239
1239
  class LimitedTextBaseColumn extends TextBaseColumn {
1240
1240
  constructor(schema, limit) {
1241
- super(
1242
- schema,
1243
- limit ? schema.stringMax(limit) : schema.stringSchema()
1244
- );
1241
+ super(schema, schema.stringMax(limit));
1245
1242
  this.data.maxChars = limit;
1246
1243
  }
1247
1244
  toSQL() {
1248
- return orchidCore.joinTruthy(
1249
- this.dataType,
1250
- this.data.maxChars !== void 0 && `(${this.data.maxChars})`
1251
- );
1245
+ return orchidCore.joinTruthy(this.dataType, `(${this.data.maxChars})`);
1252
1246
  }
1253
1247
  }
1254
1248
  class VarCharColumn extends LimitedTextBaseColumn {
@@ -1282,31 +1276,8 @@ class StringColumn extends VarCharColumn {
1282
1276
  );
1283
1277
  }
1284
1278
  }
1285
- class CharColumn extends LimitedTextBaseColumn {
1286
- constructor() {
1287
- super(...arguments);
1288
- this.dataType = "char";
1289
- }
1290
- toCode(t, m) {
1291
- const { maxChars } = this.data;
1292
- return columnCode(
1293
- this,
1294
- t,
1295
- `char(${maxChars != null ? maxChars : ""})${orchidCore.stringDataToCode(this.data, m)}`,
1296
- m
1297
- );
1298
- }
1299
- }
1300
- const setTextColumnData = (column, minArg, maxArg) => {
1301
- if (minArg !== void 0) {
1302
- column.data.min = column.data.minArg = minArg;
1303
- if (maxArg !== void 0) {
1304
- column.data.max = column.data.maxArg = maxArg;
1305
- }
1306
- }
1307
- };
1308
1279
  const textColumnToCode = (column, t, m) => {
1309
- const data = __spreadValues$e({}, column.data);
1280
+ const data = __spreadValues$f({}, column.data);
1310
1281
  let args = "";
1311
1282
  const hasMax = data.maxArg !== void 0 && data.max === data.maxArg;
1312
1283
  if (data.minArg !== void 0 && data.min === data.minArg || hasMax) {
@@ -1328,12 +1299,10 @@ const textColumnToCode = (column, t, m) => {
1328
1299
  m
1329
1300
  );
1330
1301
  };
1331
- const minMaxToSchema = (schema, min, max) => min ? max ? schema.stringMinMax(min, max) : schema.stringMin(min) : schema.stringSchema();
1332
1302
  class TextColumn extends TextBaseColumn {
1333
- constructor(schema, min, max) {
1334
- super(schema, minMaxToSchema(schema, min, max));
1303
+ constructor(schema) {
1304
+ super(schema, schema.stringSchema());
1335
1305
  this.dataType = "text";
1336
- setTextColumnData(this, min, max);
1337
1306
  }
1338
1307
  toCode(t, m) {
1339
1308
  return textColumnToCode(this, t, m);
@@ -1605,7 +1574,7 @@ class UUIDColumn extends ColumnType {
1605
1574
  `uuid()`,
1606
1575
  m,
1607
1576
  // don't output the default default
1608
- 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
1609
1578
  );
1610
1579
  }
1611
1580
  }
@@ -1620,10 +1589,9 @@ class XMLColumn extends ColumnType {
1620
1589
  }
1621
1590
  }
1622
1591
  class CitextColumn extends TextBaseColumn {
1623
- constructor(schema, min, max) {
1624
- super(schema, minMaxToSchema(schema, min, max));
1592
+ constructor(schema) {
1593
+ super(schema, schema.stringSchema());
1625
1594
  this.dataType = "citext";
1626
- setTextColumnData(this, min, max);
1627
1595
  }
1628
1596
  toCode(t, m) {
1629
1597
  return textColumnToCode(this, t, m);
@@ -1902,31 +1870,41 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
1902
1870
  return `"${tableName}"."${key}"`;
1903
1871
  };
1904
1872
  const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1905
- var _a, _b, _c, _d;
1906
1873
  const index = column.indexOf(".");
1907
- if (index !== -1) {
1908
- const table = column.slice(0, index);
1909
- const key = column.slice(index + 1);
1910
- if (key === "*") {
1911
- if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1912
- return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
1913
- }
1914
- 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}"`;
1915
1889
  }
1916
- const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1917
- const quoted = `"${table}"`;
1918
- const col2 = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
1919
- if (col2) {
1920
- if (col2.data.name && col2.data.name !== key) {
1921
- return `"${tableName}"."${col2.data.name}" "${key}"`;
1922
- }
1923
- if (col2.data.computed) {
1924
- return `${col2.data.computed.toSQL(ctx, quoted)} "${key}"`;
1925
- }
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}"`;
1926
1901
  }
1927
- return `"${tableName}"."${key}"`;
1928
1902
  }
1929
- 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])) {
1930
1908
  return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
1931
1909
  }
1932
1910
  const col = data.shape[column];
@@ -1940,11 +1918,6 @@ const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1940
1918
  }
1941
1919
  return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
1942
1920
  };
1943
- const ownColumnToSql = (data, column, quotedAs) => {
1944
- var _a;
1945
- const name = (_a = data.shape[column]) == null ? void 0 : _a.data.name;
1946
- return `${quotedAs ? `${quotedAs}.` : ""}"${name || column}"${name && name !== column ? ` "${column}"` : ""}`;
1947
- };
1948
1921
  const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
1949
1922
  return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
1950
1923
  };
@@ -2346,21 +2319,21 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
2346
2319
  ands.push(`${multiple ? `(${columnsSql})` : columnsSql} IN ${value}`);
2347
2320
  };
2348
2321
 
2349
- var __defProp$d = Object.defineProperty;
2322
+ var __defProp$e = Object.defineProperty;
2350
2323
  var __defProps$5 = Object.defineProperties;
2351
2324
  var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
2352
- var __getOwnPropSymbols$e = Object.getOwnPropertySymbols;
2353
- var __hasOwnProp$e = Object.prototype.hasOwnProperty;
2354
- var __propIsEnum$e = Object.prototype.propertyIsEnumerable;
2355
- var __defNormalProp$d = (obj, key, value) => key in obj ? __defProp$d(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2356
- 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) => {
2357
2330
  for (var prop in b || (b = {}))
2358
- if (__hasOwnProp$e.call(b, prop))
2359
- __defNormalProp$d(a, prop, b[prop]);
2360
- if (__getOwnPropSymbols$e)
2361
- for (var prop of __getOwnPropSymbols$e(b)) {
2362
- if (__propIsEnum$e.call(b, prop))
2363
- __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]);
2364
2337
  }
2365
2338
  return a;
2366
2339
  };
@@ -2426,8 +2399,8 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
2426
2399
  const whereSql = whereToSql(
2427
2400
  ctx,
2428
2401
  q,
2429
- __spreadProps$5(__spreadValues$d({}, q.q), {
2430
- 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), {
2431
2404
  [table.q.as || table.table]: table.q.shape
2432
2405
  })
2433
2406
  }),
@@ -2574,21 +2547,21 @@ const getIsJoinSubQuery = (query) => {
2574
2547
  return false;
2575
2548
  };
2576
2549
 
2577
- var __defProp$c = Object.defineProperty;
2550
+ var __defProp$d = Object.defineProperty;
2578
2551
  var __defProps$4 = Object.defineProperties;
2579
2552
  var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
2580
- var __getOwnPropSymbols$d = Object.getOwnPropertySymbols;
2581
- var __hasOwnProp$d = Object.prototype.hasOwnProperty;
2582
- var __propIsEnum$d = Object.prototype.propertyIsEnumerable;
2583
- var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2584
- 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) => {
2585
2558
  for (var prop in b || (b = {}))
2586
- if (__hasOwnProp$d.call(b, prop))
2587
- __defNormalProp$c(a, prop, b[prop]);
2588
- if (__getOwnPropSymbols$d)
2589
- for (var prop of __getOwnPropSymbols$d(b)) {
2590
- if (__propIsEnum$d.call(b, prop))
2591
- __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]);
2592
2565
  }
2593
2566
  return a;
2594
2567
  };
@@ -2610,27 +2583,28 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2610
2583
  return { w: first, a: args };
2611
2584
  } else {
2612
2585
  const joinToQ = joinTo.q;
2613
- const shape = (_a = joinToQ.withShapes) == null ? void 0 : _a[first];
2614
- if (!shape) {
2615
- 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");
2616
2589
  }
2617
2590
  const j = joinTo.queryBuilder.baseQuery.clone();
2618
2591
  j.table = first;
2619
2592
  j.q = {
2620
- shape,
2593
+ shape: w.shape,
2594
+ computeds: w.computeds,
2621
2595
  adapter: joinToQ.adapter,
2622
2596
  handleResult: joinToQ.handleResult,
2623
2597
  returnType: "all",
2624
2598
  logger: joinToQ.logger
2625
2599
  };
2626
2600
  j.baseQuery = j;
2627
- const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinToQ.joinedShapes), {
2601
+ const joinedShapes = __spreadProps$4(__spreadValues$d({}, joinToQ.joinedShapes), {
2628
2602
  [joinToQ.as || joinTo.table]: joinTo.shape
2629
2603
  });
2630
2604
  const r = args[0](
2631
2605
  makeJoinQueryBuilder(
2632
2606
  j,
2633
- 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,
2634
2608
  joinTo
2635
2609
  )
2636
2610
  );
@@ -2653,16 +2627,16 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2653
2627
  pushQueryArray(q, "or", query.or);
2654
2628
  }
2655
2629
  if (query.scopes) {
2656
- 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);
2657
2631
  }
2658
2632
  }
2659
- const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2633
+ const joinedShapes = __spreadProps$4(__spreadValues$d({}, joinTo.q.joinedShapes), {
2660
2634
  [joinTo.q.as || joinTo.table]: joinTo.shape
2661
2635
  });
2662
2636
  const r = args0(
2663
2637
  makeJoinQueryBuilder(
2664
2638
  q,
2665
- 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,
2666
2640
  joinTo
2667
2641
  )
2668
2642
  );
@@ -2710,30 +2684,32 @@ const _queryNone = (q) => {
2710
2684
  };
2711
2685
  const isQueryNone = (q) => q.then === noneMethods.then;
2712
2686
 
2713
- var __defProp$b = Object.defineProperty;
2687
+ var __defProp$c = Object.defineProperty;
2714
2688
  var __defProps$3 = Object.defineProperties;
2715
2689
  var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
2716
- var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
2717
- var __hasOwnProp$c = Object.prototype.hasOwnProperty;
2718
- var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
2719
- var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2720
- 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) => {
2721
2695
  for (var prop in b || (b = {}))
2722
- if (__hasOwnProp$c.call(b, prop))
2723
- __defNormalProp$b(a, prop, b[prop]);
2724
- if (__getOwnPropSymbols$c)
2725
- for (var prop of __getOwnPropSymbols$c(b)) {
2726
- if (__propIsEnum$c.call(b, prop))
2727
- __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]);
2728
2702
  }
2729
2703
  return a;
2730
2704
  };
2731
2705
  var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
2732
2706
  const _join = (query, require2, type, first, args) => {
2733
- var _a, _b;
2707
+ var _a, _b, _c, _d;
2734
2708
  let joinKey;
2735
2709
  let shape;
2736
2710
  let parsers;
2711
+ let batchParsers;
2712
+ let computeds;
2737
2713
  let joinSubQuery = false;
2738
2714
  if (typeof first === "function") {
2739
2715
  first = first(query.relations);
@@ -2749,6 +2725,8 @@ const _join = (query, require2, type, first, args) => {
2749
2725
  if (joinKey) {
2750
2726
  shape = getShapeFromSelect(q, joinSubQuery);
2751
2727
  parsers = q.q.parsers;
2728
+ batchParsers = q.q.batchParsers;
2729
+ computeds = q.q.computeds;
2752
2730
  if (joinSubQuery) {
2753
2731
  first = q.clone();
2754
2732
  first.shape = shape;
@@ -2759,12 +2737,17 @@ const _join = (query, require2, type, first, args) => {
2759
2737
  const relation = query.relations[joinKey];
2760
2738
  if (relation) {
2761
2739
  shape = getShapeFromSelect(relation.relationConfig.query);
2762
- 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;
2763
2744
  } else {
2764
- 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;
2765
2748
  if (shape) {
2766
2749
  if (!require2)
2767
- shape = __spreadValues$b({}, shape);
2750
+ shape = __spreadValues$c({}, shape);
2768
2751
  parsers = {};
2769
2752
  for (const key in shape) {
2770
2753
  const parser = shape[key].parseFn;
@@ -2775,20 +2758,6 @@ const _join = (query, require2, type, first, args) => {
2775
2758
  }
2776
2759
  }
2777
2760
  }
2778
- if (joinKey) {
2779
- setQueryObjectValue(
2780
- query,
2781
- "joinedShapes",
2782
- joinKey,
2783
- shape
2784
- );
2785
- setQueryObjectValue(
2786
- query,
2787
- "joinedParsers",
2788
- joinKey,
2789
- parsers
2790
- );
2791
- }
2792
2761
  const joinArgs = processJoinArgs(
2793
2762
  query,
2794
2763
  first,
@@ -2811,17 +2780,60 @@ const _join = (query, require2, type, first, args) => {
2811
2780
  joinKey,
2812
2781
  j.q.parsers
2813
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
+ );
2814
2801
  }
2815
2802
  } else if (require2 && "r" in joinArgs && isQueryNone(joinArgs.r)) {
2816
2803
  return _queryNone(query);
2804
+ } else {
2805
+ addAllShapesAndParsers(
2806
+ query,
2807
+ joinKey,
2808
+ shape,
2809
+ parsers,
2810
+ batchParsers,
2811
+ computeds
2812
+ );
2817
2813
  }
2818
2814
  return pushQueryValue(query, "join", {
2819
2815
  type,
2820
2816
  args: joinArgs
2821
2817
  });
2822
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
+ };
2823
2835
  const _joinLateral = (self, type, arg, cb, as) => {
2824
- var _a, _b, _c;
2836
+ var _a, _b, _c, _d, _e, _f, _g;
2825
2837
  const q = self;
2826
2838
  let relation;
2827
2839
  if (typeof arg === "string") {
@@ -2829,13 +2841,14 @@ const _joinLateral = (self, type, arg, cb, as) => {
2829
2841
  if (relation) {
2830
2842
  arg = relation.relationConfig.query.clone();
2831
2843
  } else {
2832
- const shape = (_a = q.q.withShapes) == null ? void 0 : _a[arg];
2833
- if (shape) {
2844
+ const w = (_a = q.q.withShapes) == null ? void 0 : _a[arg];
2845
+ if (w) {
2834
2846
  const t = Object.create(q.queryBuilder);
2835
2847
  t.table = arg;
2836
- t.shape = shape;
2837
- t.q = __spreadProps$3(__spreadValues$b({}, t.q), {
2838
- shape
2848
+ t.shape = w.shape;
2849
+ t.computeds = w.computeds;
2850
+ t.q = __spreadProps$3(__spreadValues$c({}, t.q), {
2851
+ shape: w.shape
2839
2852
  });
2840
2853
  t.baseQuery = t;
2841
2854
  arg = t;
@@ -2844,7 +2857,8 @@ const _joinLateral = (self, type, arg, cb, as) => {
2844
2857
  }
2845
2858
  const query = arg;
2846
2859
  query.q.joinTo = q;
2847
- ((_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;
2848
2862
  let result = resolveSubQueryCallback(query, cb);
2849
2863
  if (relation) {
2850
2864
  result = relation.relationConfig.joinQuery(
@@ -2857,12 +2871,13 @@ const _joinLateral = (self, type, arg, cb, as) => {
2857
2871
  const shape = getShapeFromSelect(result, true);
2858
2872
  setQueryObjectValue(q, "joinedShapes", joinKey, shape);
2859
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
+ }
2860
2877
  }
2861
- return pushQueryValue(q, "join", [
2862
- type,
2863
- result,
2864
- as || getQueryAs(result)
2865
- ]);
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]);
2866
2881
  };
2867
2882
 
2868
2883
  class EnumColumn extends ColumnType {
@@ -3047,10 +3062,9 @@ const defaultSchemaConfig = {
3047
3062
  bigSerial: () => new BigSerialColumn(defaultSchemaConfig),
3048
3063
  money: () => new MoneyColumn(defaultSchemaConfig),
3049
3064
  varchar: (limit) => new VarCharColumn(defaultSchemaConfig, limit),
3050
- char: (limit) => new CharColumn(defaultSchemaConfig, limit),
3051
- text: (min, max) => new TextColumn(defaultSchemaConfig, min, max),
3065
+ text: () => new TextColumn(defaultSchemaConfig),
3052
3066
  string: (limit) => new StringColumn(defaultSchemaConfig, limit),
3053
- citext: (min, max) => new CitextColumn(defaultSchemaConfig, min, max),
3067
+ citext: () => new CitextColumn(defaultSchemaConfig),
3054
3068
  date: () => new DateColumn(defaultSchemaConfig),
3055
3069
  timestampNoTZ: (precision) => new TimestampColumn(defaultSchemaConfig, precision),
3056
3070
  timestamp: (precision) => new TimestampTZColumn(defaultSchemaConfig, precision)
@@ -3140,21 +3154,215 @@ class Transaction {
3140
3154
  log.afterQuery(sql, logData);
3141
3155
  throw err;
3142
3156
  }
3143
- sql.text = `RELEASE SAVEPOINT "${transactionId}"`;
3144
- if (log)
3145
- logData = log.beforeQuery(sql);
3146
- await adapter.query(sql);
3147
- if (log)
3148
- log.afterQuery(sql, logData);
3149
- return result;
3150
- } finally {
3151
- trx.transactionId = transactionId - 1;
3157
+ sql.text = `RELEASE SAVEPOINT "${transactionId}"`;
3158
+ if (log)
3159
+ logData = log.beforeQuery(sql);
3160
+ await adapter.query(sql);
3161
+ if (log)
3162
+ log.afterQuery(sql, logData);
3163
+ return result;
3164
+ } finally {
3165
+ trx.transactionId = transactionId - 1;
3166
+ }
3167
+ }
3168
+ }
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
+ 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
+ }
3152
3311
  }
3153
3312
  }
3154
3313
  }
3155
- }
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
+ };
3156
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
+ };
3157
3364
  const queryMethodByReturnType = {
3365
+ undefined: "query",
3158
3366
  all: "query",
3159
3367
  rows: "arrays",
3160
3368
  pluck: "arrays",
@@ -3202,9 +3410,6 @@ Object.defineProperty(Then.prototype, "then", {
3202
3410
  });
3203
3411
  }
3204
3412
  });
3205
- const handleResult = (q, returnType, result, isSubQuery) => {
3206
- return parseResult(q, q.q.parsers, returnType, result, isSubQuery);
3207
- };
3208
3413
  function maybeWrappedThen(resolve, reject) {
3209
3414
  const { q } = this;
3210
3415
  let beforeHooks;
@@ -3262,7 +3467,7 @@ const callAfterHook = function(cb) {
3262
3467
  };
3263
3468
  const beginSql = { text: "BEGIN" };
3264
3469
  const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks, resolve, reject) => {
3265
- var _a;
3470
+ var _a, _b;
3266
3471
  const { q: query } = q;
3267
3472
  let sql;
3268
3473
  let logData;
@@ -3279,7 +3484,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3279
3484
  sql = q.toSQL();
3280
3485
  const { hookSelect } = sql;
3281
3486
  const { returnType = "all" } = query;
3282
- const returns = hookSelect ? "all" : returnType;
3487
+ const tempReturnType = hookSelect || returnType === "rows" && q.q.batchParsers ? "all" : returnType;
3283
3488
  let result;
3284
3489
  let queryResult;
3285
3490
  if ("text" in sql) {
@@ -3289,7 +3494,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3289
3494
  if (query.log) {
3290
3495
  logData = query.log.beforeQuery(sql);
3291
3496
  }
3292
- queryResult = await adapter[hookSelect ? "query" : queryMethodByReturnType[returnType]](sql);
3497
+ queryResult = await adapter[queryMethodByReturnType[tempReturnType]](sql);
3293
3498
  if (query.patchResult) {
3294
3499
  await query.patchResult(q, queryResult);
3295
3500
  }
@@ -3297,9 +3502,9 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3297
3502
  query.log.afterQuery(sql, logData);
3298
3503
  sql = void 0;
3299
3504
  }
3300
- result = query.handleResult(q, returns, queryResult);
3505
+ result = query.handleResult(q, tempReturnType, queryResult);
3301
3506
  } else {
3302
- const queryMethod = hookSelect ? "query" : queryMethodByReturnType[returnType];
3507
+ const queryMethod = queryMethodByReturnType[tempReturnType];
3303
3508
  if (!trx) {
3304
3509
  if (query.log)
3305
3510
  logData = query.log.beforeQuery(beginSql);
@@ -3334,9 +3539,37 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3334
3539
  if (query.patchResult) {
3335
3540
  await query.patchResult(q, queryResult);
3336
3541
  }
3337
- result = query.handleResult(q, returns, queryResult);
3542
+ result = query.handleResult(q, tempReturnType, queryResult);
3338
3543
  }
3339
- 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) {
3340
3573
  if (queryResult.rowCount) {
3341
3574
  if (afterHooks || query.after) {
3342
3575
  const args = [result, q];
@@ -3348,7 +3581,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3348
3581
  );
3349
3582
  }
3350
3583
  if (afterCommitHooks && trx) {
3351
- ((_a = trx.afterCommit) != null ? _a : trx.afterCommit = []).push(
3584
+ ((_b = trx.afterCommit) != null ? _b : trx.afterCommit = []).push(
3352
3585
  result,
3353
3586
  q,
3354
3587
  afterCommitHooks
@@ -3361,8 +3594,24 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3361
3594
  const args = [result, q];
3362
3595
  await Promise.all(query.after.map(callAfterHook, args));
3363
3596
  }
3364
- if (hookSelect)
3365
- 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
+ );
3366
3615
  }
3367
3616
  if (query.transform) {
3368
3617
  result = orchidCore.applyTransforms(returnType, query.transform, result);
@@ -3408,57 +3657,91 @@ const assignError = (to, from) => {
3408
3657
  to.routine = from.routine;
3409
3658
  return to;
3410
3659
  };
3411
- const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
3660
+ const handleResult = (q, returnType, result, isSubQuery) => {
3412
3661
  var _a, _b;
3662
+ const { parsers } = q.q;
3413
3663
  switch (returnType) {
3414
3664
  case "all": {
3415
3665
  if (q.q.throwOnNotFound && result.rows.length === 0)
3416
3666
  throw new NotFoundError(q);
3667
+ const promise = parseBatch(q, result);
3417
3668
  const { rows } = result;
3418
3669
  if (parsers) {
3419
3670
  for (const row of rows) {
3420
3671
  parseRecord(parsers, row);
3421
3672
  }
3422
3673
  }
3423
- return rows;
3674
+ return promise ? promise.then(() => rows) : rows;
3424
3675
  }
3425
3676
  case "one": {
3426
- const row = result.rows[0];
3427
- if (!row)
3677
+ const { rows } = result;
3678
+ if (!rows.length)
3428
3679
  return;
3429
- 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];
3430
3684
  }
3431
3685
  case "oneOrThrow": {
3432
- const row = result.rows[0];
3433
- if (!row)
3686
+ const { rows } = result;
3687
+ if (!rows.length)
3434
3688
  throw new NotFoundError(q);
3435
- 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];
3436
3693
  }
3437
3694
  case "rows": {
3438
- return parsers ? parseRows(
3439
- parsers,
3440
- result.fields,
3441
- result.rows
3442
- ) : 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;
3443
3707
  }
3444
3708
  case "pluck": {
3445
- const pluck = parsers == null ? void 0 : parsers.pluck;
3446
- if (pluck) {
3447
- return result.rows.map(isSubQuery ? pluck : (row) => pluck(row[0]));
3448
- } else if (isSubQuery) {
3449
- 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
+ });
3450
3716
  }
3451
- return result.rows.map((row) => row[0]);
3717
+ parsePluck(parsers, isSubQuery, rows);
3718
+ return rows;
3452
3719
  }
3453
3720
  case "value": {
3454
- const value = (_a = result.rows[0]) == null ? void 0 : _a[0];
3455
- 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;
3456
3730
  }
3457
3731
  case "valueOrThrow": {
3458
- const value = (_b = result.rows[0]) == null ? void 0 : _b[0];
3459
- 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)
3460
3743
  throw new NotFoundError(q);
3461
- return parseValue(value, parsers);
3744
+ return parseValue(rows[0][0], parsers);
3462
3745
  }
3463
3746
  case "rowCount": {
3464
3747
  if (q.q.throwOnNotFound && result.rowCount === 0) {
@@ -3471,6 +3754,17 @@ const parseResult = (q, parsers, returnType = "all", result, isSubQuery) => {
3471
3754
  }
3472
3755
  }
3473
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
+ };
3474
3768
  const parseRecord = (parsers, row) => {
3475
3769
  for (const key in parsers) {
3476
3770
  if (key in row) {
@@ -3488,38 +3782,43 @@ const parseRows = (parsers, fields, rows) => {
3488
3782
  }
3489
3783
  }
3490
3784
  }
3491
- 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
+ }
3492
3797
  };
3493
3798
  const parseValue = (value, parsers) => {
3494
3799
  const parser = parsers == null ? void 0 : parsers[orchidCore.getValueKey];
3495
3800
  return parser ? parser(value) : value;
3496
3801
  };
3497
- const filterResult = (q, returnType, queryResult, hookSelect, result) => {
3802
+ const filterResult = (q, returnType, queryResult, result, tempColumns, hasAfterHook) => {
3498
3803
  var _a;
3499
3804
  if (returnType === "all") {
3500
- const pick = getSelectPick(queryResult, hookSelect);
3501
- return result.map((full) => {
3502
- const filtered = {};
3503
- for (const key of pick) {
3504
- filtered[key] = full[key];
3505
- }
3506
- return filtered;
3507
- });
3805
+ return filterAllResult(result, tempColumns, hasAfterHook);
3508
3806
  }
3509
3807
  if (returnType === "oneOrThrow" || returnType === "one") {
3510
- const row = result[0];
3808
+ let row = result[0];
3511
3809
  if (!row) {
3512
3810
  if (returnType === "oneOrThrow")
3513
3811
  throw new NotFoundError(q);
3514
3812
  return void 0;
3813
+ } else if (!(tempColumns == null ? void 0 : tempColumns.size)) {
3814
+ return row;
3515
3815
  } else {
3516
- result = {};
3517
- for (const key in row) {
3518
- if (!hookSelect.includes(key)) {
3519
- result[key] = row[key];
3520
- }
3816
+ if (hasAfterHook)
3817
+ row = __spreadValues$b({}, row);
3818
+ for (const column of tempColumns) {
3819
+ delete row[column];
3521
3820
  }
3522
- return result;
3821
+ return row;
3523
3822
  }
3524
3823
  }
3525
3824
  if (returnType === "value") {
@@ -3539,20 +3838,30 @@ const filterResult = (q, returnType, queryResult, hookSelect, result) => {
3539
3838
  return result.map((row) => row[key]);
3540
3839
  }
3541
3840
  if (returnType === "rows") {
3542
- const pick = getSelectPick(queryResult, hookSelect);
3543
- return result.map(
3544
- (full) => pick.map((key) => full[key])
3545
- );
3841
+ result = filterAllResult(result, tempColumns, hasAfterHook);
3842
+ return result.map((record) => Object.values(record));
3546
3843
  }
3547
3844
  return;
3548
3845
  };
3549
- const getSelectPick = (queryResult, hookSelect) => {
3550
- const pick = [];
3551
- for (const field of queryResult.fields) {
3552
- if (!hookSelect.includes(field.name))
3553
- 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
+ }
3554
3863
  }
3555
- return pick;
3864
+ return result;
3556
3865
  };
3557
3866
 
3558
3867
  const addParserForRawExpression = (q, key, raw) => {
@@ -3560,58 +3869,220 @@ const addParserForRawExpression = (q, key, raw) => {
3560
3869
  if (type == null ? void 0 : type.parseFn)
3561
3870
  orchidCore.setParserToQuery(q.q, key, type.parseFn);
3562
3871
  };
3563
- const subQueryResult = {
3564
- // sub query can't return a rowCount, use -1 as for impossible case
3565
- rowCount: -1,
3566
- rows: orchidCore.emptyArray,
3567
- fields: orchidCore.emptyArray
3568
- };
3569
3872
  const addParsersForSelectJoined = (q, arg, as = arg) => {
3570
- var _a;
3873
+ var _a, _b, _c, _d;
3571
3874
  const parsers = (_a = q.q.joinedParsers) == null ? void 0 : _a[arg];
3572
3875
  if (parsers) {
3573
3876
  orchidCore.setParserToQuery(q.q, as, (row) => parseRecord(parsers, row));
3574
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
+ }
3575
3887
  };
3576
3888
  const addParserForSelectItem = (q, as, key, arg) => {
3889
+ var _a, _b, _c, _d;
3577
3890
  if (typeof arg === "object" || typeof arg === "function") {
3578
3891
  if (orchidCore.isExpression(arg)) {
3579
3892
  addParserForRawExpression(q, key, arg);
3580
3893
  } else {
3581
3894
  const { q: query } = arg;
3582
- if (query.parsers || query.transform) {
3583
- orchidCore.setParserToQuery(q.q, key, (item) => {
3584
- const t = query.returnType || "all";
3585
- subQueryResult.rows = t === "value" || t === "valueOrThrow" ? [[item]] : t === "one" || t === "oneOrThrow" ? [item] : item;
3586
- const result = query.handleResult(arg, t, subQueryResult, true);
3587
- return query.transform ? orchidCore.applyTransforms(t, query.transform, result) : result;
3588
- });
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
+ }
3589
3900
  }
3590
- if (query.returnType === "valueOrThrow" || query.returnType === "oneOrThrow") {
3591
- pushQueryValue(
3592
- q,
3593
- "transform",
3594
- (data) => {
3595
- if (Array.isArray(data)) {
3596
- for (const item of data) {
3597
- if (item[key] === void 0) {
3598
- 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
+ }
3599
3936
  }
3937
+ break;
3600
3938
  }
3601
- } else {
3602
- if (data[key] === void 0) {
3603
- 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;
4006
+ }
4007
+ }
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;
3604
4042
  }
4043
+ finalizeNestedHookSelect(
4044
+ batches,
4045
+ originalReturnType,
4046
+ tempColumns,
4047
+ renames,
4048
+ key
4049
+ );
3605
4050
  }
3606
- return data;
4051
+ applyBatchTransforms(query, batches);
4052
+ return;
3607
4053
  }
3608
- );
4054
+ });
3609
4055
  }
3610
4056
  }
3611
4057
  return arg;
3612
4058
  }
3613
4059
  return setParserForSelectedString(q, arg, as, key);
3614
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
+ };
3615
4086
  const emptyArrSQL = new RawSQL("'[]'");
3616
4087
  const processSelectArg = (q, as, arg, columnAs) => {
3617
4088
  var _a, _b, _c, _d;
@@ -3625,7 +4096,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
3625
4096
  value = resolveSubQueryCallback(q, value);
3626
4097
  if (isQueryNone(value)) {
3627
4098
  if (value.q.innerJoinLateral) {
3628
- return;
4099
+ return false;
3629
4100
  }
3630
4101
  } else if (!orchidCore.isExpression(value) && value.joinQuery) {
3631
4102
  value = value.joinQuery(value, q);
@@ -3635,17 +4106,23 @@ const processSelectArg = (q, as, arg, columnAs) => {
3635
4106
  query = value.json(false);
3636
4107
  value.q.coalesceValue = emptyArrSQL;
3637
4108
  } else if (returnType === "pluck") {
3638
- 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);
3639
4110
  value.q.coalesceValue = emptyArrSQL;
3640
4111
  } else {
3641
- if ((returnType === "value" || returnType === "valueOrThrow") && value.q.select) {
3642
- if (typeof value.q.select[0] === "string") {
3643
- value.q.select[0] = {
3644
- selectAs: { r: value.q.select[0] }
3645
- };
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);
3646
4122
  }
4123
+ } else {
4124
+ query = value;
3647
4125
  }
3648
- query = value;
3649
4126
  }
3650
4127
  let asOverride = key;
3651
4128
  if ((_a = value.q.joinedShapes) == null ? void 0 : _a[key]) {
@@ -3679,7 +4156,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
3679
4156
  return { selectAs };
3680
4157
  };
3681
4158
  const setParserForSelectedString = (q, arg, as, columnAs) => {
3682
- var _a, _b, _c, _d;
4159
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
3683
4160
  const index = arg.indexOf(".");
3684
4161
  if (index !== -1) {
3685
4162
  const table = arg.slice(0, index);
@@ -3687,25 +4164,58 @@ const setParserForSelectedString = (q, arg, as, columnAs) => {
3687
4164
  if (column === "*") {
3688
4165
  addParsersForSelectJoined(q, table, columnAs);
3689
4166
  return table === as ? column : arg;
3690
- } else {
3691
- if (table === as) {
4167
+ } else if (table === as) {
4168
+ if (columnAs) {
3692
4169
  const parser = (_a = q.q.parsers) == null ? void 0 : _a[column];
3693
4170
  if (parser)
3694
- orchidCore.setParserToQuery(q.q, columnAs || column, parser);
3695
- return column;
3696
- } else {
3697
- const parser = (_c = (_b = q.q.joinedParsers) == null ? void 0 : _b[table]) == null ? void 0 : _c[column];
3698
- if (parser)
3699
- orchidCore.setParserToQuery(q.q, columnAs || column, parser);
3700
- return arg;
4171
+ q.q.parsers[columnAs] = parser;
3701
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;
3702
4197
  }
3703
4198
  } else {
3704
- const parser = (_d = q.q.parsers) == null ? void 0 : _d[arg];
3705
- if (parser)
3706
- orchidCore.setParserToQuery(q.q, columnAs || arg, parser);
3707
- 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);
4205
+ }
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;
3708
4217
  }
4218
+ return column;
3709
4219
  };
3710
4220
  const getShapeFromSelect = (q, isSubQuery) => {
3711
4221
  const query = q.q;
@@ -3726,7 +4236,9 @@ const getShapeFromSelect = (q, isSubQuery) => {
3726
4236
  for (const item of select) {
3727
4237
  if (typeof item === "string") {
3728
4238
  addColumnToShapeFromSelect(q, item, shape, query, result, isSubQuery);
3729
- } else if ("selectAs" in item) {
4239
+ } else if (orchidCore.isExpression(item)) {
4240
+ result.value = item.result.value;
4241
+ } else if (item && "selectAs" in item) {
3730
4242
  for (const key in item.selectAs) {
3731
4243
  const it = item.selectAs[key];
3732
4244
  if (typeof it === "string") {
@@ -3741,7 +4253,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
3741
4253
  );
3742
4254
  } else if (orchidCore.isExpression(it)) {
3743
4255
  result[key] = it.result.value;
3744
- } else {
4256
+ } else if (it) {
3745
4257
  const { returnType } = it.q;
3746
4258
  if (returnType === "value" || returnType === "valueOrThrow") {
3747
4259
  const type = it.q[orchidCore.getValueKey];
@@ -3752,8 +4264,6 @@ const getShapeFromSelect = (q, isSubQuery) => {
3752
4264
  }
3753
4265
  }
3754
4266
  }
3755
- } else if (orchidCore.isExpression(item)) {
3756
- result.value = item.result.value;
3757
4267
  }
3758
4268
  }
3759
4269
  }
@@ -3789,12 +4299,13 @@ function _querySelect(q, args) {
3789
4299
  return q;
3790
4300
  }
3791
4301
  const as = q.q.as || q.table;
3792
- const selectArgs = new Array(len);
3793
- for (let i = 0; i < len; i++) {
3794
- selectArgs[i] = processSelectArg(q, as, args[i]);
3795
- 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)
3796
4308
  return _queryNone(q);
3797
- }
3798
4309
  }
3799
4310
  return pushQueryArray(q, "select", selectArgs);
3800
4311
  }
@@ -3864,20 +4375,25 @@ const _get = (query, returnType, arg) => {
3864
4375
  }
3865
4376
  }
3866
4377
  q[orchidCore.getValueKey] = type;
3867
- setParserForSelectedString(
4378
+ const selected = setParserForSelectedString(
3868
4379
  query,
3869
4380
  arg,
3870
4381
  getQueryAs(query),
3871
4382
  orchidCore.getValueKey
3872
4383
  );
3873
- 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;
3874
4391
  } else {
3875
4392
  type = arg.result.value;
3876
4393
  q[orchidCore.getValueKey] = type;
3877
4394
  addParserForRawExpression(query, orchidCore.getValueKey, arg);
3878
- q.expr = arg;
4395
+ q.select = [q.expr = arg];
3879
4396
  }
3880
- q.select = [q.expr];
3881
4397
  return setQueryOperators(
3882
4398
  query,
3883
4399
  (type == null ? void 0 : type.operators) || Operators.any
@@ -3913,23 +4429,27 @@ class AsMethods {
3913
4429
  }
3914
4430
 
3915
4431
  function queryFrom(self, arg) {
3916
- var _a, _b;
4432
+ var _a, _b, _c, _d;
3917
4433
  const data = self.q;
3918
4434
  if (typeof arg === "string") {
3919
4435
  data.as || (data.as = arg);
3920
- 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;
3921
4439
  } else if (orchidCore.isExpression(arg)) {
3922
4440
  data.as || (data.as = "t");
3923
4441
  } else if (Array.isArray(arg)) {
3924
4442
  const { shape } = data;
3925
- 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 = {};
3926
4445
  for (const item of arg) {
3927
4446
  if (typeof item === "string") {
3928
- const withShape = data.withShapes[item];
3929
- Object.assign(shape, withShape);
3930
- for (const key in withShape) {
3931
- if (withShape[key].parseFn) {
3932
- 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;
3933
4453
  }
3934
4454
  }
3935
4455
  } else if (!orchidCore.isExpression(item)) {
@@ -3942,6 +4462,7 @@ function queryFrom(self, arg) {
3942
4462
  data.as || (data.as = q.q.as || q.table || "t");
3943
4463
  data.shape = getShapeFromSelect(arg, true);
3944
4464
  data.parsers = q.q.parsers;
4465
+ data.batchParsers = q.q.batchParsers;
3945
4466
  }
3946
4467
  data.from = arg;
3947
4468
  return self;
@@ -4051,44 +4572,117 @@ function queryJson(self, coalesce) {
4051
4572
  const pushSelectSql = (ctx, table, query, quotedAs) => {
4052
4573
  ctx.sql.push(selectToSql(ctx, table, query, quotedAs));
4053
4574
  };
4054
- 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 = [];
4055
4579
  if (query.select) {
4056
- const list = [];
4057
4580
  for (const item of query.select) {
4058
4581
  if (typeof item === "string") {
4059
- list.push(selectedStringToSQL(ctx, table, query, quotedAs, item));
4060
- } else if ("selectAs" in item) {
4061
- const obj = item.selectAs;
4062
- for (const as in obj) {
4063
- const value = obj[as];
4064
- if (typeof value === "object" || typeof value === "function") {
4065
- if (orchidCore.isExpression(value)) {
4066
- list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
4067
- } else {
4068
- 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;
4069
4588
  }
4070
- } else {
4071
- list.push(
4072
- `${columnToSql(
4073
- ctx,
4074
- table.q,
4075
- table.q.shape,
4076
- value,
4077
- quotedAs,
4078
- true
4079
- )} "${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
4080
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
+ }
4081
4639
  }
4640
+ } else {
4641
+ list.push(selectedObjectToSQL(ctx, quotedAs, item));
4082
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}"`;
4083
4661
  } else {
4084
- list.push(selectedObjectToSQL(ctx, quotedAs, item));
4662
+ quotedTable = quotedAs;
4663
+ columnName = select;
4664
+ col = query.shape[select];
4665
+ sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4085
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);
4086
4682
  }
4087
- return list.join(", ");
4088
4683
  }
4089
- return selectAllSql(table, query, quotedAs);
4684
+ return list.length ? list.join(", ") : selectAllSql(table, query, quotedAs);
4090
4685
  };
4091
- const selectedStringToSQL = (ctx, table, query, quotedAs, item) => item === "*" ? selectAllSql(table, query, quotedAs) : columnToSqlWithAs(ctx, table.q, item, quotedAs, true);
4092
4686
  function selectedObjectToSQL(ctx, quotedAs, item) {
4093
4687
  const sql = item.toSQL(ctx, quotedAs);
4094
4688
  return ctx.aliasValue ? `${sql} r` : sql;
@@ -4098,7 +4692,7 @@ const selectAllSql = (table, query, quotedAs) => {
4098
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(", ")) || "*";
4099
4693
  };
4100
4694
  const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
4101
- var _a;
4695
+ var _a, _b, _c;
4102
4696
  const { returnType = "all" } = query.q;
4103
4697
  if (isQueryNone(query)) {
4104
4698
  let sql;
@@ -4162,20 +4756,27 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
4162
4756
  case "pluck": {
4163
4757
  const { select } = query.q;
4164
4758
  const first = select == null ? void 0 : select[0];
4165
- if (!select || !first) {
4759
+ if (!first && ((_b = query.q.computeds) == null ? void 0 : _b[as])) {
4760
+ query = queryJson(query);
4761
+ } else if (!first) {
4166
4762
  throw new OrchidOrmInternalError(
4167
4763
  query,
4168
4764
  `Nothing was selected for pluck`
4169
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"), '[]')`));
4170
4771
  }
4171
- const cloned = query.clone();
4172
- cloned.q.select = [{ selectAs: { c: first } }];
4173
- query = queryWrap(cloned, cloned.baseQuery.clone());
4174
- _queryGetOptional(query, new RawSQL(`COALESCE(json_agg("c"), '[]')`));
4175
4772
  break;
4176
4773
  }
4177
4774
  case "value":
4178
4775
  case "valueOrThrow":
4776
+ if ((_c = query.q.computeds) == null ? void 0 : _c[as]) {
4777
+ query = queryJson(query);
4778
+ }
4779
+ break;
4179
4780
  case "rows":
4180
4781
  case "rowCount":
4181
4782
  case "void":
@@ -4677,42 +5278,22 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs) =
4677
5278
  };
4678
5279
  const pushReturningSql = (ctx, q, data, quotedAs, hookSelect, keyword = "RETURNING") => {
4679
5280
  const { select } = data;
4680
- if (!(hookSelect == null ? void 0 : hookSelect.length) && !select)
4681
- return hookSelect;
4682
- let selected;
4683
- let hookFiltered;
4684
- if (select) {
4685
- 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);
4686
5286
  if (hookSelect) {
4687
- if (select.includes("*")) {
4688
- hookFiltered = orchidCore.emptyArray;
4689
- } else {
4690
- hookFiltered = [];
4691
- for (const column of hookSelect) {
4692
- if (!hookFiltered.includes(column) && !(select == null ? void 0 : select.includes(column)) && !(select == null ? void 0 : select.includes(`${quotedAs}.${column}`))) {
4693
- hookFiltered.push(column);
4694
- }
4695
- }
5287
+ for (const column of hookSelect) {
5288
+ tempSelect.set(column, { select: column });
4696
5289
  }
4697
5290
  }
4698
- } else {
4699
- hookFiltered = [];
4700
- for (const column of hookSelect) {
4701
- if (!hookFiltered.includes(column))
4702
- hookFiltered.push(column);
4703
- }
4704
- }
4705
- ctx.sql.push(keyword);
4706
- if (hookFiltered == null ? void 0 : hookFiltered.length) {
4707
- if (selected)
4708
- ctx.sql.push(`${selected},`);
4709
- ctx.sql.push(
4710
- hookFiltered.map((column) => ownColumnToSql(data, column, quotedAs)).join(", ")
4711
- );
4712
- } else {
4713
- ctx.sql.push(selected);
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));
4714
5295
  }
4715
- return hookFiltered;
5296
+ return;
4716
5297
  };
4717
5298
 
4718
5299
  const pushUpdateSql = (ctx, table, query, quotedAs) => {
@@ -5078,7 +5659,7 @@ const makeSQL = (table, options) => {
5078
5659
  if (query.for.mode)
5079
5660
  sql.push(query.for.mode);
5080
5661
  }
5081
- return { text: sql.join(" "), values };
5662
+ return { text: sql.join(" "), values, hookSelect: query.hookSelect };
5082
5663
  };
5083
5664
  function pushLimitSQL(sql, values, q) {
5084
5665
  if (!q.returnsOne) {
@@ -5111,6 +5692,8 @@ const cloneQuery = (q) => {
5111
5692
  q.with = q.with.slice(0);
5112
5693
  if (q.select)
5113
5694
  q.select = q.select.slice(0);
5695
+ if (q.hookSelect)
5696
+ q.hookSelect = new Map(q.hookSelect);
5114
5697
  if (q.and)
5115
5698
  q.and = q.and.slice(0);
5116
5699
  if (q.or)
@@ -5123,6 +5706,8 @@ const cloneQuery = (q) => {
5123
5706
  q.joinedShapes = __spreadValues$a({}, q.joinedShapes);
5124
5707
  if (q.scopes)
5125
5708
  q.scopes = __spreadValues$a({}, q.scopes);
5709
+ if (q.parsers)
5710
+ q.parsers = __spreadValues$a({}, q.parsers);
5126
5711
  if (q.updateData) {
5127
5712
  q.updateData = q.updateData.slice(
5128
5713
  0
@@ -5155,7 +5740,7 @@ const cloneQuery = (q) => {
5155
5740
  if (q.afterCreate) {
5156
5741
  q.afterCreate = q.afterCreate.slice(0);
5157
5742
  if (q.afterCreateSelect) {
5158
- q.afterCreateSelect = q.afterCreateSelect.slice(0);
5743
+ q.afterCreateSelect = new Set(q.afterCreateSelect);
5159
5744
  }
5160
5745
  }
5161
5746
  } else if (q.type === "update") {
@@ -5164,7 +5749,7 @@ const cloneQuery = (q) => {
5164
5749
  if (q.afterUpdate) {
5165
5750
  q.afterUpdate = q.afterUpdate.slice(0);
5166
5751
  if (q.afterUpdateSelect) {
5167
- q.afterUpdateSelect = q.afterUpdateSelect.slice(0);
5752
+ q.afterUpdateSelect = new Set(q.afterUpdateSelect);
5168
5753
  }
5169
5754
  }
5170
5755
  } else if (q.type === "delete") {
@@ -5173,7 +5758,7 @@ const cloneQuery = (q) => {
5173
5758
  if (q.afterDelete) {
5174
5759
  q.afterDelete = q.afterDelete.slice(0);
5175
5760
  if (q.afterDeleteSelect) {
5176
- q.afterDeleteSelect = q.afterDeleteSelect.slice(0);
5761
+ q.afterDeleteSelect = new Set(q.afterDeleteSelect);
5177
5762
  }
5178
5763
  }
5179
5764
  }
@@ -5198,8 +5783,6 @@ var __spreadValues$9 = (a, b) => {
5198
5783
  const getClonedQueryData = (query) => {
5199
5784
  const cloned = __spreadValues$9({}, query);
5200
5785
  delete cloned[toSQLCacheKey];
5201
- if (cloned.parsers)
5202
- cloned.parsers = __spreadValues$9({}, cloned.parsers);
5203
5786
  cloneQuery(cloned);
5204
5787
  return cloned;
5205
5788
  };
@@ -5310,7 +5893,6 @@ const makeColumnTypes = (schema) => {
5310
5893
  bigSerial: schema.bigSerial,
5311
5894
  money: schema.money,
5312
5895
  varchar: schema.varchar,
5313
- char: schema.char,
5314
5896
  text: schema.text,
5315
5897
  string: schema.string,
5316
5898
  citext: schema.citext,
@@ -5405,12 +5987,14 @@ class VirtualColumn extends ColumnType {
5405
5987
  }
5406
5988
  }
5407
5989
 
5408
- class UnknownColumn extends VirtualColumn {
5990
+ const _UnknownColumn = class extends VirtualColumn {
5409
5991
  constructor(schema) {
5410
5992
  super(schema, schema.unknown());
5411
5993
  }
5412
- }
5413
- 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 };
5414
5998
 
5415
5999
  const makeColumnsByType = (schema) => {
5416
6000
  const t = makeColumnTypes(schema);
@@ -5418,16 +6002,13 @@ const makeColumnsByType = (schema) => {
5418
6002
  bool: t.boolean,
5419
6003
  boolean: t.boolean,
5420
6004
  bytea: t.bytea,
5421
- char: t.char,
5422
6005
  int8: t.bigint,
5423
6006
  bigint: t.bigint,
5424
6007
  int2: t.smallint,
5425
6008
  smallint: t.smallint,
5426
6009
  int4: t.integer,
5427
6010
  integer: t.integer,
5428
- text() {
5429
- return t.text(0, Infinity);
5430
- },
6011
+ text: t.text,
5431
6012
  json: t.jsonText,
5432
6013
  xml: t.xml,
5433
6014
  point: t.point,
@@ -5446,8 +6027,6 @@ const makeColumnsByType = (schema) => {
5446
6027
  money: t.money,
5447
6028
  macaddr: t.macaddr,
5448
6029
  inet: t.inet,
5449
- bpchar: t.char,
5450
- character: t.char,
5451
6030
  varchar: t.varchar,
5452
6031
  "character varying": t.varchar,
5453
6032
  date: t.date,
@@ -6464,7 +7043,7 @@ const insert = (self, {
6464
7043
  q.values = values;
6465
7044
  if (!q.kind)
6466
7045
  q.kind = kind;
6467
- const { select, returnType = "all" } = q;
7046
+ const { select, returnType } = q;
6468
7047
  if (!select) {
6469
7048
  if (returnType !== "void")
6470
7049
  q.returnType = "rowCount";
@@ -6474,7 +7053,7 @@ const insert = (self, {
6474
7053
  } else if (returnType === "value" || returnType === "valueOrThrow") {
6475
7054
  q.returnType = "pluck";
6476
7055
  }
6477
- } else if (returnType === "all") {
7056
+ } else if (!returnType || returnType === "all") {
6478
7057
  q.returnType = "from" in values ? values.from.q.returnType : "one";
6479
7058
  } else if (returnType === "pluck") {
6480
7059
  q.returnType = "valueOrThrow";
@@ -6493,7 +7072,7 @@ const getFromSelectColumns = (from, obj, many) => {
6493
7072
  if (typeof item === "string") {
6494
7073
  const index = item.indexOf(".");
6495
7074
  queryColumns.push(index === -1 ? item : item.slice(index + 1));
6496
- } else if ("selectAs" in item) {
7075
+ } else if (item && "selectAs" in item) {
6497
7076
  queryColumns.push(...Object.keys(item.selectAs));
6498
7077
  }
6499
7078
  });
@@ -6537,12 +7116,7 @@ const _queryCreateMany = (q, data) => {
6537
7116
  };
6538
7117
  const _queryInsertMany = (q, data) => {
6539
7118
  const ctx = createCtx();
6540
- let result = insert(
6541
- q,
6542
- handleManyData(q, data, ctx),
6543
- "object",
6544
- true
6545
- );
7119
+ let result = insert(q, handleManyData(q, data, ctx), "object", true);
6546
7120
  if (!data.length)
6547
7121
  result = result.none();
6548
7122
  return result;
@@ -7355,11 +7929,15 @@ class Having {
7355
7929
  }
7356
7930
 
7357
7931
  const before = (q, key, cb) => pushQueryValue(q, `before${key}`, cb);
7358
- const after = (q, key, select, cb, commit) => pushQueryArray(
7359
- pushQueryValue(q, `after${key}${commit ? "Commit" : ""}`, cb),
7360
- `after${key}Select`,
7361
- select
7362
- );
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
+ };
7363
7941
  const _queryHookBeforeQuery = (q, cb) => {
7364
7942
  return pushQueryValue(q, "before", cb);
7365
7943
  };
@@ -8420,7 +8998,9 @@ const mergableObjects = {
8420
8998
  parsers: true,
8421
8999
  defaults: true,
8422
9000
  joinedShapes: true,
8423
- joinedParsers: true
9001
+ joinedParsers: true,
9002
+ joinedBatchParsers: true,
9003
+ selectedComputeds: true
8424
9004
  };
8425
9005
  class MergeQueryMethods {
8426
9006
  merge(q) {
@@ -8445,6 +9025,8 @@ class MergeQueryMethods {
8445
9025
  b: a[key].b,
8446
9026
  u: [...a[key].u, ...value.u]
8447
9027
  } : value;
9028
+ } else if (value instanceof Set) {
9029
+ a[key] = a[key] ? /* @__PURE__ */ new Set([...a[key], ...value]) : value;
8448
9030
  } else {
8449
9031
  a[key] = value;
8450
9032
  }
@@ -8639,7 +9221,10 @@ class WithMethods {
8639
9221
  }
8640
9222
  pushQueryValue(q, "with", { n: name, o: options, q: query });
8641
9223
  const shape = getShapeFromSelect(query, true);
8642
- return setQueryObjectValue(q, "withShapes", name, shape);
9224
+ return setQueryObjectValue(q, "withShapes", name, {
9225
+ shape,
9226
+ computeds: query.q.computeds
9227
+ });
8643
9228
  }
8644
9229
  withRecursive(name, ...args) {
8645
9230
  var _a, _b, _c;
@@ -8648,10 +9233,9 @@ class WithMethods {
8648
9233
  const arg = q.queryBuilder.clone();
8649
9234
  arg.q.withShapes = q.q.withShapes;
8650
9235
  let query = typeof baseFn === "function" ? baseFn(arg) : baseFn;
8651
- const shape = ((_b = (_a = arg.q).withShapes) != null ? _b : _a.withShapes = {})[name] = getShapeFromSelect(
8652
- query,
8653
- true
8654
- );
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;
8655
9239
  const recursive = recursiveFn(arg);
8656
9240
  query = _queryUnion(query, [recursive], (_c = options.union) != null ? _c : "UNION ALL");
8657
9241
  options.recursive = true;
@@ -8661,7 +9245,7 @@ class WithMethods {
8661
9245
  });
8662
9246
  }
8663
9247
  pushQueryValue(q, "with", { n: name, o: options, q: query });
8664
- return setQueryObjectValue(q, "withShapes", name, shape);
9248
+ return setQueryObjectValue(q, "withShapes", name, withConfig);
8665
9249
  }
8666
9250
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8667
9251
  withSql(name, ...args) {
@@ -8672,12 +9256,9 @@ class WithMethods {
8672
9256
  o: options,
8673
9257
  s: sql(q)
8674
9258
  });
8675
- return setQueryObjectValue(
8676
- q,
8677
- "withShapes",
8678
- name,
8679
- shape(this.columnTypes)
8680
- );
9259
+ return setQueryObjectValue(q, "withShapes", name, {
9260
+ shape: shape(this.columnTypes)
9261
+ });
8681
9262
  }
8682
9263
  }
8683
9264
 
@@ -9109,7 +9690,7 @@ class Where {
9109
9690
  *
9110
9691
  * ### Text column operators
9111
9692
  *
9112
- * For `text`, `char`, `varchar`, and `json` columns.
9693
+ * For `text`, `varchar`, `string`, and `json` columns.
9113
9694
  *
9114
9695
  * `json` is stored as text, so it has text operators. Use the `jsonb` type for JSON operators.
9115
9696
  *
@@ -10195,12 +10776,9 @@ function orCreate(q, data, updateData, mergeData) {
10195
10776
  if (mergeData)
10196
10777
  data = __spreadValues$1(__spreadValues$1({}, mergeData), data);
10197
10778
  const inner = q2.create(data);
10198
- const { handleResult: handleResult2 } = inner.q;
10199
10779
  inner.q.handleResult = (q3, t, r, s) => {
10200
- queryResult = r;
10201
- const res = handleResult2(q3, t, r, s);
10202
- result = res;
10203
- return res;
10780
+ result = handleResult(q3, t, r, s);
10781
+ return inner.q.hookSelect ? result.map((row) => __spreadValues$1({}, row)) : result;
10204
10782
  };
10205
10783
  await inner;
10206
10784
  created = true;
@@ -10848,8 +11426,13 @@ class QueryMethods {
10848
11426
  pluck(select) {
10849
11427
  const q = this.clone();
10850
11428
  q.q.returnType = "pluck";
10851
- q.q.select = [select];
10852
- 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;
10853
11436
  return q;
10854
11437
  }
10855
11438
  /**
@@ -11601,6 +12184,7 @@ class Db {
11601
12184
  tableData
11602
12185
  };
11603
12186
  this.baseQuery = this;
12187
+ this.relations = {};
11604
12188
  const logger = options.logger || console;
11605
12189
  const parsers = {};
11606
12190
  let hasParsers = false;
@@ -11651,6 +12235,7 @@ class Db {
11651
12235
  );
11652
12236
  }
11653
12237
  this.internal.columnsForSelectAll = list;
12238
+ this.internal.columnsKeysForSelectAll = __spreadValues({}, shape);
11654
12239
  }
11655
12240
  this.q = {
11656
12241
  adapter,
@@ -11689,6 +12274,8 @@ class Db {
11689
12274
  this.defaultSelectColumns = columns.filter(
11690
12275
  (column) => !shape[column].data.isHidden
11691
12276
  );
12277
+ if (options.computed)
12278
+ applyComputedColumns(this, options.computed);
11692
12279
  const defaultSelect = this.defaultSelectColumns.length === columns.length ? void 0 : this.defaultSelectColumns;
11693
12280
  this.toSQL = defaultSelect ? function(options2) {
11694
12281
  const q = this.clone();
@@ -11697,7 +12284,6 @@ class Db {
11697
12284
  }
11698
12285
  return toSQL.call(q, options2);
11699
12286
  } : toSQL;
11700
- this.relations = {};
11701
12287
  if (modifyQuery) {
11702
12288
  for (const cb of modifyQuery) {
11703
12289
  cb(this);
@@ -11995,16 +12581,6 @@ const testTransaction = {
11995
12581
  }
11996
12582
  };
11997
12583
 
11998
- function addComputedColumns(q, computed) {
11999
- const { shape } = q;
12000
- for (const key in computed) {
12001
- const expr = computed[key](q);
12002
- shape[key] = expr.result.value;
12003
- expr.result.value.data.computed = expr;
12004
- }
12005
- return q;
12006
- }
12007
-
12008
12584
  const rowToColumnInfo = (row) => {
12009
12585
  const typed = row;
12010
12586
  return {
@@ -12055,13 +12631,13 @@ exports.BitVaryingColumn = BitVaryingColumn;
12055
12631
  exports.BooleanColumn = BooleanColumn;
12056
12632
  exports.BoxColumn = BoxColumn;
12057
12633
  exports.ByteaColumn = ByteaColumn;
12058
- exports.CharColumn = CharColumn;
12059
12634
  exports.CidrColumn = CidrColumn;
12060
12635
  exports.CircleColumn = CircleColumn;
12061
12636
  exports.CitextColumn = CitextColumn;
12062
12637
  exports.Clear = Clear;
12063
12638
  exports.ColumnRefExpression = ColumnRefExpression;
12064
12639
  exports.ColumnType = ColumnType;
12640
+ exports.ComputedColumn = ComputedColumn;
12065
12641
  exports.Create = Create;
12066
12642
  exports.CustomTypeColumn = CustomTypeColumn;
12067
12643
  exports.DateBaseColumn = DateBaseColumn;
@@ -12202,11 +12778,11 @@ exports._queryWhereIn = _queryWhereIn;
12202
12778
  exports._queryWhereNot = _queryWhereNot;
12203
12779
  exports._queryWhereNotSql = _queryWhereNotSql;
12204
12780
  exports._queryWhereSql = _queryWhereSql;
12205
- exports.addComputedColumns = addComputedColumns;
12206
12781
  exports.addParserForRawExpression = addParserForRawExpression;
12207
12782
  exports.addParserForSelectItem = addParserForSelectItem;
12208
12783
  exports.addQueryOn = addQueryOn;
12209
12784
  exports.anyShape = anyShape;
12785
+ exports.applyComputedColumns = applyComputedColumns;
12210
12786
  exports.checkIfASimpleQuery = checkIfASimpleQuery;
12211
12787
  exports.cloneQuery = cloneQuery;
12212
12788
  exports.cloneQueryBaseUnscoped = cloneQueryBaseUnscoped;
@@ -12223,6 +12799,7 @@ exports.countSelect = countSelect;
12223
12799
  exports.createDb = createDb;
12224
12800
  exports.defaultSchemaConfig = defaultSchemaConfig;
12225
12801
  exports.extendQuery = extendQuery;
12802
+ exports.filterResult = filterResult;
12226
12803
  exports.foreignKeyArgumentToCode = foreignKeyArgumentToCode;
12227
12804
  exports.getClonedQueryData = getClonedQueryData;
12228
12805
  exports.getColumnInfo = getColumnInfo;
@@ -12248,10 +12825,11 @@ exports.makeFnExpression = makeFnExpression;
12248
12825
  exports.makeRegexToFindInSql = makeRegexToFindInSql;
12249
12826
  exports.makeSQL = makeSQL;
12250
12827
  exports.parseRecord = parseRecord;
12251
- exports.parseResult = parseResult;
12252
12828
  exports.parseTableData = parseTableData;
12253
12829
  exports.parseTableDataInput = parseTableDataInput;
12254
12830
  exports.primaryKeyInnerToCode = primaryKeyInnerToCode;
12831
+ exports.processComputedBatches = processComputedBatches;
12832
+ exports.processComputedResult = processComputedResult;
12255
12833
  exports.processSelectArg = processSelectArg;
12256
12834
  exports.pushLimitSQL = pushLimitSQL;
12257
12835
  exports.pushQueryArray = pushQueryArray;