pqb 0.18.34 → 0.19.1
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 +2645 -2558
- package/dist/index.js +100 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +100 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1072,7 +1072,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs, not) => {
|
|
|
1072
1072
|
const res = data(qb);
|
|
1073
1073
|
const expr = res instanceof Expression ? res : res.q.expr;
|
|
1074
1074
|
if (!(res instanceof Expression) && res.q.expr) {
|
|
1075
|
-
const q = "relationConfig" in res ? res.relationConfig.joinQuery(
|
|
1075
|
+
const q = "relationConfig" in res ? res.relationConfig.joinQuery(res, table) : res.clone();
|
|
1076
1076
|
q.q.select = [expr];
|
|
1077
1077
|
ands.push(`${prefix}(${makeSQL(q, ctx).text})`);
|
|
1078
1078
|
} else {
|
|
@@ -1306,7 +1306,7 @@ const processJoinItem = (ctx, table, query, item, quotedAs) => {
|
|
|
1306
1306
|
if (typeof first === "string") {
|
|
1307
1307
|
if (first in table.relations) {
|
|
1308
1308
|
const { query: toQuery, joinQuery } = table.relations[first].relationConfig;
|
|
1309
|
-
const jq = joinQuery(
|
|
1309
|
+
const jq = joinQuery(toQuery, table);
|
|
1310
1310
|
const { q: j } = jq;
|
|
1311
1311
|
const tableName = typeof j.from === "string" ? j.from : jq.table;
|
|
1312
1312
|
target = quoteSchemaAndTable(j.schema, tableName);
|
|
@@ -1434,8 +1434,8 @@ const processArgs = (args, ctx, table, query, first, joinAs, joinShape, quotedAs
|
|
|
1434
1434
|
base = base.as(q.q.as);
|
|
1435
1435
|
}
|
|
1436
1436
|
const { q: query2 } = first.joinQueryAfterCallback(
|
|
1437
|
-
|
|
1438
|
-
|
|
1437
|
+
base,
|
|
1438
|
+
table
|
|
1439
1439
|
);
|
|
1440
1440
|
if (query2.and) {
|
|
1441
1441
|
pushQueryArray(q, "and", query2.and);
|
|
@@ -2429,16 +2429,18 @@ const makeRegexToFindInSql = (value) => {
|
|
|
2429
2429
|
return new RegExp(`${value}(?=(?:[^']*'[^']*')*[^']*$)`, "g");
|
|
2430
2430
|
};
|
|
2431
2431
|
const resolveSubQueryCallback = (q, cb) => {
|
|
2432
|
-
const { isSubQuery } = q.q;
|
|
2432
|
+
const { isSubQuery, relChain } = q.q;
|
|
2433
2433
|
q.q.isSubQuery = true;
|
|
2434
|
+
q.q.relChain = void 0;
|
|
2434
2435
|
const result = cb(q);
|
|
2435
2436
|
q.q.isSubQuery = isSubQuery;
|
|
2437
|
+
q.q.relChain = relChain;
|
|
2436
2438
|
return result;
|
|
2437
2439
|
};
|
|
2438
2440
|
const joinSubQuery = (q, sub) => {
|
|
2439
2441
|
if (!("relationConfig" in sub))
|
|
2440
2442
|
return sub;
|
|
2441
|
-
return sub.relationConfig.joinQuery(
|
|
2443
|
+
return sub.relationConfig.joinQuery(sub, q);
|
|
2442
2444
|
};
|
|
2443
2445
|
|
|
2444
2446
|
const pushQueryArray = (q, key, value) => {
|
|
@@ -5814,8 +5816,8 @@ const _joinLateral = (q, type, arg, cb, as) => {
|
|
|
5814
5816
|
let result = cb(query);
|
|
5815
5817
|
if (relation) {
|
|
5816
5818
|
result = relation.relationConfig.joinQuery(
|
|
5817
|
-
|
|
5818
|
-
|
|
5819
|
+
result,
|
|
5820
|
+
q
|
|
5819
5821
|
);
|
|
5820
5822
|
}
|
|
5821
5823
|
const joinKey = as || result.q.as || result.table;
|
|
@@ -5886,7 +5888,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
|
|
|
5886
5888
|
if (typeof value === "function") {
|
|
5887
5889
|
value = resolveSubQueryCallback(q, value);
|
|
5888
5890
|
if (!isExpression(value) && value.joinQuery) {
|
|
5889
|
-
value = value.joinQuery(
|
|
5891
|
+
value = value.joinQuery(value, q);
|
|
5890
5892
|
let query;
|
|
5891
5893
|
const returnType = value.q.returnType;
|
|
5892
5894
|
if (!returnType || returnType === "all") {
|
|
@@ -9323,6 +9325,63 @@ const noneMethods = {
|
|
|
9323
9325
|
catch: () => new Promise(noop)
|
|
9324
9326
|
};
|
|
9325
9327
|
|
|
9328
|
+
class ScopeMethods {
|
|
9329
|
+
/**
|
|
9330
|
+
* See {@link ScopeMethods}
|
|
9331
|
+
*
|
|
9332
|
+
* Use the `scope` method to apply a pre-defined scope.
|
|
9333
|
+
*
|
|
9334
|
+
* ```ts
|
|
9335
|
+
* // use the `active` scope that is defined in the table:
|
|
9336
|
+
* await db.some.scope('active');
|
|
9337
|
+
* ```
|
|
9338
|
+
*
|
|
9339
|
+
* @param scope - name of the scope to apply
|
|
9340
|
+
*/
|
|
9341
|
+
scope(scope) {
|
|
9342
|
+
var _a;
|
|
9343
|
+
const q = this.clone();
|
|
9344
|
+
if (!((_a = q.q.scopes) == null ? void 0 : _a[scope])) {
|
|
9345
|
+
const s = this.internal.scopes[scope];
|
|
9346
|
+
if (s.and)
|
|
9347
|
+
pushQueryArray(q, "and", s.and);
|
|
9348
|
+
if (s.or)
|
|
9349
|
+
pushQueryArray(q, "or", s.or);
|
|
9350
|
+
setQueryObjectValue(q, "scopes", scope, s);
|
|
9351
|
+
}
|
|
9352
|
+
return q;
|
|
9353
|
+
}
|
|
9354
|
+
/**
|
|
9355
|
+
* See {@link ScopeMethods}
|
|
9356
|
+
*
|
|
9357
|
+
* Remove conditions that were added by the scope from the query.
|
|
9358
|
+
*
|
|
9359
|
+
* ```ts
|
|
9360
|
+
* // SomeTable has a default scope, ignore it for this query:
|
|
9361
|
+
* await db.some.unScope('default');
|
|
9362
|
+
* ```
|
|
9363
|
+
*
|
|
9364
|
+
* @param scope - name of the scope to remove from the query
|
|
9365
|
+
*/
|
|
9366
|
+
unScope(scope) {
|
|
9367
|
+
var _a;
|
|
9368
|
+
const q = this.clone();
|
|
9369
|
+
const data = q.q;
|
|
9370
|
+
const s = (_a = q.q.scopes) == null ? void 0 : _a[scope];
|
|
9371
|
+
if (s) {
|
|
9372
|
+
const { and, or } = s;
|
|
9373
|
+
if (and) {
|
|
9374
|
+
data.and = data.and.filter((x) => !and.includes(x));
|
|
9375
|
+
}
|
|
9376
|
+
if (or) {
|
|
9377
|
+
data.or = data.or.filter((x) => !or.includes(x));
|
|
9378
|
+
}
|
|
9379
|
+
delete q.q.scopes[scope];
|
|
9380
|
+
}
|
|
9381
|
+
return q;
|
|
9382
|
+
}
|
|
9383
|
+
}
|
|
9384
|
+
|
|
9326
9385
|
class ColumnRefExpression extends Expression {
|
|
9327
9386
|
constructor(_type, name) {
|
|
9328
9387
|
super();
|
|
@@ -9959,7 +10018,8 @@ applyMixins(QueryMethods, [
|
|
|
9959
10018
|
MergeQueryMethods,
|
|
9960
10019
|
RawSqlMethods,
|
|
9961
10020
|
CopyMethods,
|
|
9962
|
-
TransformMethods
|
|
10021
|
+
TransformMethods,
|
|
10022
|
+
ScopeMethods
|
|
9963
10023
|
]);
|
|
9964
10024
|
|
|
9965
10025
|
var __defProp = Object.defineProperty;
|
|
@@ -10002,10 +10062,12 @@ class Db {
|
|
|
10002
10062
|
this.shape = shape;
|
|
10003
10063
|
this.columnTypes = columnTypes2;
|
|
10004
10064
|
var _a, _b;
|
|
10005
|
-
const tableData = getTableData();
|
|
10006
10065
|
const self = this;
|
|
10066
|
+
const scopes = options.scopes ? {} : emptyObject;
|
|
10067
|
+
const tableData = getTableData();
|
|
10007
10068
|
this.internal = __spreadProps(__spreadValues({}, tableData), {
|
|
10008
|
-
transactionStorage
|
|
10069
|
+
transactionStorage,
|
|
10070
|
+
scopes
|
|
10009
10071
|
});
|
|
10010
10072
|
this.baseQuery = this;
|
|
10011
10073
|
const logger = options.logger || console;
|
|
@@ -10108,6 +10170,21 @@ class Db {
|
|
|
10108
10170
|
super(self, message);
|
|
10109
10171
|
}
|
|
10110
10172
|
};
|
|
10173
|
+
if (options.scopes) {
|
|
10174
|
+
for (const key in options.scopes) {
|
|
10175
|
+
const q = options.scopes[key](this).q;
|
|
10176
|
+
const s = {};
|
|
10177
|
+
if (q.and)
|
|
10178
|
+
s.and = q.and;
|
|
10179
|
+
if (q.or)
|
|
10180
|
+
s.or = q.or;
|
|
10181
|
+
scopes[key] = s;
|
|
10182
|
+
}
|
|
10183
|
+
if (scopes.default) {
|
|
10184
|
+
Object.assign(this.q, scopes.default);
|
|
10185
|
+
this.q.scopes = { default: scopes.default };
|
|
10186
|
+
}
|
|
10187
|
+
}
|
|
10111
10188
|
}
|
|
10112
10189
|
[inspect.custom]() {
|
|
10113
10190
|
return `QueryObject<${this.table}>`;
|
|
@@ -10256,21 +10333,19 @@ const createDb = (_a) => {
|
|
|
10256
10333
|
commonOptions
|
|
10257
10334
|
);
|
|
10258
10335
|
qb.queryBuilder = qb;
|
|
10259
|
-
const
|
|
10260
|
-
|
|
10261
|
-
return new Db(
|
|
10262
|
-
adapter,
|
|
10263
|
-
qb,
|
|
10264
|
-
table,
|
|
10265
|
-
typeof shape === "function" ? getColumnTypes(ct, shape, nowSQL, options2 == null ? void 0 : options2.language) : shape,
|
|
10266
|
-
ct,
|
|
10267
|
-
transactionStorage,
|
|
10268
|
-
__spreadValues(__spreadValues({}, commonOptions), options2)
|
|
10269
|
-
);
|
|
10270
|
-
},
|
|
10336
|
+
const tableConstructor = (table, shape, options2) => new Db(
|
|
10337
|
+
adapter,
|
|
10271
10338
|
qb,
|
|
10272
|
-
|
|
10339
|
+
table,
|
|
10340
|
+
typeof shape === "function" ? getColumnTypes(ct, shape, nowSQL, options2 == null ? void 0 : options2.language) : shape,
|
|
10341
|
+
ct,
|
|
10342
|
+
transactionStorage,
|
|
10343
|
+
__spreadValues(__spreadValues({}, commonOptions), options2)
|
|
10273
10344
|
);
|
|
10345
|
+
const db = Object.assign(tableConstructor, qb, {
|
|
10346
|
+
adapter,
|
|
10347
|
+
close: () => adapter.close()
|
|
10348
|
+
});
|
|
10274
10349
|
for (const name of Object.getOwnPropertyNames(Db.prototype)) {
|
|
10275
10350
|
db[name] = Db.prototype[name];
|
|
10276
10351
|
}
|