pqb 0.40.8 → 0.40.10

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.mjs CHANGED
@@ -803,8 +803,8 @@ const columnCode = (type, ctx, key, code, data = type.data, skip) => {
803
803
  addCode(code, part);
804
804
  }
805
805
  }
806
- if (data.isHidden)
807
- addCode(code, ".hidden()");
806
+ if (data.explicitSelect)
807
+ addCode(code, ".select(false)");
808
808
  if (data.isNullable)
809
809
  addCode(code, ".nullable()");
810
810
  if (type.encodeFn && type.encodeFn !== (skip == null ? void 0 : skip.encodeFn))
@@ -2356,7 +2356,7 @@ const getArgQueryTarget = (ctx, first, joinSubQuery, cloned) => {
2356
2356
  }
2357
2357
  };
2358
2358
  const subJoinToSql = (ctx, jq, innerAs, outerAs, cloned) => {
2359
- if (!jq.q.select && jq.internal.columnsForSelectAll) {
2359
+ if (!jq.q.select && jq.q.selectAllColumns) {
2360
2360
  if (!cloned)
2361
2361
  jq = jq.clone();
2362
2362
  jq.q.select = [new RawSQL(`${innerAs}.*`)];
@@ -2576,6 +2576,13 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, whereExists) => {
2576
2576
  s: joinSubQuery
2577
2577
  };
2578
2578
  };
2579
+ const preprocessJoinArg = (q, arg) => {
2580
+ if (typeof arg !== "function")
2581
+ return arg;
2582
+ arg = arg(q.relations);
2583
+ arg.joinQueryAfterCallback = arg.joinQuery;
2584
+ return arg;
2585
+ };
2579
2586
  const makeJoinQueryBuilder = (joinedQuery, joinedShapes, joinTo) => {
2580
2587
  const q = joinedQuery.baseQuery.clone();
2581
2588
  q.baseQuery = q;
@@ -2638,10 +2645,7 @@ const _join = (query, require2, type, first, args) => {
2638
2645
  let batchParsers;
2639
2646
  let computeds;
2640
2647
  let joinSubQuery = false;
2641
- if (typeof first === "function") {
2642
- first = first(query.relations);
2643
- first.joinQueryAfterCallback = first.joinQuery;
2644
- }
2648
+ first = preprocessJoinArg(query, first);
2645
2649
  if (typeof first === "object") {
2646
2650
  if (require2 && isQueryNone(first)) {
2647
2651
  return _queryNone(query);
@@ -2693,7 +2697,7 @@ const _join = (query, require2, type, first, args) => {
2693
2697
  );
2694
2698
  if (joinKey && "s" in joinArgs && joinArgs.s) {
2695
2699
  const j = "j" in joinArgs ? (_b = joinArgs.r) != null ? _b : joinArgs.j : "r" in joinArgs ? joinArgs.r : joinArgs.q;
2696
- if (j.q.select || !j.internal.columnsForSelectAll) {
2700
+ if (j.q.select || !j.q.selectAllColumns) {
2697
2701
  const shape2 = getShapeFromSelect(j, true);
2698
2702
  setQueryObjectValue(
2699
2703
  query,
@@ -4713,7 +4717,7 @@ class SelectItemExpression extends Expression {
4713
4717
  }
4714
4718
  // `makeSQL` acts similarly to how select args are handled
4715
4719
  makeSQL(ctx, quotedAs) {
4716
- return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.query, this.q, quotedAs) : columnToSql(ctx, this.q, this.q.shape, this.item, quotedAs, true) : this.item.toSQL(ctx, quotedAs);
4720
+ return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.q, quotedAs) : columnToSql(ctx, this.q, this.q.shape, this.item, quotedAs, true) : this.item.toSQL(ctx, quotedAs);
4717
4721
  }
4718
4722
  }
4719
4723
 
@@ -4847,12 +4851,14 @@ function queryFrom(self, arg) {
4847
4851
  data.batchParsers = q.q.batchParsers;
4848
4852
  }
4849
4853
  data.from = arg;
4854
+ data.selectAllColumns = data.selectAllKeys = void 0;
4850
4855
  return self;
4851
4856
  }
4852
4857
  function queryFromSql(self, args) {
4853
4858
  const data = self.q;
4854
4859
  data.as || (data.as = "t");
4855
4860
  data.from = sqlQueryArgsToExpression(args);
4861
+ data.selectAllColumns = data.selectAllKeys = void 0;
4856
4862
  return self;
4857
4863
  }
4858
4864
  class FromMethods {
@@ -4967,11 +4973,11 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
4967
4973
  if (item === "*") {
4968
4974
  if (hookSelect) {
4969
4975
  selected != null ? selected : selected = {};
4970
- for (const key in table.internal.columnsKeysForSelectAll || query.shape) {
4976
+ for (const key in query.selectAllKeys || query.shape) {
4971
4977
  selected[key] = quotedAs;
4972
4978
  }
4973
4979
  }
4974
- sql = selectAllSql(table, query, quotedAs);
4980
+ sql = selectAllSql(query, quotedAs);
4975
4981
  } else {
4976
4982
  const index = item.indexOf(".");
4977
4983
  if (index !== -1) {
@@ -5073,15 +5079,15 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
5073
5079
  list.push(sql);
5074
5080
  }
5075
5081
  }
5076
- return list.length ? list.join(", ") : query.select ? "" : selectAllSql(table, query, quotedAs);
5082
+ return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs);
5077
5083
  };
5078
5084
  function selectedObjectToSQL(ctx, quotedAs, item) {
5079
5085
  const sql = item.toSQL(ctx, quotedAs);
5080
5086
  return ctx.aliasValue ? `${sql} r` : sql;
5081
5087
  }
5082
- const selectAllSql = (table, query, quotedAs) => {
5088
+ const selectAllSql = (query, quotedAs) => {
5083
5089
  var _a, _b, _c;
5084
- 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(", ")) || "*";
5090
+ return ((_a = query.join) == null ? void 0 : _a.length) ? ((_b = query.selectAllColumns) == null ? void 0 : _b.map((item) => `${quotedAs}.${item}`).join(", ")) || `${quotedAs}.*` : ((_c = query.selectAllColumns) == null ? void 0 : _c.join(", ")) || "*";
5085
5091
  };
5086
5092
  const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
5087
5093
  var _a, _b, _c;
@@ -5275,7 +5281,7 @@ const pushWithSql = (ctx, items) => {
5275
5281
 
5276
5282
  const checkIfASimpleQuery = (q) => {
5277
5283
  var _a, _b;
5278
- if (q.q.returnType && q.q.returnType !== "all" || q.internal.columnsForSelectAll || ((_a = q.q.and) == null ? void 0 : _a.length) || ((_b = q.q.or) == null ? void 0 : _b.length) || q.q.scopes)
5284
+ if (q.q.returnType && q.q.returnType !== "all" || q.q.selectAllColumns || ((_a = q.q.and) == null ? void 0 : _a.length) || ((_b = q.q.or) == null ? void 0 : _b.length) || q.q.scopes)
5279
5285
  return false;
5280
5286
  const keys = Object.keys(q.q);
5281
5287
  return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
@@ -5303,7 +5309,7 @@ const pushFromAndAs = (ctx, table, data, quotedAs) => {
5303
5309
  const from = getFrom(ctx, table, data, quotedAs);
5304
5310
  sql += from;
5305
5311
  if (data.as && quotedAs && quotedAs !== from) {
5306
- sql += ` AS ${quotedAs}`;
5312
+ sql += ` ${quotedAs}`;
5307
5313
  }
5308
5314
  for (const as in data.sources) {
5309
5315
  const source = data.sources[as];
@@ -9437,16 +9443,17 @@ var __spreadValues$5 = (a, b) => {
9437
9443
  }
9438
9444
  return a;
9439
9445
  };
9440
- const mergableObjects = {
9441
- shape: true,
9442
- withShapes: true,
9443
- parsers: true,
9444
- defaults: true,
9445
- joinedShapes: true,
9446
- joinedParsers: true,
9447
- joinedBatchParsers: true,
9448
- selectedComputeds: true
9449
- };
9446
+ const mergableObjects = /* @__PURE__ */ new Set([
9447
+ "shape",
9448
+ "withShapes",
9449
+ "parsers",
9450
+ "defaults",
9451
+ "joinedShapes",
9452
+ "joinedParsers",
9453
+ "joinedBatchParsers",
9454
+ "selectedComputeds"
9455
+ ]);
9456
+ const dontMergeArrays = /* @__PURE__ */ new Set(["selectAllColumns", "selectAllKeys"]);
9450
9457
  class MergeQueryMethods {
9451
9458
  merge(q) {
9452
9459
  const query = this.clone();
@@ -9462,8 +9469,10 @@ class MergeQueryMethods {
9462
9469
  break;
9463
9470
  case "object":
9464
9471
  if (Array.isArray(value)) {
9465
- a[key] = a[key] ? [...a[key], ...value] : value;
9466
- } else if (mergableObjects[key]) {
9472
+ if (!dontMergeArrays.has(key)) {
9473
+ a[key] = a[key] ? [...a[key], ...value] : value;
9474
+ }
9475
+ } else if (mergableObjects.has(key)) {
9467
9476
  a[key] = a[key] ? __spreadValues$5(__spreadValues$5({}, a[key]), value) : value;
9468
9477
  } else if (key === "union") {
9469
9478
  a[key] = a[key] ? {
@@ -9807,7 +9816,13 @@ const _queryWhereIn = (q, and, arg, values, not) => {
9807
9816
  return q;
9808
9817
  };
9809
9818
  const existsArgs = (self, q, args) => {
9810
- const joinArgs = processJoinArgs(self, q, args, false, true);
9819
+ const joinArgs = processJoinArgs(
9820
+ self,
9821
+ preprocessJoinArg(self, q),
9822
+ args,
9823
+ false,
9824
+ true
9825
+ );
9811
9826
  return [
9812
9827
  {
9813
9828
  EXISTS: joinArgs
@@ -10450,6 +10465,10 @@ class Where {
10450
10465
  * // find by a relation name if it's defined
10451
10466
  * db.user.whereExists('account');
10452
10467
  *
10468
+ * // find users who have an account with positive balance
10469
+ * // `accounts` is a relation name
10470
+ * db.user.whereExists((q) => q.accounts.where({ balance: { gt: 0 } }));
10471
+ *
10453
10472
  * // find using a table and a join conditions
10454
10473
  * db.user.whereExists(db.account, 'account.id', 'user.id');
10455
10474
  *
@@ -11383,7 +11402,11 @@ class QueryUpsertOrCreate {
11383
11402
  if (!isObjectEmpty(updateData)) {
11384
11403
  _queryUpdate(q, updateData);
11385
11404
  }
11386
- return orCreate(q, data.create, updateData, mergeData);
11405
+ const c = orCreate(q, data.create, updateData, mergeData);
11406
+ if (!c.q.select) {
11407
+ c.q.returnType = "void";
11408
+ }
11409
+ return c;
11387
11410
  }
11388
11411
  /**
11389
11412
  * `orCreate` creates a record only if it was not found by conditions.
@@ -12684,7 +12707,7 @@ class Db {
12684
12707
  const parsers = {};
12685
12708
  let hasParsers = false;
12686
12709
  let modifyQuery = void 0;
12687
- let hasCustomName = false;
12710
+ let prepareSelectAll = false;
12688
12711
  const { snakeCase } = options;
12689
12712
  for (const key in shape) {
12690
12713
  const column = shape[key];
@@ -12694,14 +12717,17 @@ class Db {
12694
12717
  parsers[key] = column.parseFn;
12695
12718
  }
12696
12719
  if (column.data.name) {
12697
- hasCustomName = true;
12720
+ prepareSelectAll = true;
12698
12721
  } else if (snakeCase) {
12699
12722
  const snakeName = toSnakeCase(key);
12700
12723
  if (snakeName !== key) {
12701
- hasCustomName = true;
12724
+ prepareSelectAll = true;
12702
12725
  column.data.name = snakeName;
12703
12726
  }
12704
12727
  }
12728
+ if (column.data.explicitSelect) {
12729
+ prepareSelectAll = true;
12730
+ }
12705
12731
  const { modifyQuery: mq } = column.data;
12706
12732
  if (mq) {
12707
12733
  modifyQuery = pushOrNewArray(modifyQuery, (q) => mq(q, column));
@@ -12721,17 +12747,6 @@ class Db {
12721
12747
  }
12722
12748
  }
12723
12749
  }
12724
- if (hasCustomName) {
12725
- const list = [];
12726
- for (const key in shape) {
12727
- const column = shape[key];
12728
- list.push(
12729
- column.data.name ? `"${column.data.name}" AS "${key}"` : `"${key}"`
12730
- );
12731
- }
12732
- this.internal.columnsForSelectAll = list;
12733
- this.internal.columnsKeysForSelectAll = __spreadValues({}, shape);
12734
- }
12735
12750
  this.q = {
12736
12751
  adapter,
12737
12752
  shape,
@@ -12764,21 +12779,22 @@ class Db {
12764
12779
  const columns = Object.keys(
12765
12780
  shape
12766
12781
  );
12767
- const { toSQL } = this;
12768
12782
  this.columns = columns;
12769
- this.defaultSelectColumns = columns.filter(
12770
- (column) => !shape[column].data.isHidden
12771
- );
12772
12783
  if (options.computed)
12773
12784
  applyComputedColumns(this, options.computed);
12774
- const defaultSelect = this.defaultSelectColumns.length === columns.length ? void 0 : this.defaultSelectColumns;
12775
- this.toSQL = defaultSelect ? function(options2) {
12776
- const q = this.clone();
12777
- if (!q.q.select) {
12778
- q.q.select = defaultSelect;
12779
- }
12780
- return toSQL.call(q, options2);
12781
- } : toSQL;
12785
+ if (prepareSelectAll) {
12786
+ const list = [];
12787
+ for (const key in shape) {
12788
+ const column = shape[key];
12789
+ if (!column.data.explicitSelect) {
12790
+ list.push(
12791
+ column.data.name ? `"${column.data.name}" AS "${key}"` : `"${key}"`
12792
+ );
12793
+ }
12794
+ }
12795
+ this.q.selectAllColumns = list;
12796
+ this.q.selectAllKeys = __spreadValues({}, shape);
12797
+ }
12782
12798
  if (modifyQuery) {
12783
12799
  for (const cb of modifyQuery) {
12784
12800
  cb(this);