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.d.ts CHANGED
@@ -455,7 +455,7 @@ interface CommonQueryData {
455
455
  * Is needed to remove these operators from query object when changing the query type, see {@link setQueryOperators}.
456
456
  */
457
457
  operators?: BaseOperators;
458
- scopes: {
458
+ scopes?: {
459
459
  [K: string]: QueryScopeData;
460
460
  };
461
461
  all?: true;
package/dist/index.js CHANGED
@@ -1776,35 +1776,68 @@ function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
1776
1776
  return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
1777
1777
  }
1778
1778
  const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
1779
- var _a, _b, _c, _d, _e;
1779
+ var _a;
1780
1780
  const index = column.indexOf(".");
1781
1781
  if (index !== -1) {
1782
- const table = column.slice(0, index);
1783
- const key = column.slice(index + 1);
1784
- if (key === "*") {
1785
- if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1786
- return select ? `row_to_json("${table}".*)` : `"${table}".r`;
1782
+ return columnWithDotToSql(
1783
+ ctx,
1784
+ data,
1785
+ shape,
1786
+ column,
1787
+ index,
1788
+ quotedAs,
1789
+ select
1790
+ );
1791
+ }
1792
+ if (!select && ((_a = data.joinedShapes) == null ? void 0 : _a[column])) {
1793
+ return `"${column}".r`;
1794
+ }
1795
+ return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1796
+ };
1797
+ const maybeSelectedColumnToSql = (ctx, data, column, quotedAs) => {
1798
+ var _a;
1799
+ const index = column.indexOf(".");
1800
+ if (index !== -1) {
1801
+ return columnWithDotToSql(ctx, data, data.shape, column, index, quotedAs);
1802
+ } else {
1803
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[column]) {
1804
+ return `"${column}".r`;
1805
+ }
1806
+ if (data.select) {
1807
+ for (const s of data.select) {
1808
+ if (typeof s === "object" && "selectAs" in s) {
1809
+ if (column in s.selectAs) {
1810
+ return simpleColumnToSQL(ctx, column, data.shape[column]);
1811
+ }
1812
+ }
1787
1813
  }
1788
- return column;
1789
1814
  }
1790
- const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1791
- const quoted = `"${table}"`;
1792
- const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
1793
- if (col) {
1794
- if (col.data.name) {
1795
- return `"${tableName}"."${col.data.name}"`;
1796
- }
1797
- if (col.data.computed) {
1798
- return `${col.data.computed.toSQL(ctx, quoted)}`;
1799
- }
1800
- return `"${tableName}"."${key}"`;
1815
+ return simpleColumnToSQL(ctx, column, data.shape[column], quotedAs);
1816
+ }
1817
+ };
1818
+ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) => {
1819
+ var _a, _b, _c, _d;
1820
+ const table = column.slice(0, index);
1821
+ const key = column.slice(index + 1);
1822
+ if (key === "*") {
1823
+ if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
1824
+ return select ? `row_to_json("${table}".*)` : `"${table}".r`;
1801
1825
  }
1802
- return `"${tableName}"."${key}"`;
1826
+ return column;
1803
1827
  }
1804
- if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
1805
- return `"${column}".r`;
1828
+ const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
1829
+ const quoted = `"${table}"`;
1830
+ const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
1831
+ if (col) {
1832
+ if (col.data.name) {
1833
+ return `"${tableName}"."${col.data.name}"`;
1834
+ }
1835
+ if (col.data.computed) {
1836
+ return `${col.data.computed.toSQL(ctx, quoted)}`;
1837
+ }
1838
+ return `"${tableName}"."${key}"`;
1806
1839
  }
1807
- return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
1840
+ return `"${tableName}"."${key}"`;
1808
1841
  };
1809
1842
  const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1810
1843
  var _a, _b, _c, _d;
@@ -1939,7 +1972,7 @@ const setQueryObjectValue = (q, object, key, value) => {
1939
1972
  return q;
1940
1973
  };
1941
1974
  const throwIfNoWhere = (q, method) => {
1942
- if (!q.q.or && !q.q.and && !q.q.all) {
1975
+ if (!q.q.or && !q.q.and && !q.q.scopes && !q.q.all) {
1943
1976
  throw new OrchidOrmInternalError(
1944
1977
  q,
1945
1978
  `Dangerous ${method} without conditions`
@@ -1996,6 +2029,22 @@ const pushWhereToSql = (sql, ctx, table, query, quotedAs, parens) => {
1996
2029
  }
1997
2030
  };
1998
2031
  const whereToSql = (ctx, table, query, quotedAs, parens) => {
2032
+ if (query.scopes) {
2033
+ let sql = andOrToSql(ctx, table, query, quotedAs, true);
2034
+ const data = Object.create(query);
2035
+ for (const key in query.scopes) {
2036
+ const scope = query.scopes[key];
2037
+ data.and = scope.and;
2038
+ data.or = scope.or;
2039
+ const scopeSql = andOrToSql(ctx, table, data, quotedAs, true);
2040
+ if (scopeSql)
2041
+ sql = sql ? sql + " AND " + scopeSql : scopeSql;
2042
+ }
2043
+ return sql;
2044
+ }
2045
+ return andOrToSql(ctx, table, query, quotedAs, parens);
2046
+ };
2047
+ const andOrToSql = (ctx, table, query, quotedAs, parens) => {
1999
2048
  var _a;
2000
2049
  let sql;
2001
2050
  if (query.or) {
@@ -2018,42 +2067,21 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
2018
2067
  };
2019
2068
  const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2020
2069
  var _a, _b, _c, _d;
2021
- if (typeof data === "function") {
2022
- const qb = Object.create(table);
2023
- qb.q = getClonedQueryData(query);
2024
- qb.q.and = qb.q.or = void 0;
2025
- qb.q.isSubQuery = true;
2026
- const res = resolveSubQueryCallback(qb, data);
2027
- if (orchidCore.isExpression(res)) {
2028
- ands.push(`(${res.toSQL(ctx, quotedAs)})`);
2029
- } else {
2030
- if (res.q.expr) {
2031
- const q = joinSubQuery(table, res);
2032
- q.q.select = [res.q.expr];
2033
- ands.push(`(${makeSQL(q, ctx).text})`);
2034
- } else {
2035
- pushWhereToSql(
2036
- ands,
2037
- ctx,
2038
- res,
2039
- res.q,
2040
- quotedAs,
2041
- true
2042
- );
2043
- }
2044
- }
2045
- return;
2046
- }
2047
2070
  if ("prototype" in data || "baseQuery" in data) {
2048
2071
  const query2 = data;
2049
- const sql = whereToSql(
2050
- ctx,
2051
- query2,
2052
- query2.q,
2053
- query2.table && `"${query2.table}"`
2054
- );
2055
- if (sql) {
2056
- ands.push(`(${sql})`);
2072
+ if (query2.q.expr) {
2073
+ const q = joinSubQuery(table, query2);
2074
+ q.q.select = [query2.q.expr];
2075
+ ands.push(`(${makeSQL(q, ctx).text})`);
2076
+ } else {
2077
+ pushWhereToSql(
2078
+ ands,
2079
+ ctx,
2080
+ query2,
2081
+ query2.q,
2082
+ query2.table && `"${query2.table}"`,
2083
+ true
2084
+ );
2057
2085
  }
2058
2086
  return;
2059
2087
  }
@@ -2071,7 +2099,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2071
2099
  } else if (key === "OR") {
2072
2100
  const arr = value.map(orchidCore.toArray);
2073
2101
  ands.push(
2074
- arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")
2102
+ `(${arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")})`
2075
2103
  );
2076
2104
  } else if (key === "NOT") {
2077
2105
  const arr = orchidCore.toArray(value);
@@ -2555,6 +2583,9 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2555
2583
  if (query.or) {
2556
2584
  pushQueryArray(q, "or", query.or);
2557
2585
  }
2586
+ if (query.scopes) {
2587
+ q.q.scopes = __spreadValues$c(__spreadValues$c({}, q.q.scopes), query.scopes);
2588
+ }
2558
2589
  }
2559
2590
  const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2560
2591
  [joinTo.q.as || joinTo.table]: joinTo.shape
@@ -3793,8 +3824,7 @@ function queryWrap(self, query, as = "t") {
3793
3824
  }
3794
3825
  function cloneQueryBaseUnscoped(query) {
3795
3826
  const q = query.baseQuery.clone();
3796
- q.q.or = q.q.and = void 0;
3797
- q.q.scopes = orchidCore.emptyObject;
3827
+ q.q.or = q.q.and = q.q.scopes = void 0;
3798
3828
  return q;
3799
3829
  }
3800
3830
 
@@ -4039,7 +4069,7 @@ const addOrder = (ctx, data, column, quotedAs, dir) => {
4039
4069
  const order = dir || (!search.order || search.order === true ? orchidCore.emptyObject : search.order);
4040
4070
  return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${orchidCore.addValue(ctx.values, `{${order.weights}}`)}, ` : ""}${search.vectorSQL}, "${column}"${order.normalization !== void 0 ? `, ${orchidCore.addValue(ctx.values, order.normalization)}` : ""}) ${order.dir || "DESC"}`;
4041
4071
  }
4042
- return `${columnToSql(ctx, data, data.shape, column, quotedAs)} ${dir || "ASC"}`;
4072
+ return `${maybeSelectedColumnToSql(ctx, data, column, quotedAs)} ${dir || "ASC"}`;
4043
4073
  };
4044
4074
 
4045
4075
  const windowToSql = (ctx, data, window, quotedAs) => {
@@ -4094,7 +4124,7 @@ const pushWithSql = (ctx, items) => {
4094
4124
 
4095
4125
  const checkIfASimpleQuery = (q) => {
4096
4126
  var _a, _b;
4097
- 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))
4127
+ 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)
4098
4128
  return false;
4099
4129
  const keys = Object.keys(q.q);
4100
4130
  return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
@@ -4581,7 +4611,7 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
4581
4611
  }
4582
4612
  pushWhereStatementSql(ctx, table, query, quotedAs);
4583
4613
  if (conditions) {
4584
- if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length)) {
4614
+ if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length) || query.scopes) {
4585
4615
  ctx.sql.push("AND", conditions);
4586
4616
  } else {
4587
4617
  ctx.sql.push("WHERE", conditions);
@@ -4784,12 +4814,12 @@ const makeSQL = (table, options) => {
4784
4814
  quotedAs
4785
4815
  );
4786
4816
  }
4787
- if (query.and || query.or) {
4817
+ if (query.and || query.or || query.scopes) {
4788
4818
  pushWhereStatementSql(ctx, table, query, quotedAs);
4789
4819
  }
4790
4820
  if (query.group) {
4791
4821
  const group = query.group.map(
4792
- (item) => orchidCore.isExpression(item) ? item.toSQL(ctx, quotedAs) : columnToSql(ctx, table.q, table.q.shape, item, quotedAs)
4822
+ (item) => orchidCore.isExpression(item) ? item.toSQL(ctx, quotedAs) : maybeSelectedColumnToSql(ctx, table.q, item, quotedAs)
4793
4823
  );
4794
4824
  sql.push(`GROUP BY ${group.join(", ")}`);
4795
4825
  }
@@ -6206,6 +6236,7 @@ const insert = (self, {
6206
6236
  const { q } = self;
6207
6237
  delete q.and;
6208
6238
  delete q.or;
6239
+ delete q.scopes;
6209
6240
  q.type = "insert";
6210
6241
  q.columns = columns;
6211
6242
  q.values = values;
@@ -8565,7 +8596,20 @@ class WithMethods {
8565
8596
  }
8566
8597
  }
8567
8598
 
8599
+ const resolveCallbacksInArgs = (q, args) => {
8600
+ for (let i = 0; i < args.length; i++) {
8601
+ const arg = args[i];
8602
+ if (typeof arg === "function") {
8603
+ const qb = Object.create(q);
8604
+ qb.q = getClonedQueryData(q.q);
8605
+ qb.q.and = qb.q.or = qb.q.scopes = void 0;
8606
+ qb.q.isSubQuery = true;
8607
+ args[i] = resolveSubQueryCallback(qb, arg);
8608
+ }
8609
+ }
8610
+ };
8568
8611
  const _queryWhere = (q, args) => {
8612
+ resolveCallbacksInArgs(q, args);
8569
8613
  return pushQueryArray(
8570
8614
  q,
8571
8615
  "and",
@@ -8580,6 +8624,7 @@ const _queryWhereSql = (q, args) => {
8580
8624
  );
8581
8625
  };
8582
8626
  const _queryWhereNot = (q, args) => {
8627
+ resolveCallbacksInArgs(q, args);
8583
8628
  return pushQueryValue(q, "and", {
8584
8629
  NOT: args
8585
8630
  });
@@ -8590,6 +8635,7 @@ const _queryWhereNotSql = (q, args) => {
8590
8635
  });
8591
8636
  };
8592
8637
  const _queryOr = (q, args) => {
8638
+ resolveCallbacksInArgs(q, args);
8593
8639
  return pushQueryArray(
8594
8640
  q,
8595
8641
  "or",
@@ -8597,10 +8643,13 @@ const _queryOr = (q, args) => {
8597
8643
  );
8598
8644
  };
8599
8645
  const _queryOrNot = (q, args) => {
8646
+ resolveCallbacksInArgs(q, args);
8600
8647
  return pushQueryArray(
8601
8648
  q,
8602
8649
  "or",
8603
- args.map((item) => [{ NOT: item }])
8650
+ args.map((item) => {
8651
+ return [{ NOT: item }];
8652
+ })
8604
8653
  );
8605
8654
  };
8606
8655
  const _queryWhereIn = (q, and, arg, values, not) => {
@@ -10422,10 +10471,8 @@ class ScopeMethods {
10422
10471
  const q = this.clone();
10423
10472
  if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
10424
10473
  const s = q.internal.scopes[scope];
10425
- if (s.and)
10426
- pushQueryArray(q, "and", s.and);
10427
- if (s.or)
10428
- pushQueryArray(q, "or", s.or);
10474
+ if (!s)
10475
+ throw new Error(`Scope ${scope} is not defined`);
10429
10476
  setQueryObjectValue(q, "scopes", scope, s);
10430
10477
  }
10431
10478
  return q;
@@ -10443,23 +10490,13 @@ class ScopeMethods {
10443
10490
  * @param scope - name of the scope to remove from the query
10444
10491
  */
10445
10492
  unscope(scope) {
10446
- var _a;
10447
10493
  const q = this.clone();
10448
- const data = q.q;
10449
- const s = (_a = q.q.scopes) == null ? void 0 : _a[scope];
10450
- if (s) {
10451
- const { and, or } = s;
10452
- if (and) {
10453
- data.and = data.and.filter((x) => !and.includes(x));
10454
- if (!data.and.length)
10455
- delete data.and;
10456
- }
10457
- if (or) {
10458
- data.or = data.or.filter((x) => !or.includes(x));
10459
- if (!data.or.length)
10460
- delete data.or;
10461
- }
10494
+ if (q.q.scopes) {
10462
10495
  delete q.q.scopes[scope];
10496
+ for (const _ in q.q.scopes) {
10497
+ return q;
10498
+ }
10499
+ delete q.q.scopes;
10463
10500
  }
10464
10501
  return q;
10465
10502
  }
@@ -10477,7 +10514,6 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
10477
10514
  and: [{ [column]: null }]
10478
10515
  };
10479
10516
  scopes.deleted = scope;
10480
- pushQueryValue(q, "and", scope.and[0]);
10481
10517
  ((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
10482
10518
  const _del = _softDelete(column);
10483
10519
  q.baseQuery.delete = function() {
@@ -11642,15 +11678,12 @@ class Db {
11642
11678
  if (options.scopes) {
11643
11679
  for (const key in options.scopes) {
11644
11680
  const q = options.scopes[key](this).q;
11645
- const s = {};
11646
- if (q.and)
11647
- s.and = q.and;
11648
- if (q.or)
11649
- s.or = q.or;
11650
- scopes[key] = s;
11681
+ scopes[key] = {
11682
+ and: q.and,
11683
+ or: q.or
11684
+ };
11651
11685
  }
11652
11686
  if (scopes.default) {
11653
- Object.assign(this.q, scopes.default);
11654
11687
  this.q.scopes = { default: scopes.default };
11655
11688
  }
11656
11689
  }