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.d.ts +2 -1
- package/dist/index.js +70 -69
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -46,6 +46,7 @@ declare class TransactionAdapter implements Adapter {
|
|
|
46
46
|
types: TypeParsers;
|
|
47
47
|
pool: Pool;
|
|
48
48
|
config: PoolConfig;
|
|
49
|
+
schema?: string;
|
|
49
50
|
constructor(adapter: Adapter, client: PoolClient, types: TypeParsers);
|
|
50
51
|
connect(): Promise<PoolClient>;
|
|
51
52
|
query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers): Promise<QueryResult<T>>;
|
|
@@ -454,7 +455,7 @@ interface CommonQueryData {
|
|
|
454
455
|
* Is needed to remove these operators from query object when changing the query type, see {@link setQueryOperators}.
|
|
455
456
|
*/
|
|
456
457
|
operators?: BaseOperators;
|
|
457
|
-
scopes
|
|
458
|
+
scopes?: {
|
|
458
459
|
[K: string]: QueryScopeData;
|
|
459
460
|
};
|
|
460
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) {
|
|
@@ -5362,6 +5359,7 @@ class TransactionAdapter {
|
|
|
5362
5359
|
this.types = types2;
|
|
5363
5360
|
this.pool = adapter.pool;
|
|
5364
5361
|
this.config = adapter.config;
|
|
5362
|
+
this.schema = adapter.schema;
|
|
5365
5363
|
}
|
|
5366
5364
|
connect() {
|
|
5367
5365
|
return Promise.resolve(this.client);
|
|
@@ -6205,6 +6203,7 @@ const insert = (self, {
|
|
|
6205
6203
|
const { q } = self;
|
|
6206
6204
|
delete q.and;
|
|
6207
6205
|
delete q.or;
|
|
6206
|
+
delete q.scopes;
|
|
6208
6207
|
q.type = "insert";
|
|
6209
6208
|
q.columns = columns;
|
|
6210
6209
|
q.values = values;
|
|
@@ -8564,7 +8563,20 @@ class WithMethods {
|
|
|
8564
8563
|
}
|
|
8565
8564
|
}
|
|
8566
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
|
+
};
|
|
8567
8578
|
const _queryWhere = (q, args) => {
|
|
8579
|
+
resolveCallbacksInArgs(q, args);
|
|
8568
8580
|
return pushQueryArray(
|
|
8569
8581
|
q,
|
|
8570
8582
|
"and",
|
|
@@ -8579,6 +8591,7 @@ const _queryWhereSql = (q, args) => {
|
|
|
8579
8591
|
);
|
|
8580
8592
|
};
|
|
8581
8593
|
const _queryWhereNot = (q, args) => {
|
|
8594
|
+
resolveCallbacksInArgs(q, args);
|
|
8582
8595
|
return pushQueryValue(q, "and", {
|
|
8583
8596
|
NOT: args
|
|
8584
8597
|
});
|
|
@@ -8589,6 +8602,7 @@ const _queryWhereNotSql = (q, args) => {
|
|
|
8589
8602
|
});
|
|
8590
8603
|
};
|
|
8591
8604
|
const _queryOr = (q, args) => {
|
|
8605
|
+
resolveCallbacksInArgs(q, args);
|
|
8592
8606
|
return pushQueryArray(
|
|
8593
8607
|
q,
|
|
8594
8608
|
"or",
|
|
@@ -8596,10 +8610,13 @@ const _queryOr = (q, args) => {
|
|
|
8596
8610
|
);
|
|
8597
8611
|
};
|
|
8598
8612
|
const _queryOrNot = (q, args) => {
|
|
8613
|
+
resolveCallbacksInArgs(q, args);
|
|
8599
8614
|
return pushQueryArray(
|
|
8600
8615
|
q,
|
|
8601
8616
|
"or",
|
|
8602
|
-
args.map((item) =>
|
|
8617
|
+
args.map((item) => {
|
|
8618
|
+
return [{ NOT: item }];
|
|
8619
|
+
})
|
|
8603
8620
|
);
|
|
8604
8621
|
};
|
|
8605
8622
|
const _queryWhereIn = (q, and, arg, values, not) => {
|
|
@@ -10421,10 +10438,8 @@ class ScopeMethods {
|
|
|
10421
10438
|
const q = this.clone();
|
|
10422
10439
|
if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
|
|
10423
10440
|
const s = q.internal.scopes[scope];
|
|
10424
|
-
if (s
|
|
10425
|
-
|
|
10426
|
-
if (s.or)
|
|
10427
|
-
pushQueryArray(q, "or", s.or);
|
|
10441
|
+
if (!s)
|
|
10442
|
+
throw new Error(`Scope ${scope} is not defined`);
|
|
10428
10443
|
setQueryObjectValue(q, "scopes", scope, s);
|
|
10429
10444
|
}
|
|
10430
10445
|
return q;
|
|
@@ -10442,23 +10457,13 @@ class ScopeMethods {
|
|
|
10442
10457
|
* @param scope - name of the scope to remove from the query
|
|
10443
10458
|
*/
|
|
10444
10459
|
unscope(scope) {
|
|
10445
|
-
var _a;
|
|
10446
10460
|
const q = this.clone();
|
|
10447
|
-
|
|
10448
|
-
const s = (_a = q.q.scopes) == null ? void 0 : _a[scope];
|
|
10449
|
-
if (s) {
|
|
10450
|
-
const { and, or } = s;
|
|
10451
|
-
if (and) {
|
|
10452
|
-
data.and = data.and.filter((x) => !and.includes(x));
|
|
10453
|
-
if (!data.and.length)
|
|
10454
|
-
delete data.and;
|
|
10455
|
-
}
|
|
10456
|
-
if (or) {
|
|
10457
|
-
data.or = data.or.filter((x) => !or.includes(x));
|
|
10458
|
-
if (!data.or.length)
|
|
10459
|
-
delete data.or;
|
|
10460
|
-
}
|
|
10461
|
+
if (q.q.scopes) {
|
|
10461
10462
|
delete q.q.scopes[scope];
|
|
10463
|
+
for (const _ in q.q.scopes) {
|
|
10464
|
+
return q;
|
|
10465
|
+
}
|
|
10466
|
+
delete q.q.scopes;
|
|
10462
10467
|
}
|
|
10463
10468
|
return q;
|
|
10464
10469
|
}
|
|
@@ -10476,7 +10481,6 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
|
|
|
10476
10481
|
and: [{ [column]: null }]
|
|
10477
10482
|
};
|
|
10478
10483
|
scopes.deleted = scope;
|
|
10479
|
-
pushQueryValue(q, "and", scope.and[0]);
|
|
10480
10484
|
((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
|
|
10481
10485
|
const _del = _softDelete(column);
|
|
10482
10486
|
q.baseQuery.delete = function() {
|
|
@@ -11641,15 +11645,12 @@ class Db {
|
|
|
11641
11645
|
if (options.scopes) {
|
|
11642
11646
|
for (const key in options.scopes) {
|
|
11643
11647
|
const q = options.scopes[key](this).q;
|
|
11644
|
-
|
|
11645
|
-
|
|
11646
|
-
|
|
11647
|
-
|
|
11648
|
-
s.or = q.or;
|
|
11649
|
-
scopes[key] = s;
|
|
11648
|
+
scopes[key] = {
|
|
11649
|
+
and: q.and,
|
|
11650
|
+
or: q.or
|
|
11651
|
+
};
|
|
11650
11652
|
}
|
|
11651
11653
|
if (scopes.default) {
|
|
11652
|
-
Object.assign(this.q, scopes.default);
|
|
11653
11654
|
this.q.scopes = { default: scopes.default };
|
|
11654
11655
|
}
|
|
11655
11656
|
}
|