pqb 0.31.3 → 0.31.5

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
@@ -1774,35 +1774,68 @@ function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
1774
1774
  return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1775
1775
  }
1776
1776
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1777
- var _a, _b, _c, _d, _e;
1777
+ var _a;
1778
1778
  const index = column.indexOf(".");
1779
1779
  if (index !== -1) {
1780
- const table = column.slice(0, index);
1781
- const key = column.slice(index + 1);
1782
- if (key === "*") {
1783
- if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1784
- return select ? `row_to_json("${table}".*)` : `"${table}".r`;
1780
+ return columnWithDotToSql(
1781
+ ctx,
1782
+ data,
1783
+ shape,
1784
+ column,
1785
+ index,
1786
+ quotedAs,
1787
+ select
1788
+ );
1789
+ }
1790
+ if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1791
+ return `"${column}".r`;
1792
+ }
1793
+ return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1794
+ };
1795
+ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1796
+ var _a;
1797
+ const index = column.indexOf(".");
1798
+ if (index !== -1) {
1799
+ return columnWithDotToSql(ctx, data, data.shape, column, index, quotedAs);
1800
+ } else {
1801
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[column]) {
1802
+ return `"${column}".r`;
1803
+ }
1804
+ if (data.select) {
1805
+ for (const s of data.select) {
1806
+ if (typeof s === "object" && "selectAs" in s) {
1807
+ if (column in s.selectAs) {
1808
+ return simpleColumnToSQL(ctx, column, data.shape[column]);
1809
+ }
1810
+ }
1785
1811
  }
1786
- return column;
1787
1812
  }
1788
- const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1789
- const quoted = `"${table}"`;
1790
- const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
1791
- if (col) {
1792
- if (col.data.name) {
1793
- return `"${tableName}"."${col.data.name}"`;
1794
- }
1795
- if (col.data.computed) {
1796
- return `${col.data.computed.toSQL(ctx, quoted)}`;
1797
- }
1798
- return `"${tableName}"."${key}"`;
1813
+ return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
1814
+ }
1815
+ };
1816
+ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
1817
+ var _a, _b, _c, _d;
1818
+ const table = column.slice(0, index);
1819
+ const key = column.slice(index + 1);
1820
+ if (key === "*") {
1821
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1822
+ return select ? `row_to_json("${table}".*)` : `"${table}".r`;
1799
1823
  }
1800
- return `"${tableName}"."${key}"`;
1824
+ return column;
1801
1825
  }
1802
- if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
1803
- return `"${column}".r`;
1826
+ const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1827
+ const quoted = `"${table}"`;
1828
+ const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
1829
+ if (col) {
1830
+ if (col.data.name) {
1831
+ return `"${tableName}"."${col.data.name}"`;
1832
+ }
1833
+ if (col.data.computed) {
1834
+ return `${col.data.computed.toSQL(ctx, quoted)}`;
1835
+ }
1836
+ return `"${tableName}"."${key}"`;
1804
1837
  }
1805
- return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1838
+ return `"${tableName}"."${key}"`;
1806
1839
  };
1807
1840
  const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1808
1841
  var _a, _b, _c, _d;
@@ -1937,7 +1970,7 @@ const setQueryObjectValue = (q, object, key, value) => {
1937
1970
  return q;
1938
1971
  };
1939
1972
  const throwIfNoWhere = (q, method) => {
1940
- if (!q.q.or && !q.q.and && !q.q.all) {
1973
+ if (!q.q.or && !q.q.and && !q.q.scopes && !q.q.all) {
1941
1974
  throw new OrchidOrmInternalError(
1942
1975
  q,
1943
1976
  `Dangerous ${method} without conditions`
@@ -1994,6 +2027,22 @@ const pushWhereToSql = (sql, ctx, table, query, quotedAs, parens) => {
1994
2027
  }
1995
2028
  };
1996
2029
  const whereToSql = (ctx, table, query, quotedAs, parens) => {
2030
+ if (query.scopes) {
2031
+ let sql = andOrToSql(ctx, table, query, quotedAs, true);
2032
+ const data = Object.create(query);
2033
+ for (const key in query.scopes) {
2034
+ const scope = query.scopes[key];
2035
+ data.and = scope.and;
2036
+ data.or = scope.or;
2037
+ const scopeSql = andOrToSql(ctx, table, data, quotedAs, true);
2038
+ if (scopeSql)
2039
+ sql = sql ? sql + " AND " + scopeSql : scopeSql;
2040
+ }
2041
+ return sql;
2042
+ }
2043
+ return andOrToSql(ctx, table, query, quotedAs, parens);
2044
+ };
2045
+ const andOrToSql = (ctx, table, query, quotedAs, parens) => {
1997
2046
  var _a;
1998
2047
  let sql;
1999
2048
  if (query.or) {
@@ -2016,42 +2065,21 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
2016
2065
  };
2017
2066
  const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2018
2067
  var _a, _b, _c, _d;
2019
- if (typeof data === "function") {
2020
- const qb = Object.create(table);
2021
- qb.q = getClonedQueryData(query);
2022
- qb.q.and = qb.q.or = void 0;
2023
- qb.q.isSubQuery = true;
2024
- const res = resolveSubQueryCallback(qb, data);
2025
- if (isExpression(res)) {
2026
- ands.push(`(${res.toSQL(ctx, quotedAs)})`);
2027
- } else {
2028
- if (res.q.expr) {
2029
- const q = joinSubQuery(table, res);
2030
- q.q.select = [res.q.expr];
2031
- ands.push(`(${makeSQL(q, ctx).text})`);
2032
- } else {
2033
- pushWhereToSql(
2034
- ands,
2035
- ctx,
2036
- res,
2037
- res.q,
2038
- quotedAs,
2039
- true
2040
- );
2041
- }
2042
- }
2043
- return;
2044
- }
2045
2068
  if ("prototype" in data || "baseQuery" in data) {
2046
2069
  const query2 = data;
2047
- const sql = whereToSql(
2048
- ctx,
2049
- query2,
2050
- query2.q,
2051
- query2.table && `"${query2.table}"`
2052
- );
2053
- if (sql) {
2054
- ands.push(`(${sql})`);
2070
+ if (query2.q.expr) {
2071
+ const q = joinSubQuery(table, query2);
2072
+ q.q.select = [query2.q.expr];
2073
+ ands.push(`(${makeSQL(q, ctx).text})`);
2074
+ } else {
2075
+ pushWhereToSql(
2076
+ ands,
2077
+ ctx,
2078
+ query2,
2079
+ query2.q,
2080
+ query2.table && `"${query2.table}"`,
2081
+ true
2082
+ );
2055
2083
  }
2056
2084
  return;
2057
2085
  }
@@ -2069,7 +2097,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2069
2097
  } else if (key === "OR") {
2070
2098
  const arr = value.map(toArray);
2071
2099
  ands.push(
2072
- arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")
2100
+ `(${arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")})`
2073
2101
  );
2074
2102
  } else if (key === "NOT") {
2075
2103
  const arr = toArray(value);
@@ -2553,6 +2581,9 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2553
2581
  if (query.or) {
2554
2582
  pushQueryArray(q, "or", query.or);
2555
2583
  }
2584
+ if (query.scopes) {
2585
+ q.q.scopes = __spreadValues$c(__spreadValues$c({}, q.q.scopes), query.scopes);
2586
+ }
2556
2587
  }
2557
2588
  const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2558
2589
  [joinTo.q.as || joinTo.table]: joinTo.shape
@@ -3791,8 +3822,7 @@ function queryWrap(self, query, as = "t") {
3791
3822
  }
3792
3823
  function cloneQueryBaseUnscoped(query) {
3793
3824
  const q = query.baseQuery.clone();
3794
- q.q.or = q.q.and = void 0;
3795
- q.q.scopes = emptyObject;
3825
+ q.q.or = q.q.and = q.q.scopes = void 0;
3796
3826
  return q;
3797
3827
  }
3798
3828
 
@@ -4037,7 +4067,7 @@ const addOrder = (ctx, data, column, quotedAs, dir) => {
4037
4067
  const order = dir || (!search.order || search.order === true ? emptyObject : search.order);
4038
4068
  return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${addValue(ctx.values, `{${order.weights}}`)}, ` : ""}${search.vectorSQL}, "${column}"${order.normalization !== void 0 ? `, ${addValue(ctx.values, order.normalization)}` : ""}) ${order.dir || "DESC"}`;
4039
4069
  }
4040
- return `${columnToSql(ctx, data, data.shape, column, quotedAs)} ${dir || "ASC"}`;
4070
+ return `${maybeSelectedColumnToSql(ctx, data, column, quotedAs)} ${dir || "ASC"}`;
4041
4071
  };
4042
4072
 
4043
4073
  const windowToSql = (ctx, data, window, quotedAs) => {
@@ -4092,7 +4122,7 @@ const pushWithSql = (ctx, items) => {
4092
4122
 
4093
4123
  const checkIfASimpleQuery = (q) => {
4094
4124
  var _a, _b;
4095
- 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))
4125
+ 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)
4096
4126
  return false;
4097
4127
  const keys = Object.keys(q.q);
4098
4128
  return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
@@ -4579,7 +4609,7 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
4579
4609
  }
4580
4610
  pushWhereStatementSql(ctx, table, query, quotedAs);
4581
4611
  if (conditions) {
4582
- if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length)) {
4612
+ if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length) || query.scopes) {
4583
4613
  ctx.sql.push("AND", conditions);
4584
4614
  } else {
4585
4615
  ctx.sql.push("WHERE", conditions);
@@ -4782,12 +4812,12 @@ const makeSQL = (table, options) => {
4782
4812
  quotedAs
4783
4813
  );
4784
4814
  }
4785
- if (query.and || query.or) {
4815
+ if (query.and || query.or || query.scopes) {
4786
4816
  pushWhereStatementSql(ctx, table, query, quotedAs);
4787
4817
  }
4788
4818
  if (query.group) {
4789
4819
  const group = query.group.map(
4790
- (item) => isExpression(item) ? item.toSQL(ctx, quotedAs) : columnToSql(ctx, table.q, table.q.shape, item, quotedAs)
4820
+ (item) => isExpression(item) ? item.toSQL(ctx, quotedAs) : maybeSelectedColumnToSql(ctx, table.q, item, quotedAs)
4791
4821
  );
4792
4822
  sql.push(`GROUP BY ${group.join(", ")}`);
4793
4823
  }
@@ -6204,6 +6234,7 @@ const insert = (self, {
6204
6234
  const { q } = self;
6205
6235
  delete q.and;
6206
6236
  delete q.or;
6237
+ delete q.scopes;
6207
6238
  q.type = "insert";
6208
6239
  q.columns = columns;
6209
6240
  q.values = values;
@@ -8563,7 +8594,20 @@ class WithMethods {
8563
8594
  }
8564
8595
  }
8565
8596
 
8597
+ const resolveCallbacksInArgs = (q, args) => {
8598
+ for (let i = 0; i < args.length; i++) {
8599
+ const arg = args[i];
8600
+ if (typeof arg === "function") {
8601
+ const qb = Object.create(q);
8602
+ qb.q = getClonedQueryData(q.q);
8603
+ qb.q.and = qb.q.or = qb.q.scopes = void 0;
8604
+ qb.q.isSubQuery = true;
8605
+ args[i] = resolveSubQueryCallback(qb, arg);
8606
+ }
8607
+ }
8608
+ };
8566
8609
  const _queryWhere = (q, args) => {
8610
+ resolveCallbacksInArgs(q, args);
8567
8611
  return pushQueryArray(
8568
8612
  q,
8569
8613
  "and",
@@ -8578,6 +8622,7 @@ const _queryWhereSql = (q, args) => {
8578
8622
  );
8579
8623
  };
8580
8624
  const _queryWhereNot = (q, args) => {
8625
+ resolveCallbacksInArgs(q, args);
8581
8626
  return pushQueryValue(q, "and", {
8582
8627
  NOT: args
8583
8628
  });
@@ -8588,6 +8633,7 @@ const _queryWhereNotSql = (q, args) => {
8588
8633
  });
8589
8634
  };
8590
8635
  const _queryOr = (q, args) => {
8636
+ resolveCallbacksInArgs(q, args);
8591
8637
  return pushQueryArray(
8592
8638
  q,
8593
8639
  "or",
@@ -8595,10 +8641,13 @@ const _queryOr = (q, args) => {
8595
8641
  );
8596
8642
  };
8597
8643
  const _queryOrNot = (q, args) => {
8644
+ resolveCallbacksInArgs(q, args);
8598
8645
  return pushQueryArray(
8599
8646
  q,
8600
8647
  "or",
8601
- args.map((item) => [{ NOT: item }])
8648
+ args.map((item) => {
8649
+ return [{ NOT: item }];
8650
+ })
8602
8651
  );
8603
8652
  };
8604
8653
  const _queryWhereIn = (q, and, arg, values, not) => {
@@ -10420,10 +10469,8 @@ class ScopeMethods {
10420
10469
  const q = this.clone();
10421
10470
  if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
10422
10471
  const s = q.internal.scopes[scope];
10423
- if (s.and)
10424
- pushQueryArray(q, "and", s.and);
10425
- if (s.or)
10426
- pushQueryArray(q, "or", s.or);
10472
+ if (!s)
10473
+ throw new Error(`Scope ${scope} is not defined`);
10427
10474
  setQueryObjectValue(q, "scopes", scope, s);
10428
10475
  }
10429
10476
  return q;
@@ -10441,23 +10488,13 @@ class ScopeMethods {
10441
10488
  * @param scope - name of the scope to remove from the query
10442
10489
  */
10443
10490
  unscope(scope) {
10444
- var _a;
10445
10491
  const q = this.clone();
10446
- const data = q.q;
10447
- const s = (_a = q.q.scopes) == null ? void 0 : _a[scope];
10448
- if (s) {
10449
- const { and, or } = s;
10450
- if (and) {
10451
- data.and = data.and.filter((x) => !and.includes(x));
10452
- if (!data.and.length)
10453
- delete data.and;
10454
- }
10455
- if (or) {
10456
- data.or = data.or.filter((x) => !or.includes(x));
10457
- if (!data.or.length)
10458
- delete data.or;
10459
- }
10492
+ if (q.q.scopes) {
10460
10493
  delete q.q.scopes[scope];
10494
+ for (const _ in q.q.scopes) {
10495
+ return q;
10496
+ }
10497
+ delete q.q.scopes;
10461
10498
  }
10462
10499
  return q;
10463
10500
  }
@@ -10475,7 +10512,6 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
10475
10512
  and: [{ [column]: null }]
10476
10513
  };
10477
10514
  scopes.deleted = scope;
10478
- pushQueryValue(q, "and", scope.and[0]);
10479
10515
  ((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
10480
10516
  const _del = _softDelete(column);
10481
10517
  q.baseQuery.delete = function() {
@@ -11640,15 +11676,12 @@ class Db {
11640
11676
  if (options.scopes) {
11641
11677
  for (const key in options.scopes) {
11642
11678
  const q = options.scopes[key](this).q;
11643
- const s = {};
11644
- if (q.and)
11645
- s.and = q.and;
11646
- if (q.or)
11647
- s.or = q.or;
11648
- scopes[key] = s;
11679
+ scopes[key] = {
11680
+ and: q.and,
11681
+ or: q.or
11682
+ };
11649
11683
  }
11650
11684
  if (scopes.default) {
11651
- Object.assign(this.q, scopes.default);
11652
11685
  this.q.scopes = { default: scopes.default };
11653
11686
  }
11654
11687
  }