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.js CHANGED
@@ -9327,6 +9327,63 @@ const noneMethods = {
9327
9327
  catch: () => new Promise(orchidCore.noop)
9328
9328
  };
9329
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
+
9330
9387
  class ColumnRefExpression extends orchidCore.Expression {
9331
9388
  constructor(_type, name) {
9332
9389
  super();
@@ -9963,7 +10020,8 @@ orchidCore.applyMixins(QueryMethods, [
9963
10020
  MergeQueryMethods,
9964
10021
  RawSqlMethods,
9965
10022
  CopyMethods,
9966
- TransformMethods
10023
+ TransformMethods,
10024
+ ScopeMethods
9967
10025
  ]);
9968
10026
 
9969
10027
  var __defProp = Object.defineProperty;
@@ -10006,10 +10064,12 @@ class Db {
10006
10064
  this.shape = shape;
10007
10065
  this.columnTypes = columnTypes2;
10008
10066
  var _a, _b;
10009
- const tableData = getTableData();
10010
10067
  const self = this;
10068
+ const scopes = options.scopes ? {} : orchidCore.emptyObject;
10069
+ const tableData = getTableData();
10011
10070
  this.internal = __spreadProps(__spreadValues({}, tableData), {
10012
- transactionStorage
10071
+ transactionStorage,
10072
+ scopes
10013
10073
  });
10014
10074
  this.baseQuery = this;
10015
10075
  const logger = options.logger || console;
@@ -10112,6 +10172,21 @@ class Db {
10112
10172
  super(self, message);
10113
10173
  }
10114
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
+ }
10115
10190
  }
10116
10191
  [node_util.inspect.custom]() {
10117
10192
  return `QueryObject<${this.table}>`;
@@ -10260,21 +10335,19 @@ const createDb = (_a) => {
10260
10335
  commonOptions
10261
10336
  );
10262
10337
  qb.queryBuilder = qb;
10263
- const db = Object.assign(
10264
- (table, shape, options2) => {
10265
- return new Db(
10266
- adapter,
10267
- qb,
10268
- table,
10269
- typeof shape === "function" ? getColumnTypes(ct, shape, nowSQL, options2 == null ? void 0 : options2.language) : shape,
10270
- ct,
10271
- transactionStorage,
10272
- __spreadValues(__spreadValues({}, commonOptions), options2)
10273
- );
10274
- },
10338
+ const tableConstructor = (table, shape, options2) => new Db(
10339
+ adapter,
10275
10340
  qb,
10276
- { 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)
10277
10346
  );
10347
+ const db = Object.assign(tableConstructor, qb, {
10348
+ adapter,
10349
+ close: () => adapter.close()
10350
+ });
10278
10351
  for (const name of Object.getOwnPropertyNames(Db.prototype)) {
10279
10352
  db[name] = Db.prototype[name];
10280
10353
  }