pqb 0.51.0 → 0.51.2

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
@@ -448,8 +448,7 @@ const simplifyColumnDefault = (value) => {
448
448
  }
449
449
  return;
450
450
  };
451
- const instantiateColumn = (typeFn, params) => {
452
- const column = typeFn();
451
+ const assignDbDataToColumn = (column, params) => {
453
452
  const { dateTimePrecision } = params;
454
453
  Object.assign(column.data, {
455
454
  ...params,
@@ -2001,18 +2000,14 @@ const getFullColumnTable = (q, column, index, as) => {
2001
2000
  return as && table !== as && q.q.aliases?.[table] === as ? as : table;
2002
2001
  };
2003
2002
 
2004
- const applySqlComputed = (ctx, q, computed, as, quotedAs) => {
2005
- addColumnParserToQuery(q, as, computed.result.value);
2006
- return computed.toSQL(ctx, quotedAs);
2007
- };
2008
- function simpleColumnToSQL(ctx, q, key, column, quotedAs) {
2003
+ function simpleColumnToSQL(ctx, key, column, quotedAs) {
2009
2004
  if (!column) return `"${key}"`;
2010
2005
  const { data } = column;
2011
- return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
2006
+ return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
2012
2007
  }
2013
- function simpleExistingColumnToSQL(ctx, q, key, column, quotedAs) {
2008
+ function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
2014
2009
  const { data } = column;
2015
- return data.computed ? applySqlComputed(ctx, q, data.computed, key, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
2010
+ return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
2016
2011
  }
2017
2012
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
2018
2013
  const index = column.indexOf(".");
@@ -2030,7 +2025,7 @@ const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
2030
2025
  if (!select && data.joinedShapes?.[column]) {
2031
2026
  return `"${column}".r`;
2032
2027
  }
2033
- return simpleColumnToSQL(ctx, data, column, shape[column], quotedAs);
2028
+ return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
2034
2029
  };
2035
2030
  const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
2036
2031
  const index = column.indexOf(".");
@@ -2044,12 +2039,12 @@ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
2044
2039
  for (const s of data.select) {
2045
2040
  if (typeof s === "object" && "selectAs" in s) {
2046
2041
  if (column in s.selectAs) {
2047
- return simpleColumnToSQL(ctx, data, column, data.shape[column]);
2042
+ return simpleColumnToSQL(ctx, column, data.shape[column]);
2048
2043
  }
2049
2044
  }
2050
2045
  }
2051
2046
  }
2052
- return simpleColumnToSQL(ctx, data, column, data.shape[column], quotedAs);
2047
+ return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
2053
2048
  }
2054
2049
  };
2055
2050
  const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
@@ -2067,7 +2062,7 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
2067
2062
  return `"${tableName}"."${col.data.name}"`;
2068
2063
  }
2069
2064
  if (col.data.computed) {
2070
- return applySqlComputed(ctx, data, col.data.computed, column, quoted);
2065
+ return col.data.computed.toSQL(ctx, quoted);
2071
2066
  }
2072
2067
  return `"${tableName}"."${key}"`;
2073
2068
  }
@@ -2107,13 +2102,7 @@ const tableColumnToSqlWithAs = (ctx, data, column, table, key, as, quotedAs, sel
2107
2102
  return `"${tableName}"."${col.data.name}" "${as}"`;
2108
2103
  }
2109
2104
  if (col.data.computed) {
2110
- return `${applySqlComputed(
2111
- ctx,
2112
- data,
2113
- col.data.computed,
2114
- as,
2115
- quoted
2116
- )} "${as}"`;
2105
+ return `${col.data.computed.toSQL(ctx, quoted)} "${as}"`;
2117
2106
  }
2118
2107
  }
2119
2108
  return `"${tableName}"."${key}"${key === as ? "" : ` "${as}"`}`;
@@ -2126,13 +2115,7 @@ const ownColumnToSqlWithAs = (ctx, data, column, as, quotedAs, select, jsonList)
2126
2115
  return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}"${col.data.name === as ? "" : ` "${as}"`}`;
2127
2116
  }
2128
2117
  if (col.data.computed) {
2129
- return `${applySqlComputed(
2130
- ctx,
2131
- data,
2132
- col.data.computed,
2133
- as,
2134
- quotedAs
2135
- )} "${as}"`;
2118
+ return `${col.data.computed.toSQL(ctx, quotedAs)} "${as}"`;
2136
2119
  }
2137
2120
  }
2138
2121
  return `${quotedAs ? `${quotedAs}.` : ""}"${column}"${column === as ? "" : ` "${as}"`}`;
@@ -2148,7 +2131,7 @@ const makeRowToJson = (table, shape, aliasName) => {
2148
2131
  const list = [];
2149
2132
  for (const key in shape) {
2150
2133
  const column = shape[key];
2151
- if (column.data.explicitSelect || column instanceof VirtualColumn) {
2134
+ if (column.data.explicitSelect) {
2152
2135
  continue;
2153
2136
  }
2154
2137
  if (aliasName && column.data.name || column.data.jsonCast) {
@@ -3072,17 +3055,31 @@ const applyComputedColumns = (q, fn) => {
3072
3055
  q.computeBatchAtRuntime = computeBatchAtRuntime;
3073
3056
  const computed = fn(q);
3074
3057
  for (const key in computed) {
3075
- const item = computed[key];
3058
+ let item = computed[key];
3059
+ if (typeof item === "function") item = item.call(computed);
3076
3060
  if (item instanceof ComputedColumn) {
3077
3061
  q.q.computeds = {
3078
3062
  ...q.q.computeds,
3079
3063
  [key]: item
3080
3064
  };
3081
3065
  } else {
3082
- const data = (q.shape[key] = item.result.value || UnknownColumn.instance).data;
3066
+ let col = item.result.value;
3067
+ if (!col) {
3068
+ item.result.value = col = Object.create(
3069
+ UnknownColumn.instance
3070
+ );
3071
+ col.data = { ...col.data };
3072
+ }
3073
+ q.shape[key] = col;
3074
+ const { data } = col;
3083
3075
  data.computed = item;
3084
3076
  data.explicitSelect = true;
3085
3077
  data.readonly = true;
3078
+ addColumnParserToQuery(
3079
+ q.q,
3080
+ key,
3081
+ item.result.value
3082
+ );
3086
3083
  }
3087
3084
  }
3088
3085
  q.computeAtRuntime = q.computeBatchAtRuntime = void 0;
@@ -3372,9 +3369,13 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3372
3369
  let renames;
3373
3370
  if (hookSelect) {
3374
3371
  for (const column of hookSelect.keys()) {
3375
- const as = hookSelect.get(column).as;
3376
- if (as) (renames ?? (renames = {}))[column] = as;
3377
- (tempColumns ?? (tempColumns = /* @__PURE__ */ new Set()))?.add(as || column);
3372
+ const { as, temp } = hookSelect.get(column);
3373
+ if (as) {
3374
+ (renames ?? (renames = {}))[column] = as;
3375
+ }
3376
+ if (temp) {
3377
+ (tempColumns ?? (tempColumns = /* @__PURE__ */ new Set()))?.add(temp);
3378
+ }
3378
3379
  }
3379
3380
  if (renames) {
3380
3381
  for (const record of result) {
@@ -3441,9 +3442,11 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3441
3442
  }
3442
3443
  if (hookSelect || tempReturnType !== returnType) {
3443
3444
  if (renames) {
3444
- for (const record of result) {
3445
- for (const a in renames) {
3445
+ for (const a in renames) {
3446
+ for (const record of result) {
3447
+ const value = record[a];
3446
3448
  record[a] = record[renames[a]];
3449
+ record[renames[a]] = value;
3447
3450
  }
3448
3451
  }
3449
3452
  }
@@ -3486,7 +3489,8 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3486
3489
  if (log && sql) {
3487
3490
  log.onError(error, sql, logData);
3488
3491
  }
3489
- return reject?.(error);
3492
+ if (reject) return reject(error);
3493
+ throw error;
3490
3494
  }
3491
3495
  };
3492
3496
  const execQuery = (adapter, method, sql) => {
@@ -3909,7 +3913,7 @@ const addParsersForSelectJoined = (q, arg, as = arg) => {
3909
3913
  );
3910
3914
  }
3911
3915
  };
3912
- const addParserForSelectItem = (q, as, key, arg, joinQuery) => {
3916
+ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
3913
3917
  if (typeof arg === "object" || typeof arg === "function") {
3914
3918
  const { q: query } = arg;
3915
3919
  if (query.batchParsers) {
@@ -4081,7 +4085,13 @@ const addParserForSelectItem = (q, as, key, arg, joinQuery) => {
4081
4085
  }
4082
4086
  return arg;
4083
4087
  }
4084
- return setParserForSelectedString(q, arg, as, key);
4088
+ return setParserForSelectedString(
4089
+ q,
4090
+ arg,
4091
+ as,
4092
+ key,
4093
+ columnAlias
4094
+ );
4085
4095
  };
4086
4096
  const collectNestedSelectBatches = (batches, rows, path, last) => {
4087
4097
  const stack = rows.map(
@@ -4173,78 +4183,75 @@ const processSelectArg = (q, as, arg, columnAs) => {
4173
4183
  as,
4174
4184
  key,
4175
4185
  value,
4186
+ key,
4176
4187
  joinQuery
4177
4188
  );
4178
4189
  }
4179
4190
  return { selectAs };
4180
4191
  };
4181
- const setParserForSelectedString = (query, arg, as, columnAs) => {
4192
+ const setParserForSelectedString = (query, arg, as, columnAs, columnAlias) => {
4182
4193
  const { q } = query;
4183
4194
  const index = arg.indexOf(".");
4184
- if (index !== -1) {
4185
- const table = getFullColumnTable(
4186
- query,
4187
- arg,
4188
- index,
4189
- as
4190
- );
4191
- const column = arg.slice(index + 1);
4192
- if (column === "*") {
4193
- addParsersForSelectJoined(query, table, columnAs);
4194
- return table === as ? column : arg;
4195
- }
4196
- if (table === as) {
4197
- if (columnAs && q.parsers) {
4198
- const parser2 = q.parsers[column];
4199
- if (parser2) orchidCore.setObjectValueImmutable(q, "parsers", columnAs, parser2);
4200
- }
4201
- return handleComputed(query, q.computeds, column);
4202
- }
4203
- const parser = q.joinedParsers?.[table]?.[column];
4204
- if (parser) orchidCore.setParserToQuery(q, columnAs || column, parser);
4205
- const batchParsers = q.joinedBatchParsers?.[table];
4206
- if (batchParsers) {
4207
- let cloned = false;
4208
- for (const bp of batchParsers) {
4209
- if (bp.path[0] === column) {
4210
- if (!cloned) {
4211
- q.batchParsers = [...q.batchParsers || []];
4212
- cloned = true;
4213
- }
4214
- q.batchParsers.push(bp);
4195
+ if (index === -1) {
4196
+ return selectColumn(query, q, arg, columnAs, columnAlias);
4197
+ }
4198
+ const table = getFullColumnTable(query, arg, index, as);
4199
+ const column = arg.slice(index + 1);
4200
+ if (column === "*") {
4201
+ addParsersForSelectJoined(query, table, columnAs);
4202
+ return table === as ? column : arg;
4203
+ }
4204
+ if (table === as) {
4205
+ return selectColumn(query, q, column, columnAs, columnAlias);
4206
+ }
4207
+ const parser = q.joinedParsers?.[table]?.[column];
4208
+ if (parser) orchidCore.setParserToQuery(q, columnAs || column, parser);
4209
+ const batchParsers = q.joinedBatchParsers?.[table];
4210
+ if (batchParsers) {
4211
+ let cloned = false;
4212
+ for (const bp of batchParsers) {
4213
+ if (bp.path[0] === column) {
4214
+ if (!cloned) {
4215
+ q.batchParsers = [...q.batchParsers || []];
4216
+ cloned = true;
4215
4217
  }
4218
+ q.batchParsers.push(bp);
4216
4219
  }
4217
4220
  }
4218
- const computeds = q.joinedComputeds?.[table];
4219
- if (computeds?.[column]) {
4220
- const computed = computeds[column];
4221
- const map = q.hookSelect = new Map(q.hookSelect);
4222
- for (const column2 of computed.deps) {
4223
- map.set(column2, { select: `${table}.${column2}` });
4224
- }
4225
- orchidCore.setObjectValueImmutable(q, "selectedComputeds", column, computed);
4226
- return;
4227
- }
4228
- return arg;
4229
- } else {
4230
- if (columnAs && q.parsers) {
4231
- const parser = q.parsers?.[arg];
4232
- if (parser) orchidCore.setObjectValueImmutable(q, "parsers", columnAs, parser);
4233
- }
4234
- return handleComputed(query, q.computeds, arg);
4235
4221
  }
4236
- };
4237
- const handleComputed = (q, computeds, column) => {
4222
+ const computeds = q.joinedComputeds?.[table];
4238
4223
  if (computeds?.[column]) {
4239
4224
  const computed = computeds[column];
4240
- const map = q.q.hookSelect = new Map(q.q.hookSelect);
4225
+ const map = q.hookSelect = new Map(q.hookSelect);
4241
4226
  for (const column2 of computed.deps) {
4242
- map.set(column2, { select: column2 });
4227
+ map.set(column2, { select: `${table}.${column2}` });
4243
4228
  }
4244
- q.q.selectedComputeds = { ...q.q.selectedComputeds, [column]: computed };
4229
+ orchidCore.setObjectValueImmutable(q, "selectedComputeds", column, computed);
4245
4230
  return;
4246
4231
  }
4247
- return column;
4232
+ return arg;
4233
+ };
4234
+ const selectColumn = (query, q, key, columnAs, columnAlias) => {
4235
+ if (columnAlias === "pluck") {
4236
+ throw new Error("?");
4237
+ }
4238
+ if (columnAs && q.parsers) {
4239
+ const parser = q.parsers[key];
4240
+ if (parser) orchidCore.setObjectValueImmutable(q, "parsers", columnAs, parser);
4241
+ }
4242
+ if (q.computeds?.[key]) {
4243
+ const computed = q.computeds[key];
4244
+ const map = query.q.hookSelect = new Map(query.q.hookSelect);
4245
+ for (const key2 of computed.deps) {
4246
+ map.set(key2, { select: key2 });
4247
+ }
4248
+ query.q.selectedComputeds = {
4249
+ ...query.q.selectedComputeds,
4250
+ [columnAlias || key]: computed
4251
+ };
4252
+ return;
4253
+ }
4254
+ return key;
4248
4255
  };
4249
4256
  const getShapeFromSelect = (q, isSubQuery) => {
4250
4257
  const query = q.q;
@@ -4846,6 +4853,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4846
4853
  return query.inCTE.selectNum || !select ? select ? "0, " + select : "0" : select;
4847
4854
  }
4848
4855
  let selected;
4856
+ let selectedAs;
4849
4857
  const list = [];
4850
4858
  if (query.select) {
4851
4859
  for (const item of query.select) {
@@ -4854,8 +4862,10 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4854
4862
  if (item === "*") {
4855
4863
  if (hookSelect) {
4856
4864
  selected ?? (selected = {});
4865
+ selectedAs ?? (selectedAs = {});
4857
4866
  for (const key in query.selectableShape) {
4858
4867
  selected[key] = quotedAs;
4868
+ selectedAs[key] = key;
4859
4869
  }
4860
4870
  }
4861
4871
  sql = selectAllSql(query, quotedAs, jsonList);
@@ -4866,6 +4876,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4866
4876
  const key = item.slice(index + 1);
4867
4877
  if (hookSelect?.get(key)) {
4868
4878
  (selected ?? (selected = {}))[key] = `"${tableName}"`;
4879
+ (selectedAs ?? (selectedAs = {}))[key] = key;
4869
4880
  }
4870
4881
  sql = tableColumnToSqlWithAs(
4871
4882
  ctx,
@@ -4879,7 +4890,10 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4879
4890
  jsonList
4880
4891
  );
4881
4892
  } else {
4882
- if (hookSelect?.get(item)) (selected ?? (selected = {}))[item] = quotedAs;
4893
+ if (hookSelect?.get(item)) {
4894
+ (selected ?? (selected = {}))[item] = quotedAs;
4895
+ (selectedAs ?? (selectedAs = {}))[item] = item;
4896
+ }
4883
4897
  sql = ownColumnToSqlWithAs(
4884
4898
  ctx,
4885
4899
  table.q,
@@ -4897,7 +4911,9 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4897
4911
  if ("selectAs" in item) {
4898
4912
  const obj = item.selectAs;
4899
4913
  for (const as in obj) {
4900
- if (hookSelect) (selected ?? (selected = {}))[as] = true;
4914
+ if (hookSelect) {
4915
+ (selected ?? (selected = {}))[as] = true;
4916
+ }
4901
4917
  const value = obj[as];
4902
4918
  if (typeof value === "object") {
4903
4919
  if (orchidCore.isExpression(value)) {
@@ -4913,6 +4929,9 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4913
4929
  }
4914
4930
  }
4915
4931
  } else if (value) {
4932
+ if (hookSelect) {
4933
+ (selectedAs ?? (selectedAs = {}))[value] = as;
4934
+ }
4916
4935
  list.push(
4917
4936
  columnToSqlWithAs(
4918
4937
  ctx,
@@ -4954,7 +4973,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4954
4973
  quotedTable = quotedAs;
4955
4974
  columnName = select;
4956
4975
  col = query.shape[select];
4957
- sql = simpleColumnToSQL(ctx, query, select, col, quotedAs);
4976
+ sql = simpleColumnToSQL(ctx, select, col, quotedAs);
4958
4977
  }
4959
4978
  } else {
4960
4979
  columnName = column;
@@ -4969,9 +4988,17 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
4969
4988
  let i = 2;
4970
4989
  while (selected[name = `${column}${i}`]) i++;
4971
4990
  item.as = name;
4991
+ item.temp = name;
4972
4992
  sql += ` "${name}"`;
4973
- } else if (col?.data.name || typeof select === "object") {
4974
- sql += ` "${columnName}"`;
4993
+ } else if (selectedAs?.[columnName]) {
4994
+ item.as = selectedAs[columnName];
4995
+ item.temp = columnName;
4996
+ continue;
4997
+ } else {
4998
+ if (col?.data.name || typeof select === "object") {
4999
+ sql += ` "${columnName}"`;
5000
+ }
5001
+ item.temp = columnName;
4975
5002
  }
4976
5003
  if (jsonList) jsonList[name] = col;
4977
5004
  list.push(sql);
@@ -5836,13 +5863,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
5836
5863
  let column = query.shape[key];
5837
5864
  let quotedColumn;
5838
5865
  if (column) {
5839
- quotedColumn = simpleExistingColumnToSQL(
5840
- ctx,
5841
- query,
5842
- key,
5843
- column,
5844
- quotedAs
5845
- );
5866
+ quotedColumn = simpleExistingColumnToSQL(ctx, key, column, quotedAs);
5846
5867
  } else if (!column) {
5847
5868
  const index = key.indexOf(".");
5848
5869
  if (index !== -1) {
@@ -5850,7 +5871,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
5850
5871
  const quoted = `"${table2}"`;
5851
5872
  const name = key.slice(index + 1);
5852
5873
  column = quotedAs === quoted ? query.shape[name] : query.joinedShapes?.[table2]?.[name];
5853
- quotedColumn = simpleColumnToSQL(ctx, query, name, column, quoted);
5874
+ quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
5854
5875
  } else {
5855
5876
  column = query.joinedShapes?.[key]?.value;
5856
5877
  quotedColumn = `"${key}".r`;
@@ -7390,6 +7411,7 @@ class VirtualColumn extends ColumnType {
7390
7411
  super(schema, inputSchema);
7391
7412
  this.dataType = "";
7392
7413
  this.operators = Operators.any;
7414
+ this.data.explicitSelect = true;
7393
7415
  }
7394
7416
  toCode() {
7395
7417
  throw new Error(`toCode is not implemented for virtual column`);
@@ -7399,6 +7421,8 @@ class VirtualColumn extends ColumnType {
7399
7421
  const _UnknownColumn = class _UnknownColumn extends VirtualColumn {
7400
7422
  constructor(schema) {
7401
7423
  super(schema, schema.unknown());
7424
+ this.selectable = true;
7425
+ this.data.explicitSelect = void 0;
7402
7426
  }
7403
7427
  };
7404
7428
  _UnknownColumn.instance = new _UnknownColumn(defaultSchemaConfig);
@@ -11722,8 +11746,6 @@ class ColumnRefExpression extends orchidCore.Expression {
11722
11746
  makeSQL(ctx, quotedAs) {
11723
11747
  return simpleExistingColumnToSQL(
11724
11748
  ctx,
11725
- // it's for parsers for computed SQL. In the column ref case, parsers should be set when selecting the column ref.
11726
- {},
11727
11749
  this.name,
11728
11750
  this.result.value,
11729
11751
  quotedAs
@@ -13357,6 +13379,7 @@ exports.addParserForSelectItem = addParserForSelectItem;
13357
13379
  exports.addQueryOn = addQueryOn;
13358
13380
  exports.anyShape = anyShape;
13359
13381
  exports.applyComputedColumns = applyComputedColumns;
13382
+ exports.assignDbDataToColumn = assignDbDataToColumn;
13360
13383
  exports.checkIfASimpleQuery = checkIfASimpleQuery;
13361
13384
  exports.cloneQueryBaseUnscoped = cloneQueryBaseUnscoped;
13362
13385
  exports.columnCheckToCode = columnCheckToCode;
@@ -13392,7 +13415,6 @@ exports.handleResult = handleResult;
13392
13415
  exports.identityToCode = identityToCode;
13393
13416
  exports.indexInnerToCode = indexInnerToCode;
13394
13417
  exports.indexToCode = indexToCode;
13395
- exports.instantiateColumn = instantiateColumn;
13396
13418
  exports.isDefaultTimeStamp = isDefaultTimeStamp;
13397
13419
  exports.isQueryReturnsAll = isQueryReturnsAll;
13398
13420
  exports.isSelectingCount = isSelectingCount;