pqb 0.19.0 → 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.mjs CHANGED
@@ -9325,6 +9325,63 @@ const noneMethods = {
9325
9325
  catch: () => new Promise(noop)
9326
9326
  };
9327
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
+
9328
9385
  class ColumnRefExpression extends Expression {
9329
9386
  constructor(_type, name) {
9330
9387
  super();
@@ -9961,7 +10018,8 @@ applyMixins(QueryMethods, [
9961
10018
  MergeQueryMethods,
9962
10019
  RawSqlMethods,
9963
10020
  CopyMethods,
9964
- TransformMethods
10021
+ TransformMethods,
10022
+ ScopeMethods
9965
10023
  ]);
9966
10024
 
9967
10025
  var __defProp = Object.defineProperty;
@@ -10004,10 +10062,12 @@ class Db {
10004
10062
  this.shape = shape;
10005
10063
  this.columnTypes = columnTypes2;
10006
10064
  var _a, _b;
10007
- const tableData = getTableData();
10008
10065
  const self = this;
10066
+ const scopes = options.scopes ? {} : emptyObject;
10067
+ const tableData = getTableData();
10009
10068
  this.internal = __spreadProps(__spreadValues({}, tableData), {
10010
- transactionStorage
10069
+ transactionStorage,
10070
+ scopes
10011
10071
  });
10012
10072
  this.baseQuery = this;
10013
10073
  const logger = options.logger || console;
@@ -10110,6 +10170,21 @@ class Db {
10110
10170
  super(self, message);
10111
10171
  }
10112
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
+ }
10113
10188
  }
10114
10189
  [inspect.custom]() {
10115
10190
  return `QueryObject<${this.table}>`;
@@ -10258,21 +10333,19 @@ const createDb = (_a) => {
10258
10333
  commonOptions
10259
10334
  );
10260
10335
  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
- },
10336
+ const tableConstructor = (table, shape, options2) => new Db(
10337
+ adapter,
10273
10338
  qb,
10274
- { adapter, close: () => adapter.close() }
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)
10275
10344
  );
10345
+ const db = Object.assign(tableConstructor, qb, {
10346
+ adapter,
10347
+ close: () => adapter.close()
10348
+ });
10276
10349
  for (const name of Object.getOwnPropertyNames(Db.prototype)) {
10277
10350
  db[name] = Db.prototype[name];
10278
10351
  }