masterrecord 1.5.8 → 1.5.9

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.
Files changed (3) hide show
  1. package/CHANGES.md +10 -0
  2. package/context.js +31 -4
  3. package/package.json +1 -1
package/CHANGES.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.5.9 — saveChanges() no longer sweeps away a concurrently-mutated entity (shared/singleton context lost write)
4
+
5
+ **Bug (silent lost write on a shared context):** queried rows are auto-tracked into the context's tracked-entity list, and `saveChanges()` snapshots that whole list, writes the dirty rows, and `__untrack()`s the entire snapshot. When one context instance is shared across concurrent units of work (e.g. a **singleton context** serving many requests), request A's save snapshots a row that request B loaded (clean), B mutates it while A's save is in flight, and A then untracks the snapshot — removing B's now-dirty row from tracking. B's own `saveChanges()` then finds it untracked and **silently issues no UPDATE**. The batch wasn't empty, so the "no tracked entities" warning never fired and the handler returned success (observed as an admin plan change round-tripping back to `free`, and other vanished updates).
6
+
7
+ **Fix:** `__untrack()` now drops only **clean** (`'track'`) entities. An entity that is still dirty (insert/modified/delete) at untrack time was not written by this save — it became dirty after the snapshot — so it is preserved and its pending write still lands. Clean entities are still released, so the tracked list doesn't grow unbounded. In the normal per-request context the whole batch is clean at this point, so behavior is unchanged. New test: `test/shared-context-untrack-preserves-dirty.test.js`.
8
+
9
+ **Strongly recommended:** scope a context **per request/unit of work**, not as a singleton. This fix removes the silent lost-write, but a context is a unit of work — sharing one instance across concurrent requests still risks cross-request transaction conflation (one request's save can commit/roll back another's pending rows). Use a scoped/transient context per request.
10
+
11
+ Full suite green (0 fail, 298 pass, 20 gated skipped); 0 lint errors.
12
+
3
13
  ## v1.5.8 — falsy defaults (0/false/'') apply on insert; MySQL syncTable quotes reserved words
4
14
 
5
15
  **Bug 1 — falsy defaults were dropped (regression surfaced by 1.5.7, latent long before).** `insertManager.validateEntity` applied a column's default with `if (currentEntity.default)` — a **truthiness** test — so `.default(0)`, `.default(false)`, and `.default('')` were silently skipped. This was masked until 1.5.7 because an unset field read as its definition *function* (truthy, non-null), so the required-field check passed anyway. Once 1.5.7 made unset fields correctly read as `undefined`, a `.notNullable().default(0)` column (e.g. a `blocked` flag) failed insert validation with *"… is a required Field"* — broadly breaking registration/insert across every model with a falsy-defaulted NOT NULL column. **Fix:** apply the default whenever it is not `undefined`/`null` (not on truthiness), writing it into both the clean model (inserted) and the raw model (read by the required-field check). Genuinely required fields with no default are still enforced. New test: `test/falsy-default-applied.test.js`.
package/context.js CHANGED
@@ -2235,13 +2235,40 @@ class context {
2235
2235
  }
2236
2236
  }
2237
2237
 
2238
- /** Remove a specific batch of entities from change tracking. */
2238
+ /**
2239
+ * Remove a batch of entities from change tracking after a save — but ONLY
2240
+ * entities that are currently CLEAN (state 'track').
2241
+ *
2242
+ * saveChanges() snapshots the tracked list, writes the dirty rows (which are
2243
+ * then reset to 'track'), and calls this to drop the batch. A batched entity
2244
+ * that is STILL dirty at this point (insert/modified/delete) was NOT written
2245
+ * by this save — it became dirty AFTER the snapshot. That happens when a
2246
+ * context instance is shared across concurrent units of work (e.g. a
2247
+ * singleton context serving many requests): request A's save snapshots a row
2248
+ * that request B loaded, B mutates it while A's save is in flight, and if A
2249
+ * then untracked it unconditionally, B's own saveChanges() would find it
2250
+ * untracked and silently drop the UPDATE. Skipping dirty entities keeps
2251
+ * B's pending change tracked so B's save still writes it. (In the normal
2252
+ * per-request context the whole batch is clean here, so behavior is
2253
+ * unchanged.)
2254
+ *
2255
+ * NOTE: a context is a unit of work and is safest scoped per request.
2256
+ * Sharing one instance across requests still risks cross-request
2257
+ * transaction conflation; this guard removes the silent-lost-write, not the
2258
+ * need for request-scoped contexts.
2259
+ */
2239
2260
  __untrack(batch) {
2240
2261
  if (!batch || !batch.length) return;
2241
- const drop = new Set(batch);
2242
- this.__trackedEntities = this.__trackedEntities.filter(e => !drop.has(e));
2262
+ const drop = new Set();
2243
2263
  for (const e of batch) {
2244
- if (e && e.__ID !== undefined) this.__trackedEntitiesMap.delete(e.__ID);
2264
+ if (!e) continue;
2265
+ if (e.__state && e.__state !== 'track') continue; // keep still-dirty entities
2266
+ drop.add(e);
2267
+ }
2268
+ if (drop.size === 0) return;
2269
+ this.__trackedEntities = this.__trackedEntities.filter(e => !drop.has(e));
2270
+ for (const e of drop) {
2271
+ if (e.__ID !== undefined) this.__trackedEntitiesMap.delete(e.__ID);
2245
2272
  }
2246
2273
  }
2247
2274
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.5.8",
3
+ "version": "1.5.9",
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",