pqb 0.7.13 → 0.8.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.esm.js CHANGED
@@ -221,24 +221,24 @@ const windowToSql = (window, values, quotedAs) => {
221
221
  }
222
222
  };
223
223
 
224
- const processJoinItem = (ctx, model, args, quotedAs) => {
224
+ const processJoinItem = (ctx, table, args, quotedAs) => {
225
225
  let target;
226
226
  let conditions;
227
227
  const [first] = args;
228
228
  if (typeof first === "string") {
229
- if (first in model.relations) {
229
+ if (first in table.relations) {
230
230
  const {
231
231
  key,
232
232
  query: toQuery,
233
233
  joinQuery
234
- } = model.relations[first];
235
- const jq = joinQuery(model, toQuery);
234
+ } = table.relations[first];
235
+ const jq = joinQuery(table, toQuery);
236
236
  const { query } = jq;
237
- const table = typeof query.from === "string" ? query.from : jq.table;
238
- target = quoteSchemaAndTable(query.schema, table);
237
+ const tableName = typeof query.from === "string" ? query.from : jq.table;
238
+ target = quoteSchemaAndTable(query.schema, tableName);
239
239
  const as = query.as || key;
240
240
  const joinAs = q(as);
241
- if (as !== table) {
241
+ if (as !== tableName) {
242
242
  target += ` AS ${joinAs}`;
243
243
  }
244
244
  const queryData = {
@@ -247,7 +247,7 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
247
247
  };
248
248
  if (args[1]) {
249
249
  const arg = args[1](
250
- new ctx.onQueryBuilder(jq, jq.shape, model)
250
+ new ctx.onQueryBuilder(jq, jq.shape, table)
251
251
  ).query;
252
252
  if (arg.and)
253
253
  queryData.and.push(...arg.and);
@@ -257,7 +257,7 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
257
257
  conditions = whereToSql(ctx, jq, queryData, joinAs);
258
258
  } else {
259
259
  target = q(first);
260
- conditions = processArgs(args, ctx, model, first, target, quotedAs);
260
+ conditions = processArgs(args, ctx, table, first, target, quotedAs);
261
261
  }
262
262
  } else {
263
263
  const query = first.query;
@@ -271,8 +271,8 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
271
271
  target += ` AS ${quoted}`;
272
272
  }
273
273
  }
274
- conditions = processArgs(args, ctx, model, first, joinAs, quotedAs);
275
- const whereSql = whereToSql(ctx, model, query, joinAs);
274
+ conditions = processArgs(args, ctx, table, first, joinAs, quotedAs);
275
+ const whereSql = whereToSql(ctx, table, query, joinAs);
276
276
  if (whereSql) {
277
277
  if (conditions)
278
278
  conditions += ` AND ${whereSql}`;
@@ -282,21 +282,21 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
282
282
  }
283
283
  return { target, conditions };
284
284
  };
285
- const processArgs = (args, ctx, model, first, joinAs, quotedAs) => {
285
+ const processArgs = (args, ctx, table, first, joinAs, quotedAs) => {
286
286
  var _a;
287
287
  if (args.length === 2) {
288
288
  const arg = args[1];
289
289
  if (typeof arg === "function") {
290
290
  let shape;
291
291
  if (typeof first === "string") {
292
- shape = (_a = model.query.withShapes) == null ? void 0 : _a[first];
292
+ shape = (_a = table.query.withShapes) == null ? void 0 : _a[first];
293
293
  if (!shape) {
294
294
  throw new Error("Cannot get shape of `with` statement");
295
295
  }
296
296
  } else {
297
297
  shape = first.shape;
298
298
  }
299
- const jq = arg(new ctx.onQueryBuilder(first, shape, model));
299
+ const jq = arg(new ctx.onQueryBuilder(first, shape, table));
300
300
  return whereToSql(ctx, jq, jq.query, joinAs);
301
301
  } else {
302
302
  return getObjectOrRawConditions(arg, ctx.values, quotedAs, joinAs);
@@ -331,11 +331,11 @@ const getObjectOrRawConditions = (data, values, quotedAs, joinAs) => {
331
331
  return pairs.join(", ");
332
332
  }
333
333
  };
334
- const pushJoinSql = (ctx, model, query, quotedAs) => {
334
+ const pushJoinSql = (ctx, table, query, quotedAs) => {
335
335
  query.join.forEach((item) => {
336
336
  const { target, conditions } = processJoinItem(
337
337
  ctx,
338
- model,
338
+ table,
339
339
  item.args,
340
340
  quotedAs
341
341
  );
@@ -345,41 +345,41 @@ const pushJoinSql = (ctx, model, query, quotedAs) => {
345
345
  });
346
346
  };
347
347
 
348
- const pushWhereStatementSql = (ctx, model, query, quotedAs) => {
349
- const res = whereToSql(ctx, model, query, quotedAs, false);
348
+ const pushWhereStatementSql = (ctx, table, query, quotedAs) => {
349
+ const res = whereToSql(ctx, table, query, quotedAs, false);
350
350
  if (res) {
351
351
  ctx.sql.push("WHERE", res);
352
352
  }
353
353
  };
354
- const pushWhereToSql = (sql, ctx, model, query, quotedAs, not) => {
355
- const res = whereToSql(ctx, model, query, quotedAs, not);
354
+ const pushWhereToSql = (sql, ctx, table, query, quotedAs, not) => {
355
+ const res = whereToSql(ctx, table, query, quotedAs, not);
356
356
  if (res) {
357
357
  sql.push(res);
358
358
  }
359
359
  };
360
- const whereToSql = (ctx, model, query, quotedAs, not) => {
360
+ const whereToSql = (ctx, table, query, quotedAs, not) => {
361
361
  if (query.or) {
362
362
  const ors = query.and ? [query.and, ...query.or] : query.or;
363
- return ors.map((and) => processAnds(and, ctx, model, quotedAs, not)).join(" OR ");
363
+ return ors.map((and) => processAnds(and, ctx, table, quotedAs, not)).join(" OR ");
364
364
  } else if (query.and) {
365
- return processAnds(query.and, ctx, model, quotedAs, not);
365
+ return processAnds(query.and, ctx, table, quotedAs, not);
366
366
  } else {
367
367
  return void 0;
368
368
  }
369
369
  };
370
- const processAnds = (and, ctx, model, quotedAs, not) => {
370
+ const processAnds = (and, ctx, table, quotedAs, not) => {
371
371
  const ands = [];
372
- and.forEach((data) => processWhere(ands, ctx, model, data, quotedAs, not));
372
+ and.forEach((data) => processWhere(ands, ctx, table, data, quotedAs, not));
373
373
  return ands.join(" AND ");
374
374
  };
375
- const processWhere = (ands, ctx, model, data, quotedAs, not) => {
375
+ const processWhere = (ands, ctx, table, data, quotedAs, not) => {
376
376
  const prefix = not ? "NOT " : "";
377
377
  if (typeof data === "function") {
378
- const qb = data(new ctx.whereQueryBuilder(model, model.shape));
378
+ const qb = data(new ctx.whereQueryBuilder(table, table.shape));
379
379
  pushWhereToSql(ands, ctx, qb, qb.query, quotedAs, not);
380
380
  return;
381
381
  }
382
- if ("prototype" in data || "__model" in data) {
382
+ if ("prototype" in data || "__table" in data) {
383
383
  const query = data;
384
384
  const sql = whereToSql(
385
385
  ctx,
@@ -400,15 +400,15 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
400
400
  const value = data[key];
401
401
  if (key === "AND") {
402
402
  const arr = toArray(value);
403
- ands.push(processAnds(arr, ctx, model, quotedAs, not));
403
+ ands.push(processAnds(arr, ctx, table, quotedAs, not));
404
404
  } else if (key === "OR") {
405
405
  const arr = value.map(toArray);
406
406
  ands.push(
407
- arr.map((and) => processAnds(and, ctx, model, quotedAs, not)).join(" OR ")
407
+ arr.map((and) => processAnds(and, ctx, table, quotedAs, not)).join(" OR ")
408
408
  );
409
409
  } else if (key === "NOT") {
410
410
  const arr = toArray(value);
411
- ands.push(processAnds(arr, ctx, model, quotedAs, !not));
411
+ ands.push(processAnds(arr, ctx, table, quotedAs, !not));
412
412
  } else if (key === "ON") {
413
413
  if (Array.isArray(value)) {
414
414
  const item = value;
@@ -417,7 +417,7 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
417
417
  const rightColumn = quoteFullColumn(
418
418
  item[2],
419
419
  getQueryAs({
420
- table: model.table,
420
+ table: table.table,
421
421
  query: { as: quotedAs }
422
422
  })
423
423
  );
@@ -450,7 +450,7 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
450
450
  joinItems.forEach((item) => {
451
451
  const { target, conditions } = processJoinItem(
452
452
  ctx,
453
- model,
453
+ table,
454
454
  item,
455
455
  quotedAs
456
456
  );
@@ -467,7 +467,7 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
467
467
  )}`
468
468
  );
469
469
  } else {
470
- const column = model.shape[key];
470
+ const column = table.shape[key];
471
471
  if (!column) {
472
472
  throw new Error(`Unknown column ${key} provided to condition`);
473
473
  }
@@ -534,7 +534,7 @@ var __spreadValues$p = (a, b) => {
534
534
  return a;
535
535
  };
536
536
  var __spreadProps$j = (a, b) => __defProps$j(a, __getOwnPropDescs$j(b));
537
- const aggregateToSql = (ctx, model, item, quotedAs) => {
537
+ const aggregateToSql = (ctx, table, item, quotedAs) => {
538
538
  var _a;
539
539
  const sql = [`${item.function}(`];
540
540
  ctx = __spreadProps$j(__spreadValues$p({}, ctx), { sql });
@@ -577,7 +577,7 @@ const aggregateToSql = (ctx, model, item, quotedAs) => {
577
577
  if (options.filter || options.filterOr) {
578
578
  const whereSql = whereToSql(
579
579
  ctx,
580
- model,
580
+ table,
581
581
  {
582
582
  and: options.filter ? [options.filter] : void 0,
583
583
  or: (_a = options.filterOr) == null ? void 0 : _a.map((item2) => [item2])
@@ -673,10 +673,10 @@ const jsonToSql = (item, values, quotedAs) => {
673
673
  }
674
674
  return "";
675
675
  };
676
- const pushSelectSql = (ctx, model, query, quotedAs) => {
677
- ctx.sql.push(selectToSql(ctx, model, query, quotedAs));
676
+ const pushSelectSql = (ctx, table, query, quotedAs) => {
677
+ ctx.sql.push(selectToSql(ctx, table, query, quotedAs));
678
678
  };
679
- const selectToSql = (ctx, model, query, quotedAs) => {
679
+ const selectToSql = (ctx, table, query, quotedAs) => {
680
680
  var _a;
681
681
  if (query.select) {
682
682
  const list = [];
@@ -713,13 +713,13 @@ const selectToSql = (ctx, model, query, quotedAs) => {
713
713
  list.push(
714
714
  `${item.function}(${selectToSql(
715
715
  ctx,
716
- model,
716
+ table,
717
717
  { select: item.arguments },
718
718
  quotedAs
719
719
  )})${item.as ? ` AS ${q(item.as)}` : ""}`
720
720
  );
721
721
  } else {
722
- list.push(aggregateToSql(ctx, model, item, quotedAs));
722
+ list.push(aggregateToSql(ctx, table, item, quotedAs));
723
723
  }
724
724
  }
725
725
  });
@@ -748,7 +748,7 @@ const pushSubQuerySql = (query, as, values, list) => {
748
748
  }
749
749
  select.length = 0;
750
750
  select[0] = { selectAs: { c: first } };
751
- query = query._wrap(query.__model.clone());
751
+ query = query._wrap(query.__table.clone());
752
752
  query._getOptional(raw(`COALESCE(json_agg("c"), '[]')`));
753
753
  break;
754
754
  }
@@ -777,12 +777,12 @@ const aggregateOptionNames = [
777
777
  "filterOr",
778
778
  "withinGroup"
779
779
  ];
780
- const pushHavingSql = (ctx, model, query, quotedAs) => {
781
- const conditions = havingToSql(ctx, model, query, quotedAs);
780
+ const pushHavingSql = (ctx, table, query, quotedAs) => {
781
+ const conditions = havingToSql(ctx, table, query, quotedAs);
782
782
  if (conditions.length)
783
783
  ctx.sql.push("HAVING", conditions);
784
784
  };
785
- const havingToSql = (ctx, model, query, quotedAs) => {
785
+ const havingToSql = (ctx, table, query, quotedAs) => {
786
786
  const or = query.having && query.havingOr ? [query.having, ...query.havingOr] : query.having ? [query.having] : query.havingOr;
787
787
  if (!(or == null ? void 0 : or.length))
788
788
  return "";
@@ -790,7 +790,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
790
790
  or.forEach((and) => {
791
791
  const ands = [];
792
792
  and.forEach((item) => {
793
- if ("prototype" in item || "__model" in item) {
793
+ if ("prototype" in item || "__table" in item) {
794
794
  const query2 = item;
795
795
  const sql = havingToSql(
796
796
  ctx,
@@ -816,7 +816,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
816
816
  if (!aggregateOptionNames.includes(
817
817
  op
818
818
  )) {
819
- const operator = model.shape[column].operators[op];
819
+ const operator = table.shape[column].operators[op];
820
820
  if (!operator) {
821
821
  throw new Error(
822
822
  `Unknown operator ${op} provided to condition`
@@ -824,7 +824,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
824
824
  }
825
825
  const expression = aggregateToSql(
826
826
  ctx,
827
- model,
827
+ table,
828
828
  {
829
829
  function: key,
830
830
  arg: column,
@@ -845,7 +845,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
845
845
  ands.push(
846
846
  `${aggregateToSql(
847
847
  ctx,
848
- model,
848
+ table,
849
849
  {
850
850
  function: key,
851
851
  arg: column,
@@ -911,17 +911,17 @@ const queryKeysOfNotSimpleQuery = [
911
911
  "for"
912
912
  ];
913
913
 
914
- const pushFromAndAs = (ctx, model, query, quotedAs) => {
914
+ const pushFromAndAs = (ctx, table, query, quotedAs) => {
915
915
  ctx.sql.push("FROM");
916
916
  if (query.fromOnly)
917
917
  ctx.sql.push("ONLY");
918
- const from = getFrom(model, query, ctx.values);
918
+ const from = getFrom(table, query, ctx.values);
919
919
  ctx.sql.push(from);
920
920
  if (query.as && quotedAs && quotedAs !== from) {
921
921
  ctx.sql.push("AS", quotedAs);
922
922
  }
923
923
  };
924
- const getFrom = (model, query, values) => {
924
+ const getFrom = (table, query, values) => {
925
925
  if (query.from) {
926
926
  if (typeof query.from === "object") {
927
927
  if (isRaw(query.from)) {
@@ -940,7 +940,7 @@ const getFrom = (model, query, values) => {
940
940
  }
941
941
  return quoteSchemaAndTable(query.schema, query.from);
942
942
  }
943
- return quoteSchemaAndTable(query.schema, model.table);
943
+ return quoteSchemaAndTable(query.schema, table.table);
944
944
  };
945
945
 
946
946
  const pushQueryArray = (q, key, value) => {
@@ -970,7 +970,7 @@ const setQueryObjectValue = (q, object, key, value) => {
970
970
  return q;
971
971
  };
972
972
 
973
- const pushInsertSql = (ctx, model, query, quotedAs) => {
973
+ const pushInsertSql = (ctx, table, query, quotedAs) => {
974
974
  const quotedColumns = query.columns.map(q);
975
975
  ctx.sql.push(`INSERT INTO ${quotedAs}(${quotedColumns.join(", ")})`);
976
976
  if (query.fromQuery) {
@@ -1025,22 +1025,22 @@ const pushInsertSql = (ctx, model, query, quotedAs) => {
1025
1025
  ctx.sql.push("DO UPDATE SET", set);
1026
1026
  }
1027
1027
  }
1028
- pushWhereStatementSql(ctx, model, query, quotedAs);
1029
- pushReturningSql(ctx, model, query, quotedAs);
1028
+ pushWhereStatementSql(ctx, table, query, quotedAs);
1029
+ pushReturningSql(ctx, table, query, quotedAs);
1030
1030
  };
1031
1031
  const encodeRow = (ctx, row) => {
1032
1032
  return row.map(
1033
1033
  (value) => value === void 0 ? "DEFAULT" : addValue(ctx.values, value)
1034
1034
  ).join(", ");
1035
1035
  };
1036
- const pushReturningSql = (ctx, model, query, quotedAs) => {
1036
+ const pushReturningSql = (ctx, table, query, quotedAs) => {
1037
1037
  if (query.select) {
1038
- ctx.sql.push(`RETURNING ${selectToSql(ctx, model, query, quotedAs)}`);
1038
+ ctx.sql.push(`RETURNING ${selectToSql(ctx, table, query, quotedAs)}`);
1039
1039
  }
1040
1040
  };
1041
1041
 
1042
- const pushUpdateSql = (ctx, model, query, quotedAs) => {
1043
- const quotedTable = quoteSchemaAndTable(query.schema, model.table);
1042
+ const pushUpdateSql = (ctx, table, query, quotedAs) => {
1043
+ const quotedTable = quoteSchemaAndTable(query.schema, table.table);
1044
1044
  ctx.sql.push(`UPDATE ${quotedTable}`);
1045
1045
  if (quotedTable !== quotedAs) {
1046
1046
  ctx.sql.push(`AS ${quotedAs}`);
@@ -1049,8 +1049,8 @@ const pushUpdateSql = (ctx, model, query, quotedAs) => {
1049
1049
  const set = [];
1050
1050
  processData(ctx, set, query.updateData);
1051
1051
  ctx.sql.push(set.join(", "));
1052
- pushWhereStatementSql(ctx, model, query, quotedAs);
1053
- pushReturningSql(ctx, model, query, quotedAs);
1052
+ pushWhereStatementSql(ctx, table, query, quotedAs);
1053
+ pushReturningSql(ctx, table, query, quotedAs);
1054
1054
  };
1055
1055
  const processData = (ctx, set, data) => {
1056
1056
  let append;
@@ -1087,9 +1087,9 @@ const processValue = (values, key, value) => {
1087
1087
  return addValue(values, value);
1088
1088
  };
1089
1089
 
1090
- const pushDeleteSql = (ctx, model, query, quotedAs) => {
1090
+ const pushDeleteSql = (ctx, table, query, quotedAs) => {
1091
1091
  var _a, _b, _c;
1092
- const from = q(model.table);
1092
+ const from = q(table.table);
1093
1093
  ctx.sql.push(`DELETE FROM ${from}`);
1094
1094
  if (from !== quotedAs) {
1095
1095
  ctx.sql.push(`AS ${quotedAs}`);
@@ -1097,12 +1097,12 @@ const pushDeleteSql = (ctx, model, query, quotedAs) => {
1097
1097
  let conditions;
1098
1098
  if ((_a = query.join) == null ? void 0 : _a.length) {
1099
1099
  const items = query.join.map(
1100
- (item) => processJoinItem(ctx, model, item.args, quotedAs)
1100
+ (item) => processJoinItem(ctx, table, item.args, quotedAs)
1101
1101
  );
1102
1102
  ctx.sql.push(`USING ${items.map((item) => item.target).join(", ")}`);
1103
1103
  conditions = items.map((item) => item.conditions).filter(Boolean).join(" AND ");
1104
1104
  }
1105
- pushWhereStatementSql(ctx, model, query, quotedAs);
1105
+ pushWhereStatementSql(ctx, table, query, quotedAs);
1106
1106
  if (conditions == null ? void 0 : conditions.length) {
1107
1107
  if (((_b = query.and) == null ? void 0 : _b.length) || ((_c = query.or) == null ? void 0 : _c.length)) {
1108
1108
  ctx.sql.push("AND", conditions);
@@ -1110,7 +1110,7 @@ const pushDeleteSql = (ctx, model, query, quotedAs) => {
1110
1110
  ctx.sql.push("WHERE", conditions);
1111
1111
  }
1112
1112
  }
1113
- pushReturningSql(ctx, model, query, quotedAs);
1113
+ pushReturningSql(ctx, table, query, quotedAs);
1114
1114
  };
1115
1115
 
1116
1116
  const pushTruncateSql = (ctx, table, query) => {
@@ -1133,13 +1133,13 @@ const pushColumnInfoSql = (ctx, table, query) => {
1133
1133
  }
1134
1134
  };
1135
1135
 
1136
- const pushCopySql = (ctx, model, query, quotedAs) => {
1136
+ const pushCopySql = (ctx, table, query, quotedAs) => {
1137
1137
  const { sql } = ctx;
1138
1138
  const { copy } = query;
1139
1139
  const columns = copy.columns ? `(${copy.columns.map(q).join(", ")})` : "";
1140
1140
  const target = "from" in copy ? copy.from : copy.to;
1141
1141
  sql.push(
1142
- `COPY ${q(model.table)}${columns} ${"from" in copy ? "FROM" : "TO"} ${typeof target === "string" ? quote(target) : `PROGRAM ${quote(target.program)}`}`
1142
+ `COPY ${q(table.table)}${columns} ${"from" in copy ? "FROM" : "TO"} ${typeof target === "string" ? quote(target) : `PROGRAM ${quote(target.program)}`}`
1143
1143
  );
1144
1144
  if (Object.keys(copy).length > (copy.columns ? 2 : 1)) {
1145
1145
  const options = [];
@@ -1169,19 +1169,19 @@ const pushCopySql = (ctx, model, query, quotedAs) => {
1169
1169
  options.push(`ENCODING ${quote(copy.encoding)}`);
1170
1170
  sql.push(`WITH (${options.join(", ")})`);
1171
1171
  }
1172
- pushWhereStatementSql(ctx, model, query, quotedAs);
1172
+ pushWhereStatementSql(ctx, table, query, quotedAs);
1173
1173
  };
1174
1174
 
1175
1175
  const toSqlCacheKey = Symbol("toSqlCache");
1176
- const toSql = (model, options) => {
1177
- return !(options == null ? void 0 : options.clearCache) && model.query[toSqlCacheKey] || (model.query[toSqlCacheKey] = makeSql(model, options));
1176
+ const toSql = (table, options) => {
1177
+ return !(options == null ? void 0 : options.clearCache) && table.query[toSqlCacheKey] || (table.query[toSqlCacheKey] = makeSql(table, options));
1178
1178
  };
1179
- const makeSql = (model, { values = [] } = {}) => {
1180
- const query = model.query;
1179
+ const makeSql = (table, { values = [] } = {}) => {
1180
+ const query = table.query;
1181
1181
  const sql = [];
1182
1182
  const ctx = {
1183
- whereQueryBuilder: model.whereQueryBuilder,
1184
- onQueryBuilder: model.onQueryBuilder,
1183
+ whereQueryBuilder: table.whereQueryBuilder,
1184
+ onQueryBuilder: table.onQueryBuilder,
1185
1185
  sql,
1186
1186
  values
1187
1187
  };
@@ -1190,56 +1190,56 @@ const makeSql = (model, { values = [] } = {}) => {
1190
1190
  }
1191
1191
  if (query.type) {
1192
1192
  if (query.type === "truncate") {
1193
- if (!model.table)
1193
+ if (!table.table)
1194
1194
  throw new Error("Table is missing for truncate");
1195
- pushTruncateSql(ctx, model.table, query);
1195
+ pushTruncateSql(ctx, table.table, query);
1196
1196
  return { text: sql.join(" "), values };
1197
1197
  }
1198
1198
  if (query.type === "columnInfo") {
1199
- if (!model.table)
1199
+ if (!table.table)
1200
1200
  throw new Error("Table is missing for truncate");
1201
- pushColumnInfoSql(ctx, model.table, query);
1201
+ pushColumnInfoSql(ctx, table.table, query);
1202
1202
  return { text: sql.join(" "), values };
1203
1203
  }
1204
- if (!model.table)
1204
+ if (!table.table)
1205
1205
  throw new Error(`Table is missing for ${query.type}`);
1206
- const quotedAs2 = q(query.as || model.table);
1206
+ const quotedAs2 = q(query.as || table.table);
1207
1207
  if (query.type === "insert") {
1208
- pushInsertSql(ctx, model, query, q(model.table));
1208
+ pushInsertSql(ctx, table, query, q(table.table));
1209
1209
  return { text: sql.join(" "), values };
1210
1210
  }
1211
1211
  if (query.type === "update") {
1212
- pushUpdateSql(ctx, model, query, quotedAs2);
1212
+ pushUpdateSql(ctx, table, query, quotedAs2);
1213
1213
  return { text: sql.join(" "), values };
1214
1214
  }
1215
1215
  if (query.type === "delete") {
1216
- pushDeleteSql(ctx, model, query, quotedAs2);
1216
+ pushDeleteSql(ctx, table, query, quotedAs2);
1217
1217
  return { text: sql.join(" "), values };
1218
1218
  }
1219
1219
  if (query.type === "copy") {
1220
- pushCopySql(ctx, model, query, quotedAs2);
1220
+ pushCopySql(ctx, table, query, quotedAs2);
1221
1221
  return { text: sql.join(" "), values };
1222
1222
  }
1223
1223
  }
1224
- const quotedAs = model.table && q(query.as || model.table);
1224
+ const quotedAs = table.table && q(query.as || table.table);
1225
1225
  sql.push("SELECT");
1226
1226
  if (query.distinct) {
1227
1227
  pushDistinctSql(ctx, query.distinct, quotedAs);
1228
1228
  }
1229
- pushSelectSql(ctx, model, query, quotedAs);
1230
- if (model.table || query.from) {
1231
- pushFromAndAs(ctx, model, query, quotedAs);
1229
+ pushSelectSql(ctx, table, query, quotedAs);
1230
+ if (table.table || query.from) {
1231
+ pushFromAndAs(ctx, table, query, quotedAs);
1232
1232
  }
1233
1233
  if (query.join) {
1234
1234
  pushJoinSql(
1235
1235
  ctx,
1236
- model,
1236
+ table,
1237
1237
  query,
1238
1238
  quotedAs
1239
1239
  );
1240
1240
  }
1241
1241
  if (query.and || query.or) {
1242
- pushWhereStatementSql(ctx, model, query, quotedAs);
1242
+ pushWhereStatementSql(ctx, table, query, quotedAs);
1243
1243
  }
1244
1244
  if (query.group) {
1245
1245
  const group = query.group.map(
@@ -1248,7 +1248,7 @@ const makeSql = (model, { values = [] } = {}) => {
1248
1248
  sql.push(`GROUP BY ${group.join(", ")}`);
1249
1249
  }
1250
1250
  if (query.having || query.havingOr) {
1251
- pushHavingSql(ctx, model, query, quotedAs);
1251
+ pushHavingSql(ctx, table, query, quotedAs);
1252
1252
  }
1253
1253
  if (query.window) {
1254
1254
  const window = [];
@@ -4154,9 +4154,9 @@ class Delete {
4154
4154
 
4155
4155
  const forQueryBuilder = (q, type, tableNames) => {
4156
4156
  q.query.for = { type, tableNames };
4157
- q.__model = Object.create(q.__model);
4158
- q.__model.__model = q.__model;
4159
- Object.assign(q.__model, {
4157
+ q.__table = Object.create(q.__table);
4158
+ q.__table.__table = q.__table;
4159
+ Object.assign(q.__table, {
4160
4160
  noWait() {
4161
4161
  return this.clone()._noWait();
4162
4162
  },
@@ -4247,7 +4247,7 @@ var __spreadValues$5 = (a, b) => {
4247
4247
  return a;
4248
4248
  };
4249
4249
  const processHavingArg = (arg) => {
4250
- if ("__model" in arg || isRaw(arg)) {
4250
+ if ("__table" in arg || isRaw(arg)) {
4251
4251
  return arg;
4252
4252
  } else {
4253
4253
  const processed = __spreadValues$5({}, arg);
@@ -4694,13 +4694,13 @@ class WhereQueryBuilder extends Where {
4694
4694
  this.withData = {};
4695
4695
  this.table = typeof q === "object" ? q.table : q;
4696
4696
  this.shape = shape;
4697
- this.__model = this;
4697
+ this.__table = this;
4698
4698
  if (typeof q === "object" && q.query.as) {
4699
4699
  this.query.as = q.query.as;
4700
4700
  }
4701
4701
  }
4702
4702
  clone() {
4703
- const cloned = Object.create(this.__model);
4703
+ const cloned = Object.create(this.__table);
4704
4704
  cloned.query = getClonedQueryData(this.query);
4705
4705
  return cloned;
4706
4706
  }
@@ -4841,7 +4841,7 @@ class Json {
4841
4841
  return this.clone()._json();
4842
4842
  }
4843
4843
  _json() {
4844
- const q = this._wrap(this.__model.clone());
4844
+ const q = this._wrap(this.__table.clone());
4845
4845
  q._getOptional(
4846
4846
  raw(
4847
4847
  queryTypeWithLimitOne[this.query.returnType] ? `row_to_json("t".*)` : `COALESCE(json_agg(row_to_json("t".*)), '[]')`
@@ -5247,7 +5247,7 @@ class Update {
5247
5247
  ctx.resultAll = await handleResult(q, queryResult);
5248
5248
  if (ctx.updateLater) {
5249
5249
  await Promise.all(ctx.updateLaterPromises);
5250
- const t = this.__model.clone().transacting(q);
5250
+ const t = this.__table.clone().transacting(q);
5251
5251
  const keys = this.primaryKeys;
5252
5252
  t._whereIn(
5253
5253
  keys,
@@ -5496,7 +5496,7 @@ class QueryMethods {
5496
5496
  return this;
5497
5497
  }
5498
5498
  clone() {
5499
- const cloned = Object.create(this.__model);
5499
+ const cloned = Object.create(this.__table);
5500
5500
  cloned.query = getClonedQueryData(this.query);
5501
5501
  return cloned;
5502
5502
  }
@@ -5680,7 +5680,7 @@ class Db {
5680
5680
  this.whereQueryBuilder = WhereQueryBuilder;
5681
5681
  this.onQueryBuilder = OnQueryBuilder;
5682
5682
  var _a, _b;
5683
- this.__model = this;
5683
+ this.__table = this;
5684
5684
  const logger = options.logger || console;
5685
5685
  this.query = {
5686
5686
  adapter,