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