masterrecord 1.5.12 → 1.5.13
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/CHANGES.md +12 -0
- package/Entity/entityTrackerModel.js +13 -6
- package/QueryLanguage/queryMethods.js +35 -11
- package/context.js +45 -8
- package/package.json +1 -1
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.5.13 — EF-Core-style change tracking: O(changes) saves (dirty index) + asNoTracking()
|
|
4
|
+
|
|
5
|
+
Fixing the write loss in 1.5.12 exposed two costs of correct retention in a long-lived context — unbounded memory and O(total-tracked) saves. Both are addressed the way EF Core does.
|
|
6
|
+
|
|
7
|
+
**1. Dirty index (EF's `StateManager`) — a flush is O(changes), not O(total tracked).** The context keeps a `__dirtyEntities` set: the subset of tracked entities that are insert/modified/delete. Entities enter it the instant a setter / `add()` / `remove()` / `delete()` / `attach()` makes them dirty (all funnel through a new `__markDirty()`), and leave it when flushed or detached. `saveChanges()` now reads **only** this set, so an empty save is O(1) and a flush is O(changes) even with tens of thousands of clean rows tracked. Belt-and-suspenders for completeness: `__track()` also enqueues any entity that is already dirty when tracked, so no dirty entity can ever be tracked yet missed by the change set.
|
|
8
|
+
|
|
9
|
+
**2. `asNoTracking()` (EF's `AsNoTracking`) — read-only queries retain nothing.** `db.Model.asNoTracking().toList()` returns entities without entering them into the change tracker, so a read-heavy endpoint that lists a whole table no longer grows the tracked set. Mutating a no-tracking entity does not enqueue a write (it is detached), matching EF. This is the fix for the memory growth a process-lifetime singleton context accumulated from read-only `.toList()` calls.
|
|
10
|
+
|
|
11
|
+
**Guidance (also EF's):** a context is a unit of work — prefer a **short-lived, request-scoped** context (EF disposes the DbContext per request). For read-only work on a shared/long-lived context, use `asNoTracking()`. `clearChangeTracker()` remains, but it drops pending changes, so it is not safe as a per-request mitigation on a shared context — scope the context or use `asNoTracking()` instead.
|
|
12
|
+
|
|
13
|
+
New tests: `test/no-tracking-and-dirty-index.test.js` — asNoTracking retains nothing and its mutations don't persist; a normal query tracks and its edits persist; a save over 2000 tracked rows with 3 dirty puts exactly 3 in the change set and flushes only those. Full suite green (0 fail, 307 pass, 20 gated skipped); 0 lint errors.
|
|
14
|
+
|
|
3
15
|
## v1.5.12 — collision-free tracking identity + single source of truth (THE write-loss root cause)
|
|
4
16
|
|
|
5
17
|
**Bug (silent write loss with monotonic decay — the one behind 1.5.8→1.5.11):** queried entities were assigned a **random `__ID` in `[1, 100000]`** (`entityTrackerModel.buildObject`). Added entities used a collision-free sequential id, but read-only `.toList()` results used random ones. In a long-lived, read-heavy singleton context the tracked set grows toward tens of thousands, and by the birthday paradox a new entity's random `__ID` increasingly collides with one already in the identity map. `__track()` dedups by id (`if (!map.has(id))`), so the colliding entity was **never added to the tracked set** — `changeSet` never saw it, its INSERT/UPDATE was never issued, and `saveChanges()` still returned `true`. This is exactly why the library **passed in isolation** (small set, no collisions) but **decayed over a server's lifetime** (22/22 fresh → 16/22 → 14/22 as the set filled), with no exception, no rollback, and nothing between the SELECT and the missing UPDATE in the SQL log. Proven: 3000 queried rows → 52 dropped by collision, and a dropped entity's write is lost.
|
|
@@ -150,11 +150,12 @@ class EntityTrackerModel {
|
|
|
150
150
|
throw new Error('Cannot delete: entity is not attached to a context');
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
-
// Mark entity for deletion
|
|
153
|
+
// Mark entity for deletion — dirty, so register it in the dirty index
|
|
154
|
+
// (a delete that isn't in the change set would never be issued).
|
|
154
155
|
this.__state = 'delete';
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (!this.__context.__trackedEntitiesMap.has(this.__ID)) {
|
|
156
|
+
if (typeof this.__context.__markDirty === 'function') {
|
|
157
|
+
this.__context.__markDirty(this);
|
|
158
|
+
} else if (!this.__context.__trackedEntitiesMap.has(this.__ID)) {
|
|
158
159
|
this.__context.__track(this);
|
|
159
160
|
}
|
|
160
161
|
|
|
@@ -392,7 +393,9 @@ class EntityTrackerModel {
|
|
|
392
393
|
// Ensure this entity is tracked on any modification. THIS is the
|
|
393
394
|
// line that makes a just-inserted, attached entity's later edits
|
|
394
395
|
// persist instead of being silently dropped.
|
|
395
|
-
if (this.__context && typeof this.__context.
|
|
396
|
+
if (this.__context && typeof this.__context.__markDirty === 'function') {
|
|
397
|
+
this.__context.__markDirty(this);
|
|
398
|
+
} else if (this.__context && typeof this.__context.__track === 'function') {
|
|
396
399
|
this.__context.__track(this);
|
|
397
400
|
}
|
|
398
401
|
const fieldDefForSet = currentEntity[modelField];
|
|
@@ -531,7 +534,11 @@ class EntityTrackerModel {
|
|
|
531
534
|
modelClass.__state = "modified";
|
|
532
535
|
modelClass.__version = (modelClass.__version || 0) + 1;
|
|
533
536
|
modelClass.__dirtyFields.push(entityField);
|
|
534
|
-
|
|
537
|
+
if (typeof modelClass.__context.__markDirty === 'function') {
|
|
538
|
+
modelClass.__context.__markDirty(modelClass);
|
|
539
|
+
} else {
|
|
540
|
+
modelClass.__context.__track(modelClass);
|
|
541
|
+
}
|
|
535
542
|
}
|
|
536
543
|
this["__proto__"]["_" + entityField] = value;
|
|
537
544
|
},
|
|
@@ -28,6 +28,20 @@ class queryMethods{
|
|
|
28
28
|
this.__context = context;
|
|
29
29
|
this.__queryObject = new queryScript();
|
|
30
30
|
this.__useCache = false; // Disable caching by default (opt-in with .cache())
|
|
31
|
+
this.__noTracking = false; // opt-in with .asNoTracking() for read-only reads
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Read-only query: do NOT enter results into the change tracker (like EF
|
|
36
|
+
* Core's AsNoTracking). Nothing is retained, so a read-heavy endpoint that
|
|
37
|
+
* `.toList()`s a large table no longer grows the tracked set. Mutations on
|
|
38
|
+
* the returned entities are not persisted (they are detached), matching EF.
|
|
39
|
+
*
|
|
40
|
+
* @example db.Users.asNoTracking().toList();
|
|
41
|
+
*/
|
|
42
|
+
asNoTracking(){
|
|
43
|
+
this.__noTracking = true;
|
|
44
|
+
return this;
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
// build a single entity
|
|
@@ -37,7 +51,13 @@ class queryMethods{
|
|
|
37
51
|
const ent = new entityTrackerModel();
|
|
38
52
|
const mod = ent.build(dataModel, $that.__entity, $that.__context);
|
|
39
53
|
mod.__state = "track";
|
|
40
|
-
$that.
|
|
54
|
+
if ($that.__noTracking) {
|
|
55
|
+
// AsNoTracking: flag it so a later mutation won't enqueue a write
|
|
56
|
+
// (__markDirty ignores no-tracking entities) and never track it.
|
|
57
|
+
mod.__noTracking = true;
|
|
58
|
+
} else {
|
|
59
|
+
$that.__context.__track(mod);
|
|
60
|
+
}
|
|
41
61
|
return mod;
|
|
42
62
|
}else{
|
|
43
63
|
return null;
|
|
@@ -714,7 +734,9 @@ class queryMethods{
|
|
|
714
734
|
if (this.__state === 'track') {
|
|
715
735
|
this.__state = 'modified';
|
|
716
736
|
}
|
|
717
|
-
if (this.__context && typeof this.__context.
|
|
737
|
+
if (this.__context && typeof this.__context.__markDirty === 'function') {
|
|
738
|
+
this.__context.__markDirty(this);
|
|
739
|
+
} else if (this.__context && typeof this.__context.__track === 'function') {
|
|
718
740
|
this.__context.__track(this);
|
|
719
741
|
}
|
|
720
742
|
},
|
|
@@ -762,7 +784,9 @@ class queryMethods{
|
|
|
762
784
|
if (this.__state === 'track') {
|
|
763
785
|
this.__state = 'modified';
|
|
764
786
|
}
|
|
765
|
-
if (this.__context && typeof this.__context.
|
|
787
|
+
if (this.__context && typeof this.__context.__markDirty === 'function') {
|
|
788
|
+
this.__context.__markDirty(this);
|
|
789
|
+
} else if (this.__context && typeof this.__context.__track === 'function') {
|
|
766
790
|
this.__context.__track(this);
|
|
767
791
|
}
|
|
768
792
|
},
|
|
@@ -882,11 +906,11 @@ class queryMethods{
|
|
|
882
906
|
throw new Error('Cannot delete: entity is not attached to a context');
|
|
883
907
|
}
|
|
884
908
|
|
|
885
|
-
// Mark entity for deletion
|
|
909
|
+
// Mark entity for deletion — dirty, so register it in the dirty index.
|
|
886
910
|
this.__state = 'delete';
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
if (!this.__context.__trackedEntitiesMap.has(this.__ID)) {
|
|
911
|
+
if (typeof this.__context.__markDirty === 'function') {
|
|
912
|
+
this.__context.__markDirty(this);
|
|
913
|
+
} else if (!this.__context.__trackedEntitiesMap.has(this.__ID)) {
|
|
890
914
|
this.__context.__track(this);
|
|
891
915
|
}
|
|
892
916
|
|
|
@@ -1032,14 +1056,14 @@ class queryMethods{
|
|
|
1032
1056
|
entityValue[f] = undefined;
|
|
1033
1057
|
}
|
|
1034
1058
|
}
|
|
1035
|
-
this.__context.
|
|
1059
|
+
this.__context.__markDirty(entityValue); // insert is dirty
|
|
1036
1060
|
}
|
|
1037
|
-
|
|
1061
|
+
|
|
1038
1062
|
remove(entityValue){
|
|
1039
1063
|
entityValue.__state = "delete";
|
|
1040
1064
|
entityValue.__entity = this.__entity;
|
|
1041
1065
|
entityValue.__context = this.__context;
|
|
1042
|
-
this.__context.
|
|
1066
|
+
this.__context.__markDirty(entityValue); // delete is dirty
|
|
1043
1067
|
}
|
|
1044
1068
|
|
|
1045
1069
|
removeRange(entityValues){
|
|
@@ -1048,7 +1072,7 @@ class queryMethods{
|
|
|
1048
1072
|
entityValue.__state = "delete";
|
|
1049
1073
|
entityValue.__entity = this.__entity;
|
|
1050
1074
|
entityValue.__context = this.__context;
|
|
1051
|
-
this.__context.
|
|
1075
|
+
this.__context.__markDirty(entityValue); // delete is dirty
|
|
1052
1076
|
}
|
|
1053
1077
|
}
|
|
1054
1078
|
|
package/context.js
CHANGED
|
@@ -222,6 +222,12 @@ class context {
|
|
|
222
222
|
// writes whenever the two disagreed (a stale/colliding id in one but not the
|
|
223
223
|
// other). All mutations go through __track / __untrack / __clearTracked.
|
|
224
224
|
__trackedEntitiesMap = new Map(); // O(1) identity lookup; the authority
|
|
225
|
+
// Dirty index (like EF Core's StateManager): the subset of tracked entities
|
|
226
|
+
// that are insert/modified/delete. Entities enter it the moment a setter /
|
|
227
|
+
// add() / remove() makes them dirty, and leave it when flushed or detached.
|
|
228
|
+
// saveChanges() reads ONLY this set, so a flush is O(changes) — not O(total
|
|
229
|
+
// tracked) — and an empty save is O(1), even with tens of thousands tracked.
|
|
230
|
+
__dirtyEntities = new Set();
|
|
225
231
|
__relationshipModels = [];
|
|
226
232
|
__contextSeedData = {}; // Store seed data by table name
|
|
227
233
|
__contextSeedConfig = { // Seed data configuration
|
|
@@ -2189,16 +2195,18 @@ class context {
|
|
|
2189
2195
|
// silently lose the UPDATE (returning true). Committing only the
|
|
2190
2196
|
// change set makes an empty save a true no-op that never touches
|
|
2191
2197
|
// another unit of work's entities.
|
|
2192
|
-
//
|
|
2193
|
-
// tracked
|
|
2194
|
-
// clean rows
|
|
2198
|
+
// Read ONLY the dirty index — the change set is O(changes), never
|
|
2199
|
+
// O(total tracked). A read-heavy context can track tens of thousands of
|
|
2200
|
+
// clean rows; none of them are scanned here. (Self-heals if a clean
|
|
2201
|
+
// entity ever lingers in the index.)
|
|
2195
2202
|
const changeSet = [];
|
|
2196
|
-
for (const e of this.
|
|
2203
|
+
for (const e of this.__dirtyEntities) {
|
|
2197
2204
|
if (e && e.__state && e.__state !== 'track') changeSet.push(e);
|
|
2205
|
+
else this.__dirtyEntities.delete(e);
|
|
2198
2206
|
}
|
|
2199
2207
|
|
|
2200
2208
|
if (changeSet.length === 0) {
|
|
2201
|
-
// Nothing dirty — do NOT touch the
|
|
2209
|
+
// Nothing dirty — O(1) no-op; do NOT touch the tracked set.
|
|
2202
2210
|
return true;
|
|
2203
2211
|
}
|
|
2204
2212
|
|
|
@@ -2299,7 +2307,9 @@ class context {
|
|
|
2299
2307
|
// that were not re-mutated in flight, so no state test is applied here (a
|
|
2300
2308
|
// state test is what stranded deletes, whose state stays 'delete').
|
|
2301
2309
|
for (const e of batch) {
|
|
2302
|
-
if (e
|
|
2310
|
+
if (!e) continue;
|
|
2311
|
+
if (e.__ID != null) this.__trackedEntitiesMap.delete(e.__ID);
|
|
2312
|
+
this.__dirtyEntities.delete(e); // keep the dirty index in lockstep
|
|
2303
2313
|
}
|
|
2304
2314
|
}
|
|
2305
2315
|
|
|
@@ -2793,8 +2803,9 @@ class context {
|
|
|
2793
2803
|
// Ensure context reference
|
|
2794
2804
|
entity.__context = this;
|
|
2795
2805
|
|
|
2796
|
-
//
|
|
2797
|
-
|
|
2806
|
+
// Attach() marks the entity modified, so it is dirty — register it in the
|
|
2807
|
+
// dirty index so saveChanges() actually flushes it.
|
|
2808
|
+
this.__markDirty(entity);
|
|
2798
2809
|
|
|
2799
2810
|
return entity;
|
|
2800
2811
|
}
|
|
@@ -2897,6 +2908,31 @@ class context {
|
|
|
2897
2908
|
// a no-op and distinct objects never collide. Map is the sole registry;
|
|
2898
2909
|
// the list view derives from it, so they cannot desynchronise.
|
|
2899
2910
|
this.__trackedEntitiesMap.set(model.__ID, model);
|
|
2911
|
+
// Robustness: if the entity being tracked is ALREADY dirty, enqueue it in
|
|
2912
|
+
// the dirty index too. This guarantees the index is complete no matter
|
|
2913
|
+
// how an entity gets tracked (setter, add/remove, or a direct __track
|
|
2914
|
+
// after a state change) — a dirty entity can never be tracked yet missed
|
|
2915
|
+
// by the change set.
|
|
2916
|
+
if (model.__state && model.__state !== 'track' && !model.__noTracking) {
|
|
2917
|
+
this.__dirtyEntities.add(model);
|
|
2918
|
+
}
|
|
2919
|
+
return model;
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
/**
|
|
2923
|
+
* Mark an entity dirty (insert/modified/delete): track it in the identity map
|
|
2924
|
+
* AND add it to the dirty index that saveChanges() reads. EVERY dirty-state
|
|
2925
|
+
* transition (setters, add(), remove(), delete(), attach()) funnels through
|
|
2926
|
+
* here, so the dirty index is complete — an entity that is dirty is always in
|
|
2927
|
+
* the change set and therefore always flushed.
|
|
2928
|
+
*
|
|
2929
|
+
* No-tracking entities (AsNoTracking query results) are intentionally ignored
|
|
2930
|
+
* — like EF, mutating a no-tracking entity does not enqueue a write.
|
|
2931
|
+
*/
|
|
2932
|
+
__markDirty(model) {
|
|
2933
|
+
if (!model || model.__noTracking) return model;
|
|
2934
|
+
this.__track(model);
|
|
2935
|
+
this.__dirtyEntities.add(model);
|
|
2900
2936
|
return model;
|
|
2901
2937
|
}
|
|
2902
2938
|
|
|
@@ -2922,6 +2958,7 @@ class context {
|
|
|
2922
2958
|
*/
|
|
2923
2959
|
__clearTracked() {
|
|
2924
2960
|
this.__trackedEntitiesMap.clear(); // sole registry; the list view follows
|
|
2961
|
+
this.__dirtyEntities.clear();
|
|
2925
2962
|
}
|
|
2926
2963
|
|
|
2927
2964
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.13",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./MasterRecord.js",
|