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.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
|
-
|
|
2048
|
-
|
|
2049
|
-
query2
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
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) {
|
|
@@ -6204,6 +6201,7 @@ const insert = (self, {
|
|
|
6204
6201
|
const { q } = self;
|
|
6205
6202
|
delete q.and;
|
|
6206
6203
|
delete q.or;
|
|
6204
|
+
delete q.scopes;
|
|
6207
6205
|
q.type = "insert";
|
|
6208
6206
|
q.columns = columns;
|
|
6209
6207
|
q.values = values;
|
|
@@ -8563,7 +8561,20 @@ class WithMethods {
|
|
|
8563
8561
|
}
|
|
8564
8562
|
}
|
|
8565
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
|
+
};
|
|
8566
8576
|
const _queryWhere = (q, args) => {
|
|
8577
|
+
resolveCallbacksInArgs(q, args);
|
|
8567
8578
|
return pushQueryArray(
|
|
8568
8579
|
q,
|
|
8569
8580
|
"and",
|
|
@@ -8578,6 +8589,7 @@ const _queryWhereSql = (q, args) => {
|
|
|
8578
8589
|
);
|
|
8579
8590
|
};
|
|
8580
8591
|
const _queryWhereNot = (q, args) => {
|
|
8592
|
+
resolveCallbacksInArgs(q, args);
|
|
8581
8593
|
return pushQueryValue(q, "and", {
|
|
8582
8594
|
NOT: args
|
|
8583
8595
|
});
|
|
@@ -8588,6 +8600,7 @@ const _queryWhereNotSql = (q, args) => {
|
|
|
8588
8600
|
});
|
|
8589
8601
|
};
|
|
8590
8602
|
const _queryOr = (q, args) => {
|
|
8603
|
+
resolveCallbacksInArgs(q, args);
|
|
8591
8604
|
return pushQueryArray(
|
|
8592
8605
|
q,
|
|
8593
8606
|
"or",
|
|
@@ -8595,10 +8608,13 @@ const _queryOr = (q, args) => {
|
|
|
8595
8608
|
);
|
|
8596
8609
|
};
|
|
8597
8610
|
const _queryOrNot = (q, args) => {
|
|
8611
|
+
resolveCallbacksInArgs(q, args);
|
|
8598
8612
|
return pushQueryArray(
|
|
8599
8613
|
q,
|
|
8600
8614
|
"or",
|
|
8601
|
-
args.map((item) =>
|
|
8615
|
+
args.map((item) => {
|
|
8616
|
+
return [{ NOT: item }];
|
|
8617
|
+
})
|
|
8602
8618
|
);
|
|
8603
8619
|
};
|
|
8604
8620
|
const _queryWhereIn = (q, and, arg, values, not) => {
|
|
@@ -10420,10 +10436,8 @@ class ScopeMethods {
|
|
|
10420
10436
|
const q = this.clone();
|
|
10421
10437
|
if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
|
|
10422
10438
|
const s = q.internal.scopes[scope];
|
|
10423
|
-
if (s
|
|
10424
|
-
|
|
10425
|
-
if (s.or)
|
|
10426
|
-
pushQueryArray(q, "or", s.or);
|
|
10439
|
+
if (!s)
|
|
10440
|
+
throw new Error(`Scope ${scope} is not defined`);
|
|
10427
10441
|
setQueryObjectValue(q, "scopes", scope, s);
|
|
10428
10442
|
}
|
|
10429
10443
|
return q;
|
|
@@ -10441,23 +10455,13 @@ class ScopeMethods {
|
|
|
10441
10455
|
* @param scope - name of the scope to remove from the query
|
|
10442
10456
|
*/
|
|
10443
10457
|
unscope(scope) {
|
|
10444
|
-
var _a;
|
|
10445
10458
|
const q = this.clone();
|
|
10446
|
-
|
|
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
|
-
}
|
|
10459
|
+
if (q.q.scopes) {
|
|
10460
10460
|
delete q.q.scopes[scope];
|
|
10461
|
+
for (const _ in q.q.scopes) {
|
|
10462
|
+
return q;
|
|
10463
|
+
}
|
|
10464
|
+
delete q.q.scopes;
|
|
10461
10465
|
}
|
|
10462
10466
|
return q;
|
|
10463
10467
|
}
|
|
@@ -10475,7 +10479,6 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
|
|
|
10475
10479
|
and: [{ [column]: null }]
|
|
10476
10480
|
};
|
|
10477
10481
|
scopes.deleted = scope;
|
|
10478
|
-
pushQueryValue(q, "and", scope.and[0]);
|
|
10479
10482
|
((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
|
|
10480
10483
|
const _del = _softDelete(column);
|
|
10481
10484
|
q.baseQuery.delete = function() {
|
|
@@ -11640,15 +11643,12 @@ class Db {
|
|
|
11640
11643
|
if (options.scopes) {
|
|
11641
11644
|
for (const key in options.scopes) {
|
|
11642
11645
|
const q = options.scopes[key](this).q;
|
|
11643
|
-
|
|
11644
|
-
|
|
11645
|
-
|
|
11646
|
-
|
|
11647
|
-
s.or = q.or;
|
|
11648
|
-
scopes[key] = s;
|
|
11646
|
+
scopes[key] = {
|
|
11647
|
+
and: q.and,
|
|
11648
|
+
or: q.or
|
|
11649
|
+
};
|
|
11649
11650
|
}
|
|
11650
11651
|
if (scopes.default) {
|
|
11651
|
-
Object.assign(this.q, scopes.default);
|
|
11652
11652
|
this.q.scopes = { default: scopes.default };
|
|
11653
11653
|
}
|
|
11654
11654
|
}
|