@guren/orm 2.2.1 → 2.2.2
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 +52 -2
- package/dist/index.js +76 -16
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,13 @@ declare function applyAccessors<T extends PlainObject>(record: T, accessors?: Ac
|
|
|
21
21
|
declare function applyMutators(data: PlainObject, mutators?: MutatorDefinitions): PlainObject;
|
|
22
22
|
|
|
23
23
|
type FieldKey<TRecord extends PlainObject> = keyof TRecord & string;
|
|
24
|
+
/**
|
|
25
|
+
* Key for the prepared-payload update terminal.
|
|
26
|
+
*
|
|
27
|
+
* Exported for `Model` to call across the module boundary, but deliberately
|
|
28
|
+
* absent from the package entry point, so it is not part of the public API.
|
|
29
|
+
*/
|
|
30
|
+
declare const PREPARED_UPDATE: unique symbol;
|
|
24
31
|
/** Comparison operators supported by the QueryBuilder. */
|
|
25
32
|
type WhereOperator = '=' | '!=' | '>' | '<' | '>=' | '<=' | 'like' | 'in' | 'not in' | 'is null' | 'is not null';
|
|
26
33
|
/** A single field-level condition. */
|
|
@@ -233,6 +240,20 @@ declare class QueryBuilder<TRecord extends PlainObject = PlainObject, TResult ex
|
|
|
233
240
|
*/
|
|
234
241
|
forceUpdate(data: PlainObject): Promise<TRecord>;
|
|
235
242
|
private runBulkUpdate;
|
|
243
|
+
/**
|
|
244
|
+
* Bulk update using a payload the caller has already run through
|
|
245
|
+
* mass-assignment filtering and persistence preparation. This exists so the
|
|
246
|
+
* static `Model.update()` write path can borrow the builder's
|
|
247
|
+
* global-scope-carrying `conditions` without re-preparing the payload —
|
|
248
|
+
* running mutators twice would, for example, double-hash a hashed column.
|
|
249
|
+
*
|
|
250
|
+
* Keyed by a symbol that this package never re-exports, because a named
|
|
251
|
+
* public method here would be a supported way to write arbitrary columns:
|
|
252
|
+
* it skips both `filterFillable` and `prepareBulkPersistencePayload`, so
|
|
253
|
+
* `User.where(…)[name](requestBody)` would bypass mass-assignment protection
|
|
254
|
+
* entirely. `@internal` is only a doc tag — declarations still emit it.
|
|
255
|
+
*/
|
|
256
|
+
[PREPARED_UPDATE](payload: PlainObject): Promise<TRecord>;
|
|
236
257
|
/**
|
|
237
258
|
* Bulk delete records matching the current conditions.
|
|
238
259
|
* @returns Number of deleted records (adapter-dependent)
|
|
@@ -300,6 +321,15 @@ declare class GlobalScopeRegistry {
|
|
|
300
321
|
/** Apply all scopes (or all except excluded) to a query builder. */
|
|
301
322
|
apply<T extends PlainObject>(builder: QueryBuilder<T>, except?: string[]): QueryBuilder<T>;
|
|
302
323
|
clear(): void;
|
|
324
|
+
/**
|
|
325
|
+
* Copy this registry's entries into a new one.
|
|
326
|
+
*
|
|
327
|
+
* A subclass that registers its own scope needs a registry of its own, but it
|
|
328
|
+
* must keep the ones it inherited: starting from empty is how a model that
|
|
329
|
+
* mixes in `SoftDeletes` and then adds a `tenant` scope silently loses the
|
|
330
|
+
* `softDelete` filter.
|
|
331
|
+
*/
|
|
332
|
+
clone(): GlobalScopeRegistry;
|
|
303
333
|
get size(): number;
|
|
304
334
|
}
|
|
305
335
|
|
|
@@ -718,6 +748,22 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
718
748
|
static observe(ObserverClass: ModelObserverConstructor): void;
|
|
719
749
|
/** Clear all registered observers. */
|
|
720
750
|
static clearObservers(): void;
|
|
751
|
+
/**
|
|
752
|
+
* The registry this class may mutate, creating it on first write.
|
|
753
|
+
*
|
|
754
|
+
* The new registry is seeded from the inherited one: the read paths
|
|
755
|
+
* (`newQuery()`, `hasScopes()`) resolve `globalScopeRegistry` through the
|
|
756
|
+
* prototype chain, so a subclass that started from an empty registry would
|
|
757
|
+
* shadow — and silently drop — every scope it inherited. That is exactly the
|
|
758
|
+
* `class Post extends SoftDeletes(Base)` then `Post.addGlobalScope('tenant')`
|
|
759
|
+
* case, which would otherwise lose the `softDelete` filter.
|
|
760
|
+
*
|
|
761
|
+
* The copy is a snapshot: scopes added to the parent *after* a subclass first
|
|
762
|
+
* registers or removes one do not reach that subclass. Nothing in the
|
|
763
|
+
* framework relies on later propagation — the `SoftDeletes` mixin registers
|
|
764
|
+
* `softDelete` synchronously while building the class, before any user code
|
|
765
|
+
* can touch it.
|
|
766
|
+
*/
|
|
721
767
|
protected static getGlobalScopes(): GlobalScopeRegistry;
|
|
722
768
|
/**
|
|
723
769
|
* Register a named global scope.
|
|
@@ -731,6 +777,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
731
777
|
/**
|
|
732
778
|
* Start a query excluding specific global scope(s).
|
|
733
779
|
*
|
|
780
|
+
* Only *named* scopes can be excluded: `defaultScope` is re-applied here
|
|
781
|
+
* whatever this is asked to drop. A mixin whose filter users must be able to
|
|
782
|
+
* opt out of therefore has to register a named scope and nothing else —
|
|
783
|
+
* registering as both makes the filter unremovable, which is how `SoftDeletes`
|
|
784
|
+
* once made `withoutGlobalScope('softDelete')` a no-op.
|
|
785
|
+
*
|
|
734
786
|
* @example
|
|
735
787
|
* const all = await User.withoutGlobalScope('active').get()
|
|
736
788
|
*/
|
|
@@ -1518,8 +1570,6 @@ declare class MassAssignmentException extends Error {
|
|
|
1518
1570
|
interface SoftDeletesStatic {
|
|
1519
1571
|
/** Column name used for tracking soft deletion timestamp. */
|
|
1520
1572
|
deletedAtColumn: string;
|
|
1521
|
-
/** Default scope that excludes soft-deleted records. */
|
|
1522
|
-
defaultScope: (q: QueryBuilder<any>) => QueryBuilder<any>;
|
|
1523
1573
|
/** Start a query that includes soft-deleted records. */
|
|
1524
1574
|
withTrashed(): QueryBuilder<PlainObject>;
|
|
1525
1575
|
/** Start a query that returns only soft-deleted records. */
|
package/dist/index.js
CHANGED
|
@@ -437,7 +437,7 @@ function applyMutators(data, mutators) {
|
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
// src/GlobalScopeRegistry.ts
|
|
440
|
-
var GlobalScopeRegistry = class {
|
|
440
|
+
var GlobalScopeRegistry = class _GlobalScopeRegistry {
|
|
441
441
|
scopes = /* @__PURE__ */ new Map();
|
|
442
442
|
add(name, fn) {
|
|
443
443
|
this.scopes.set(name, fn);
|
|
@@ -462,6 +462,21 @@ var GlobalScopeRegistry = class {
|
|
|
462
462
|
clear() {
|
|
463
463
|
this.scopes.clear();
|
|
464
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Copy this registry's entries into a new one.
|
|
467
|
+
*
|
|
468
|
+
* A subclass that registers its own scope needs a registry of its own, but it
|
|
469
|
+
* must keep the ones it inherited: starting from empty is how a model that
|
|
470
|
+
* mixes in `SoftDeletes` and then adds a `tenant` scope silently loses the
|
|
471
|
+
* `softDelete` filter.
|
|
472
|
+
*/
|
|
473
|
+
clone() {
|
|
474
|
+
const copy = new _GlobalScopeRegistry();
|
|
475
|
+
for (const [name, fn] of this.scopes) {
|
|
476
|
+
copy.add(name, fn);
|
|
477
|
+
}
|
|
478
|
+
return copy;
|
|
479
|
+
}
|
|
465
480
|
get size() {
|
|
466
481
|
return this.scopes.size;
|
|
467
482
|
}
|
|
@@ -503,6 +518,7 @@ var ModelNotFoundException = class extends Error {
|
|
|
503
518
|
};
|
|
504
519
|
|
|
505
520
|
// src/QueryBuilder.ts
|
|
521
|
+
var PREPARED_UPDATE = /* @__PURE__ */ Symbol("guren.orm.preparedUpdate");
|
|
506
522
|
var QueryBuilder = class {
|
|
507
523
|
conditions = [];
|
|
508
524
|
options = { orderBy: [] };
|
|
@@ -786,12 +802,28 @@ var QueryBuilder = class {
|
|
|
786
802
|
return this.runBulkUpdate(data, false);
|
|
787
803
|
}
|
|
788
804
|
async runBulkUpdate(data, applyFillable) {
|
|
789
|
-
if (!this.adapter.update) {
|
|
790
|
-
throw new Error("Configured adapter does not support update operations.");
|
|
791
|
-
}
|
|
792
805
|
const model = this.modelClass;
|
|
793
806
|
const filtered = applyFillable ? model.filterFillable(data) : { ...data };
|
|
794
807
|
const payload = await model.prepareBulkPersistencePayload(filtered);
|
|
808
|
+
return this[PREPARED_UPDATE](payload);
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* Bulk update using a payload the caller has already run through
|
|
812
|
+
* mass-assignment filtering and persistence preparation. This exists so the
|
|
813
|
+
* static `Model.update()` write path can borrow the builder's
|
|
814
|
+
* global-scope-carrying `conditions` without re-preparing the payload —
|
|
815
|
+
* running mutators twice would, for example, double-hash a hashed column.
|
|
816
|
+
*
|
|
817
|
+
* Keyed by a symbol that this package never re-exports, because a named
|
|
818
|
+
* public method here would be a supported way to write arbitrary columns:
|
|
819
|
+
* it skips both `filterFillable` and `prepareBulkPersistencePayload`, so
|
|
820
|
+
* `User.where(…)[name](requestBody)` would bypass mass-assignment protection
|
|
821
|
+
* entirely. `@internal` is only a doc tag — declarations still emit it.
|
|
822
|
+
*/
|
|
823
|
+
async [PREPARED_UPDATE](payload) {
|
|
824
|
+
if (!this.adapter.update) {
|
|
825
|
+
throw new Error("Configured adapter does not support update operations.");
|
|
826
|
+
}
|
|
795
827
|
const advancedAdapter = this.adapter;
|
|
796
828
|
if (typeof advancedAdapter.updateAdvanced === "function") {
|
|
797
829
|
return advancedAdapter.updateAdvanced(this.table, this.conditions, payload, { trx: this.options.trx });
|
|
@@ -1209,9 +1241,26 @@ var Model = class _Model {
|
|
|
1209
1241
|
// Global Scopes
|
|
1210
1242
|
// ---------------------------------------------------------------------------
|
|
1211
1243
|
// ---------------------------------------------------------------------------
|
|
1244
|
+
/**
|
|
1245
|
+
* The registry this class may mutate, creating it on first write.
|
|
1246
|
+
*
|
|
1247
|
+
* The new registry is seeded from the inherited one: the read paths
|
|
1248
|
+
* (`newQuery()`, `hasScopes()`) resolve `globalScopeRegistry` through the
|
|
1249
|
+
* prototype chain, so a subclass that started from an empty registry would
|
|
1250
|
+
* shadow — and silently drop — every scope it inherited. That is exactly the
|
|
1251
|
+
* `class Post extends SoftDeletes(Base)` then `Post.addGlobalScope('tenant')`
|
|
1252
|
+
* case, which would otherwise lose the `softDelete` filter.
|
|
1253
|
+
*
|
|
1254
|
+
* The copy is a snapshot: scopes added to the parent *after* a subclass first
|
|
1255
|
+
* registers or removes one do not reach that subclass. Nothing in the
|
|
1256
|
+
* framework relies on later propagation — the `SoftDeletes` mixin registers
|
|
1257
|
+
* `softDelete` synchronously while building the class, before any user code
|
|
1258
|
+
* can touch it.
|
|
1259
|
+
*/
|
|
1212
1260
|
static getGlobalScopes() {
|
|
1213
1261
|
if (!Object.prototype.hasOwnProperty.call(this, "globalScopeRegistry") || !this.globalScopeRegistry) {
|
|
1214
|
-
this.globalScopeRegistry
|
|
1262
|
+
const inherited = this.globalScopeRegistry;
|
|
1263
|
+
this.globalScopeRegistry = inherited ? inherited.clone() : new GlobalScopeRegistry();
|
|
1215
1264
|
}
|
|
1216
1265
|
return this.globalScopeRegistry;
|
|
1217
1266
|
}
|
|
@@ -1231,6 +1280,12 @@ var Model = class _Model {
|
|
|
1231
1280
|
/**
|
|
1232
1281
|
* Start a query excluding specific global scope(s).
|
|
1233
1282
|
*
|
|
1283
|
+
* Only *named* scopes can be excluded: `defaultScope` is re-applied here
|
|
1284
|
+
* whatever this is asked to drop. A mixin whose filter users must be able to
|
|
1285
|
+
* opt out of therefore has to register a named scope and nothing else —
|
|
1286
|
+
* registering as both makes the filter unremovable, which is how `SoftDeletes`
|
|
1287
|
+
* once made `withoutGlobalScope('softDelete')` a no-op.
|
|
1288
|
+
*
|
|
1234
1289
|
* @example
|
|
1235
1290
|
* const all = await User.withoutGlobalScope('active').get()
|
|
1236
1291
|
*/
|
|
@@ -2092,7 +2147,7 @@ var Model = class _Model {
|
|
|
2092
2147
|
throw new Error(`${this.name}.update() aborted by observer 'saving'.`);
|
|
2093
2148
|
}
|
|
2094
2149
|
}
|
|
2095
|
-
const result = await adapter.update(table, where, payload, writeOptions);
|
|
2150
|
+
const result = this.hasScopes() ? await this.newQuery(writeOptions).where(where)[PREPARED_UPDATE](payload) : await adapter.update(table, where, payload, writeOptions);
|
|
2096
2151
|
if (hooks) {
|
|
2097
2152
|
const resultData = result;
|
|
2098
2153
|
await executeHook(hooks, "updated", resultData);
|
|
@@ -2137,7 +2192,7 @@ var Model = class _Model {
|
|
|
2137
2192
|
throw new Error(`${this.name}.delete() aborted by observer 'deleting'.`);
|
|
2138
2193
|
}
|
|
2139
2194
|
}
|
|
2140
|
-
const result = await adapter.delete(table, where, writeOptions);
|
|
2195
|
+
const result = this.hasScopes() ? await this.newQuery(writeOptions).where(where).delete() : await adapter.delete(table, where, writeOptions);
|
|
2141
2196
|
if (hooks) {
|
|
2142
2197
|
await executeHook(hooks, "deleted", whereData);
|
|
2143
2198
|
}
|
|
@@ -2602,45 +2657,50 @@ function resolveQueryableDatabase(adapter) {
|
|
|
2602
2657
|
}
|
|
2603
2658
|
|
|
2604
2659
|
// src/SoftDeletes.ts
|
|
2660
|
+
function scopedQuery(model, where) {
|
|
2661
|
+
return model.newQuery().where(where);
|
|
2662
|
+
}
|
|
2663
|
+
function withoutSoftDeleteScope(model) {
|
|
2664
|
+
return model.withoutGlobalScope("softDelete");
|
|
2665
|
+
}
|
|
2666
|
+
function trashedScopedQuery(model, where) {
|
|
2667
|
+
return withoutSoftDeleteScope(model).where(where);
|
|
2668
|
+
}
|
|
2605
2669
|
function SoftDeletes(Base) {
|
|
2606
2670
|
const SoftDeleteModel = class extends Base {
|
|
2607
2671
|
};
|
|
2608
2672
|
SoftDeleteModel.deletedAtColumn = "deletedAt";
|
|
2609
2673
|
const scopeFn = (q) => q.whereNull("deletedAt");
|
|
2610
|
-
SoftDeleteModel.defaultScope = scopeFn;
|
|
2611
2674
|
SoftDeleteModel.addGlobalScope("softDelete", scopeFn);
|
|
2612
2675
|
SoftDeleteModel.delete = async function(where) {
|
|
2613
2676
|
const adapter = this.getAdapter();
|
|
2614
2677
|
if (!adapter.update) {
|
|
2615
2678
|
throw new Error("Configured adapter does not support update operations (needed for soft delete).");
|
|
2616
2679
|
}
|
|
2617
|
-
const table = this.resolveTable();
|
|
2618
2680
|
const column = this.deletedAtColumn;
|
|
2619
|
-
return
|
|
2681
|
+
return scopedQuery(this, where)[PREPARED_UPDATE]({ [column]: /* @__PURE__ */ new Date() });
|
|
2620
2682
|
};
|
|
2621
2683
|
SoftDeleteModel.withTrashed = function() {
|
|
2622
|
-
return this
|
|
2684
|
+
return withoutSoftDeleteScope(this);
|
|
2623
2685
|
};
|
|
2624
2686
|
SoftDeleteModel.onlyTrashed = function() {
|
|
2625
2687
|
const column = this.deletedAtColumn;
|
|
2626
|
-
return this
|
|
2688
|
+
return withoutSoftDeleteScope(this).whereNotNull(column);
|
|
2627
2689
|
};
|
|
2628
2690
|
SoftDeleteModel.restore = async function(where) {
|
|
2629
2691
|
const adapter = this.getAdapter();
|
|
2630
2692
|
if (!adapter.update) {
|
|
2631
2693
|
throw new Error("Configured adapter does not support update operations (needed for restore).");
|
|
2632
2694
|
}
|
|
2633
|
-
const table = this.resolveTable();
|
|
2634
2695
|
const column = this.deletedAtColumn;
|
|
2635
|
-
return
|
|
2696
|
+
return trashedScopedQuery(this, where)[PREPARED_UPDATE]({ [column]: null });
|
|
2636
2697
|
};
|
|
2637
2698
|
SoftDeleteModel.forceDelete = async function(where) {
|
|
2638
2699
|
const adapter = this.getAdapter();
|
|
2639
2700
|
if (!adapter.delete) {
|
|
2640
2701
|
throw new Error("Configured adapter does not support delete operations.");
|
|
2641
2702
|
}
|
|
2642
|
-
|
|
2643
|
-
return adapter.delete(table, where);
|
|
2703
|
+
return trashedScopedQuery(this, where).delete();
|
|
2644
2704
|
};
|
|
2645
2705
|
return SoftDeleteModel;
|
|
2646
2706
|
}
|