masterrecord 1.5.13 → 1.5.14

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 CHANGED
@@ -1,5 +1,19 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.5.14 — context-level query tracking behavior (EF's QueryTrackingBehavior) + asTracking()
4
+
5
+ The remaining memory issue: on a long-lived/singleton context, tracked entities are held with **strong references** for the life of the context, so read-only queries that track their results grow the tracked set without bound. `asNoTracking()` (1.5.13) bounds it only per call site.
6
+
7
+ This is exactly EF Core's situation, so this release adds EF's context-level control:
8
+ - **`context.setQueryTrackingBehavior('no-track')`** — the default tracking behavior for the whole context (EF's `ChangeTracker.QueryTrackingBehavior = NoTracking`). All queries become no-tracking by default — a read-heavy context then retains nothing from reads — and you opt back in per query with...
9
+ - **`query.asTracking()`** (EF's `AsTracking()`) — track this query's results despite a no-track default, for the queries whose entities you modify and save.
10
+
11
+ Together with `asNoTracking()` and `clearChangeTracker()` (EF's `ChangeTracker.Clear()`), this is EF's full toolkit for bounding tracking.
12
+
13
+ **Honest note on memory (this matters):** EF Core does **not** hold tracked entities weakly — it uses strong references, and its memory bound is **context lifetime**: the `DbContext` is meant to be short-lived (scoped per request) and disposed, releasing the whole change tracker. masterrecord follows the same model. So the definitive fix for unbounded growth is a **request-scoped context**. If a context must be long-lived, set `setQueryTrackingBehavior('no-track')` and use `asTracking()` on write paths. (A `WeakRef`-based identity map would bound a long-lived context transparently, but that is a deliberate departure from EF's design, not what EF does — so it is not adopted here.)
14
+
15
+ New tests in `test/no-tracking-and-dirty-index.test.js`: a `no-track` default retains nothing while `asTracking()` still tracks and persists; the setter validates its argument. Full suite green (0 fail, 309 pass, 20 gated skipped); 0 lint errors.
16
+
3
17
  ## v1.5.13 — EF-Core-style change tracking: O(changes) saves (dirty index) + asNoTracking()
4
18
 
5
19
  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.
@@ -28,7 +28,9 @@ 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
31
+ // Tracking follows the context's default (EF's QueryTrackingBehavior);
32
+ // override per query with .asNoTracking() / .asTracking().
33
+ this.__noTracking = (context && context.__queryTrackingBehavior === 'no-track');
32
34
  }
33
35
 
34
36
  /**
@@ -44,6 +46,18 @@ class queryMethods{
44
46
  return this;
45
47
  }
46
48
 
49
+ /**
50
+ * Track this query's results even when the context defaults to no-tracking
51
+ * (EF Core's AsTracking). Use it on the queries whose entities you intend to
52
+ * modify and save, after setting the context to `setQueryTrackingBehavior('no-track')`.
53
+ *
54
+ * @example db.Users.asTracking().where(...).single(); // then edit + saveChanges()
55
+ */
56
+ asTracking(){
57
+ this.__noTracking = false;
58
+ return this;
59
+ }
60
+
47
61
  // build a single entity
48
62
  __singleEntityBuilder(dataModel){
49
63
  const $that = this;
package/context.js CHANGED
@@ -221,7 +221,13 @@ class context {
221
221
  // the two can never desynchronise — a dual array+map registry silently lost
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
- __trackedEntitiesMap = new Map(); // O(1) identity lookup; the authority
224
+ __trackedEntitiesMap = new Map(); // __ID -> entity; the authority
225
+ // Default query tracking behavior, EF-style (QueryTrackingBehavior).
226
+ // 'track' — queries track their results (edits persist); the default.
227
+ // 'no-track' — queries do NOT track (read-only; nothing retained). Set once
228
+ // on a long-lived/singleton context so read endpoints don't grow the
229
+ // tracked set; opt back in per query with .asTracking() where you update.
230
+ __queryTrackingBehavior = 'track';
225
231
  // Dirty index (like EF Core's StateManager): the subset of tracked entities
226
232
  // that are insert/modified/delete. Entities enter it the moment a setter /
227
233
  // add() / remove() makes them dirty, and leave it when flushed or detached.
@@ -2977,6 +2983,28 @@ class context {
2977
2983
  return this;
2978
2984
  }
2979
2985
 
2986
+ /**
2987
+ * Set the DEFAULT query tracking behavior for this context (EF Core's
2988
+ * `ChangeTracker.QueryTrackingBehavior`).
2989
+ *
2990
+ * 'no-track' — queries do NOT track their results by default, so a
2991
+ * read-heavy / long-lived (singleton) context retains nothing from
2992
+ * reads. Opt back into tracking per query with `.asTracking()` on the
2993
+ * ones whose results you intend to modify and save.
2994
+ * 'track' — the default: queries track their results so edits persist.
2995
+ *
2996
+ * This is the "not per-call-site" bound: set it once instead of adding
2997
+ * `.asNoTracking()` to every read. The definitive fix for unbounded growth
2998
+ * is still a request-scoped context; this is how to run a longer-lived one.
2999
+ */
3000
+ setQueryTrackingBehavior(behavior) {
3001
+ if (behavior !== 'track' && behavior !== 'no-track') {
3002
+ throw new Error(`masterrecord: setQueryTrackingBehavior expects 'track' or 'no-track', got ${JSON.stringify(behavior)}`);
3003
+ }
3004
+ this.__queryTrackingBehavior = behavior;
3005
+ return this;
3006
+ }
3007
+
2980
3008
  /**
2981
3009
  * Detach a single entity from change tracking. Pending changes on it are
2982
3010
  * dropped and it is no longer part of any future saveChanges().
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.5.13",
3
+ "version": "1.5.14",
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",