pqb 0.31.2 → 0.31.4

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
@@ -1937,7 +1937,7 @@ const setQueryObjectValue = (q, object, key, value) => {
1937
1937
  return q;
1938
1938
  };
1939
1939
  const throwIfNoWhere = (q, method) => {
1940
- if (!q.q.or && !q.q.and && !q.q.all) {
1940
+ if (!q.q.or && !q.q.and && !q.q.scopes && !q.q.all) {
1941
1941
  throw new OrchidOrmInternalError(
1942
1942
  q,
1943
1943
  `Dangerous ${method} without conditions`
@@ -1994,6 +1994,22 @@ const pushWhereToSql = (sql, ctx, table, query, quotedAs, parens) => {
1994
1994
  }
1995
1995
  };
1996
1996
  const whereToSql = (ctx, table, query, quotedAs, parens) => {
1997
+ if (query.scopes) {
1998
+ let sql = andOrToSql(ctx, table, query, quotedAs, true);
1999
+ const data = Object.create(query);
2000
+ for (const key in query.scopes) {
2001
+ const scope = query.scopes[key];
2002
+ data.and = scope.and;
2003
+ data.or = scope.or;
2004
+ const scopeSql = andOrToSql(ctx, table, data, quotedAs, true);
2005
+ if (scopeSql)
2006
+ sql = sql ? sql + " AND " + scopeSql : scopeSql;
2007
+ }
2008
+ return sql;
2009
+ }
2010
+ return andOrToSql(ctx, table, query, quotedAs, parens);
2011
+ };
2012
+ const andOrToSql = (ctx, table, query, quotedAs, parens) => {
1997
2013
  var _a;
1998
2014
  let sql;
1999
2015
  if (query.or) {
@@ -2016,42 +2032,21 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
2016
2032
  };
2017
2033
  const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2018
2034
  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
2035
  if ("prototype" in data || "baseQuery" in data) {
2046
2036
  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})`);
2037
+ if (query2.q.expr) {
2038
+ const q = joinSubQuery(table, query2);
2039
+ q.q.select = [query2.q.expr];
2040
+ ands.push(`(${makeSQL(q, ctx).text})`);
2041
+ } else {
2042
+ pushWhereToSql(
2043
+ ands,
2044
+ ctx,
2045
+ query2,
2046
+ query2.q,
2047
+ query2.table && `"${query2.table}"`,
2048
+ true
2049
+ );
2055
2050
  }
2056
2051
  return;
2057
2052
  }
@@ -2069,7 +2064,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2069
2064
  } else if (key === "OR") {
2070
2065
  const arr = value.map(toArray);
2071
2066
  ands.push(
2072
- arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")
2067
+ `(${arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")})`
2073
2068
  );
2074
2069
  } else if (key === "NOT") {
2075
2070
  const arr = toArray(value);
@@ -2553,6 +2548,9 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2553
2548
  if (query.or) {
2554
2549
  pushQueryArray(q, "or", query.or);
2555
2550
  }
2551
+ if (query.scopes) {
2552
+ q.q.scopes = __spreadValues$c(__spreadValues$c({}, q.q.scopes), query.scopes);
2553
+ }
2556
2554
  }
2557
2555
  const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2558
2556
  [joinTo.q.as || joinTo.table]: joinTo.shape
@@ -3791,8 +3789,7 @@ function queryWrap(self, query, as = "t") {
3791
3789
  }
3792
3790
  function cloneQueryBaseUnscoped(query) {
3793
3791
  const q = query.baseQuery.clone();
3794
- q.q.or = q.q.and = void 0;
3795
- q.q.scopes = emptyObject;
3792
+ q.q.or = q.q.and = q.q.scopes = void 0;
3796
3793
  return q;
3797
3794
  }
3798
3795
 
@@ -4092,7 +4089,7 @@ const pushWithSql = (ctx, items) => {
4092
4089
 
4093
4090
  const checkIfASimpleQuery = (q) => {
4094
4091
  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))
4092
+ 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
4093
  return false;
4097
4094
  const keys = Object.keys(q.q);
4098
4095
  return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
@@ -4579,7 +4576,7 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
4579
4576
  }
4580
4577
  pushWhereStatementSql(ctx, table, query, quotedAs);
4581
4578
  if (conditions) {
4582
- if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length)) {
4579
+ if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length) || query.scopes) {
4583
4580
  ctx.sql.push("AND", conditions);
4584
4581
  } else {
4585
4582
  ctx.sql.push("WHERE", conditions);
@@ -4782,7 +4779,7 @@ const makeSQL = (table, options) => {
4782
4779
  quotedAs
4783
4780
  );
4784
4781
  }
4785
- if (query.and || query.or) {
4782
+ if (query.and || query.or || query.scopes) {
4786
4783
  pushWhereStatementSql(ctx, table, query, quotedAs);
4787
4784
  }
4788
4785
  if (query.group) {
@@ -5360,6 +5357,7 @@ class TransactionAdapter {
5360
5357
  this.types = types2;
5361
5358
  this.pool = adapter.pool;
5362
5359
  this.config = adapter.config;
5360
+ this.schema = adapter.schema;
5363
5361
  }
5364
5362
  connect() {
5365
5363
  return Promise.resolve(this.client);
@@ -6203,6 +6201,7 @@ const insert = (self, {
6203
6201
  const { q } = self;
6204
6202
  delete q.and;
6205
6203
  delete q.or;
6204
+ delete q.scopes;
6206
6205
  q.type = "insert";
6207
6206
  q.columns = columns;
6208
6207
  q.values = values;
@@ -8562,7 +8561,20 @@ class WithMethods {
8562
8561
  }
8563
8562
  }
8564
8563
 
8564
+ const resolveCallbacksInArgs = (q, args) => {
8565
+ for (let i = 0; i < args.length; i++) {
8566
+ const arg = args[i];
8567
+ if (typeof arg === "function") {
8568
+ const qb = Object.create(q);
8569
+ qb.q = getClonedQueryData(q.q);
8570
+ qb.q.and = qb.q.or = qb.q.scopes = void 0;
8571
+ qb.q.isSubQuery = true;
8572
+ args[i] = resolveSubQueryCallback(qb, arg);
8573
+ }
8574
+ }
8575
+ };
8565
8576
  const _queryWhere = (q, args) => {
8577
+ resolveCallbacksInArgs(q, args);
8566
8578
  return pushQueryArray(
8567
8579
  q,
8568
8580
  "and",
@@ -8577,6 +8589,7 @@ const _queryWhereSql = (q, args) => {
8577
8589
  );
8578
8590
  };
8579
8591
  const _queryWhereNot = (q, args) => {
8592
+ resolveCallbacksInArgs(q, args);
8580
8593
  return pushQueryValue(q, "and", {
8581
8594
  NOT: args
8582
8595
  });
@@ -8587,6 +8600,7 @@ const _queryWhereNotSql = (q, args) => {
8587
8600
  });
8588
8601
  };
8589
8602
  const _queryOr = (q, args) => {
8603
+ resolveCallbacksInArgs(q, args);
8590
8604
  return pushQueryArray(
8591
8605
  q,
8592
8606
  "or",
@@ -8594,10 +8608,13 @@ const _queryOr = (q, args) => {
8594
8608
  );
8595
8609
  };
8596
8610
  const _queryOrNot = (q, args) => {
8611
+ resolveCallbacksInArgs(q, args);
8597
8612
  return pushQueryArray(
8598
8613
  q,
8599
8614
  "or",
8600
- args.map((item) => [{ NOT: item }])
8615
+ args.map((item) => {
8616
+ return [{ NOT: item }];
8617
+ })
8601
8618
  );
8602
8619
  };
8603
8620
  const _queryWhereIn = (q, and, arg, values, not) => {
@@ -10419,10 +10436,8 @@ class ScopeMethods {
10419
10436
  const q = this.clone();
10420
10437
  if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
10421
10438
  const s = q.internal.scopes[scope];
10422
- if (s.and)
10423
- pushQueryArray(q, "and", s.and);
10424
- if (s.or)
10425
- pushQueryArray(q, "or", s.or);
10439
+ if (!s)
10440
+ throw new Error(`Scope ${scope} is not defined`);
10426
10441
  setQueryObjectValue(q, "scopes", scope, s);
10427
10442
  }
10428
10443
  return q;
@@ -10440,23 +10455,13 @@ class ScopeMethods {
10440
10455
  * @param scope - name of the scope to remove from the query
10441
10456
  */
10442
10457
  unscope(scope) {
10443
- var _a;
10444
10458
  const q = this.clone();
10445
- const data = q.q;
10446
- const s = (_a = q.q.scopes) == null ? void 0 : _a[scope];
10447
- if (s) {
10448
- const { and, or } = s;
10449
- if (and) {
10450
- data.and = data.and.filter((x) => !and.includes(x));
10451
- if (!data.and.length)
10452
- delete data.and;
10453
- }
10454
- if (or) {
10455
- data.or = data.or.filter((x) => !or.includes(x));
10456
- if (!data.or.length)
10457
- delete data.or;
10458
- }
10459
+ if (q.q.scopes) {
10459
10460
  delete q.q.scopes[scope];
10461
+ for (const _ in q.q.scopes) {
10462
+ return q;
10463
+ }
10464
+ delete q.q.scopes;
10460
10465
  }
10461
10466
  return q;
10462
10467
  }
@@ -10474,7 +10479,6 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
10474
10479
  and: [{ [column]: null }]
10475
10480
  };
10476
10481
  scopes.deleted = scope;
10477
- pushQueryValue(q, "and", scope.and[0]);
10478
10482
  ((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
10479
10483
  const _del = _softDelete(column);
10480
10484
  q.baseQuery.delete = function() {
@@ -11639,15 +11643,12 @@ class Db {
11639
11643
  if (options.scopes) {
11640
11644
  for (const key in options.scopes) {
11641
11645
  const q = options.scopes[key](this).q;
11642
- const s = {};
11643
- if (q.and)
11644
- s.and = q.and;
11645
- if (q.or)
11646
- s.or = q.or;
11647
- scopes[key] = s;
11646
+ scopes[key] = {
11647
+ and: q.and,
11648
+ or: q.or
11649
+ };
11648
11650
  }
11649
11651
  if (scopes.default) {
11650
- Object.assign(this.q, scopes.default);
11651
11652
  this.q.scopes = { default: scopes.default };
11652
11653
  }
11653
11654
  }