pqb 0.31.3 → 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.d.ts +1 -1
- package/dist/index.js +69 -69
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
@@ -1939,7 +1939,7 @@ const setQueryObjectValue = (q, object, key, value) => {
|
|
|
1939
1939
|
return q;
|
|
1940
1940
|
};
|
|
1941
1941
|
const throwIfNoWhere = (q, method) => {
|
|
1942
|
-
if (!q.q.or && !q.q.and && !q.q.all) {
|
|
1942
|
+
if (!q.q.or && !q.q.and && !q.q.scopes && !q.q.all) {
|
|
1943
1943
|
throw new OrchidOrmInternalError(
|
|
1944
1944
|
q,
|
|
1945
1945
|
`Dangerous ${method} without conditions`
|
|
@@ -1996,6 +1996,22 @@ const pushWhereToSql = (sql, ctx, table, query, quotedAs, parens) => {
|
|
|
1996
1996
|
}
|
|
1997
1997
|
};
|
|
1998
1998
|
const whereToSql = (ctx, table, query, quotedAs, parens) => {
|
|
1999
|
+
if (query.scopes) {
|
|
2000
|
+
let sql = andOrToSql(ctx, table, query, quotedAs, true);
|
|
2001
|
+
const data = Object.create(query);
|
|
2002
|
+
for (const key in query.scopes) {
|
|
2003
|
+
const scope = query.scopes[key];
|
|
2004
|
+
data.and = scope.and;
|
|
2005
|
+
data.or = scope.or;
|
|
2006
|
+
const scopeSql = andOrToSql(ctx, table, data, quotedAs, true);
|
|
2007
|
+
if (scopeSql)
|
|
2008
|
+
sql = sql ? sql + " AND " + scopeSql : scopeSql;
|
|
2009
|
+
}
|
|
2010
|
+
return sql;
|
|
2011
|
+
}
|
|
2012
|
+
return andOrToSql(ctx, table, query, quotedAs, parens);
|
|
2013
|
+
};
|
|
2014
|
+
const andOrToSql = (ctx, table, query, quotedAs, parens) => {
|
|
1999
2015
|
var _a;
|
|
2000
2016
|
let sql;
|
|
2001
2017
|
if (query.or) {
|
|
@@ -2018,42 +2034,21 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
|
|
|
2018
2034
|
};
|
|
2019
2035
|
const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
2020
2036
|
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
2037
|
if ("prototype" in data || "baseQuery" in data) {
|
|
2048
2038
|
const query2 = data;
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
query2
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2039
|
+
if (query2.q.expr) {
|
|
2040
|
+
const q = joinSubQuery(table, query2);
|
|
2041
|
+
q.q.select = [query2.q.expr];
|
|
2042
|
+
ands.push(`(${makeSQL(q, ctx).text})`);
|
|
2043
|
+
} else {
|
|
2044
|
+
pushWhereToSql(
|
|
2045
|
+
ands,
|
|
2046
|
+
ctx,
|
|
2047
|
+
query2,
|
|
2048
|
+
query2.q,
|
|
2049
|
+
query2.table && `"${query2.table}"`,
|
|
2050
|
+
true
|
|
2051
|
+
);
|
|
2057
2052
|
}
|
|
2058
2053
|
return;
|
|
2059
2054
|
}
|
|
@@ -2071,7 +2066,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2071
2066
|
} else if (key === "OR") {
|
|
2072
2067
|
const arr = value.map(orchidCore.toArray);
|
|
2073
2068
|
ands.push(
|
|
2074
|
-
arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")
|
|
2069
|
+
`(${arr.map((and) => processAnds(and, ctx, table, query, quotedAs)).join(" OR ")})`
|
|
2075
2070
|
);
|
|
2076
2071
|
} else if (key === "NOT") {
|
|
2077
2072
|
const arr = orchidCore.toArray(value);
|
|
@@ -2555,6 +2550,9 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
|
|
|
2555
2550
|
if (query.or) {
|
|
2556
2551
|
pushQueryArray(q, "or", query.or);
|
|
2557
2552
|
}
|
|
2553
|
+
if (query.scopes) {
|
|
2554
|
+
q.q.scopes = __spreadValues$c(__spreadValues$c({}, q.q.scopes), query.scopes);
|
|
2555
|
+
}
|
|
2558
2556
|
}
|
|
2559
2557
|
const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
|
|
2560
2558
|
[joinTo.q.as || joinTo.table]: joinTo.shape
|
|
@@ -3793,8 +3791,7 @@ function queryWrap(self, query, as = "t") {
|
|
|
3793
3791
|
}
|
|
3794
3792
|
function cloneQueryBaseUnscoped(query) {
|
|
3795
3793
|
const q = query.baseQuery.clone();
|
|
3796
|
-
q.q.or = q.q.and = void 0;
|
|
3797
|
-
q.q.scopes = orchidCore.emptyObject;
|
|
3794
|
+
q.q.or = q.q.and = q.q.scopes = void 0;
|
|
3798
3795
|
return q;
|
|
3799
3796
|
}
|
|
3800
3797
|
|
|
@@ -4094,7 +4091,7 @@ const pushWithSql = (ctx, items) => {
|
|
|
4094
4091
|
|
|
4095
4092
|
const checkIfASimpleQuery = (q) => {
|
|
4096
4093
|
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))
|
|
4094
|
+
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
4095
|
return false;
|
|
4099
4096
|
const keys = Object.keys(q.q);
|
|
4100
4097
|
return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
|
|
@@ -4581,7 +4578,7 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
|
4581
4578
|
}
|
|
4582
4579
|
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
4583
4580
|
if (conditions) {
|
|
4584
|
-
if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length)) {
|
|
4581
|
+
if (((_c = query.and) == null ? void 0 : _c.length) || ((_d = query.or) == null ? void 0 : _d.length) || query.scopes) {
|
|
4585
4582
|
ctx.sql.push("AND", conditions);
|
|
4586
4583
|
} else {
|
|
4587
4584
|
ctx.sql.push("WHERE", conditions);
|
|
@@ -4784,7 +4781,7 @@ const makeSQL = (table, options) => {
|
|
|
4784
4781
|
quotedAs
|
|
4785
4782
|
);
|
|
4786
4783
|
}
|
|
4787
|
-
if (query.and || query.or) {
|
|
4784
|
+
if (query.and || query.or || query.scopes) {
|
|
4788
4785
|
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
4789
4786
|
}
|
|
4790
4787
|
if (query.group) {
|
|
@@ -6206,6 +6203,7 @@ const insert = (self, {
|
|
|
6206
6203
|
const { q } = self;
|
|
6207
6204
|
delete q.and;
|
|
6208
6205
|
delete q.or;
|
|
6206
|
+
delete q.scopes;
|
|
6209
6207
|
q.type = "insert";
|
|
6210
6208
|
q.columns = columns;
|
|
6211
6209
|
q.values = values;
|
|
@@ -8565,7 +8563,20 @@ class WithMethods {
|
|
|
8565
8563
|
}
|
|
8566
8564
|
}
|
|
8567
8565
|
|
|
8566
|
+
const resolveCallbacksInArgs = (q, args) => {
|
|
8567
|
+
for (let i = 0; i < args.length; i++) {
|
|
8568
|
+
const arg = args[i];
|
|
8569
|
+
if (typeof arg === "function") {
|
|
8570
|
+
const qb = Object.create(q);
|
|
8571
|
+
qb.q = getClonedQueryData(q.q);
|
|
8572
|
+
qb.q.and = qb.q.or = qb.q.scopes = void 0;
|
|
8573
|
+
qb.q.isSubQuery = true;
|
|
8574
|
+
args[i] = resolveSubQueryCallback(qb, arg);
|
|
8575
|
+
}
|
|
8576
|
+
}
|
|
8577
|
+
};
|
|
8568
8578
|
const _queryWhere = (q, args) => {
|
|
8579
|
+
resolveCallbacksInArgs(q, args);
|
|
8569
8580
|
return pushQueryArray(
|
|
8570
8581
|
q,
|
|
8571
8582
|
"and",
|
|
@@ -8580,6 +8591,7 @@ const _queryWhereSql = (q, args) => {
|
|
|
8580
8591
|
);
|
|
8581
8592
|
};
|
|
8582
8593
|
const _queryWhereNot = (q, args) => {
|
|
8594
|
+
resolveCallbacksInArgs(q, args);
|
|
8583
8595
|
return pushQueryValue(q, "and", {
|
|
8584
8596
|
NOT: args
|
|
8585
8597
|
});
|
|
@@ -8590,6 +8602,7 @@ const _queryWhereNotSql = (q, args) => {
|
|
|
8590
8602
|
});
|
|
8591
8603
|
};
|
|
8592
8604
|
const _queryOr = (q, args) => {
|
|
8605
|
+
resolveCallbacksInArgs(q, args);
|
|
8593
8606
|
return pushQueryArray(
|
|
8594
8607
|
q,
|
|
8595
8608
|
"or",
|
|
@@ -8597,10 +8610,13 @@ const _queryOr = (q, args) => {
|
|
|
8597
8610
|
);
|
|
8598
8611
|
};
|
|
8599
8612
|
const _queryOrNot = (q, args) => {
|
|
8613
|
+
resolveCallbacksInArgs(q, args);
|
|
8600
8614
|
return pushQueryArray(
|
|
8601
8615
|
q,
|
|
8602
8616
|
"or",
|
|
8603
|
-
args.map((item) =>
|
|
8617
|
+
args.map((item) => {
|
|
8618
|
+
return [{ NOT: item }];
|
|
8619
|
+
})
|
|
8604
8620
|
);
|
|
8605
8621
|
};
|
|
8606
8622
|
const _queryWhereIn = (q, and, arg, values, not) => {
|
|
@@ -10422,10 +10438,8 @@ class ScopeMethods {
|
|
|
10422
10438
|
const q = this.clone();
|
|
10423
10439
|
if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
|
|
10424
10440
|
const s = q.internal.scopes[scope];
|
|
10425
|
-
if (s
|
|
10426
|
-
|
|
10427
|
-
if (s.or)
|
|
10428
|
-
pushQueryArray(q, "or", s.or);
|
|
10441
|
+
if (!s)
|
|
10442
|
+
throw new Error(`Scope ${scope} is not defined`);
|
|
10429
10443
|
setQueryObjectValue(q, "scopes", scope, s);
|
|
10430
10444
|
}
|
|
10431
10445
|
return q;
|
|
@@ -10443,23 +10457,13 @@ class ScopeMethods {
|
|
|
10443
10457
|
* @param scope - name of the scope to remove from the query
|
|
10444
10458
|
*/
|
|
10445
10459
|
unscope(scope) {
|
|
10446
|
-
var _a;
|
|
10447
10460
|
const q = this.clone();
|
|
10448
|
-
|
|
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
|
-
}
|
|
10461
|
+
if (q.q.scopes) {
|
|
10462
10462
|
delete q.q.scopes[scope];
|
|
10463
|
+
for (const _ in q.q.scopes) {
|
|
10464
|
+
return q;
|
|
10465
|
+
}
|
|
10466
|
+
delete q.q.scopes;
|
|
10463
10467
|
}
|
|
10464
10468
|
return q;
|
|
10465
10469
|
}
|
|
@@ -10477,7 +10481,6 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
|
|
|
10477
10481
|
and: [{ [column]: null }]
|
|
10478
10482
|
};
|
|
10479
10483
|
scopes.deleted = scope;
|
|
10480
|
-
pushQueryValue(q, "and", scope.and[0]);
|
|
10481
10484
|
((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
|
|
10482
10485
|
const _del = _softDelete(column);
|
|
10483
10486
|
q.baseQuery.delete = function() {
|
|
@@ -11642,15 +11645,12 @@ class Db {
|
|
|
11642
11645
|
if (options.scopes) {
|
|
11643
11646
|
for (const key in options.scopes) {
|
|
11644
11647
|
const q = options.scopes[key](this).q;
|
|
11645
|
-
|
|
11646
|
-
|
|
11647
|
-
|
|
11648
|
-
|
|
11649
|
-
s.or = q.or;
|
|
11650
|
-
scopes[key] = s;
|
|
11648
|
+
scopes[key] = {
|
|
11649
|
+
and: q.and,
|
|
11650
|
+
or: q.or
|
|
11651
|
+
};
|
|
11651
11652
|
}
|
|
11652
11653
|
if (scopes.default) {
|
|
11653
|
-
Object.assign(this.q, scopes.default);
|
|
11654
11654
|
this.q.scopes = { default: scopes.default };
|
|
11655
11655
|
}
|
|
11656
11656
|
}
|