masterrecord 1.5.7 → 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.
package/CHANGES.md CHANGED
@@ -1,5 +1,23 @@
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
+
13
+ ## v1.5.8 — falsy defaults (0/false/'') apply on insert; MySQL syncTable quotes reserved words
14
+
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`.
16
+
17
+ **Bug 2 — MySQL `syncTable` didn't quote identifiers.** The MySQL branch built its inline `ALTER TABLE <t> MODIFY COLUMN <c> …` **without backticks**, so a column named after a reserved word (`key`, `order`, `group`, …) blew up with a SQL syntax error while `syncTable` reconciled its nullability/default. `createTable`, `addColumn` (via the query builder) and the Postgres branch all quote — this inline path was the outlier. **Fix:** backtick-quote the table and column. New test: `test/mysql-synctable-quotes-identifiers.test.js`.
18
+
19
+ Full suite green (0 fail, 296 pass, 20 gated skipped); 0 lint errors.
20
+
3
21
  ## v1.5.7 — unset fields no longer read as their definition function
4
22
 
5
23
  **Bug (a missing-value check that never fires):** entity fields are declared as methods (`apiKey(db){ db.string(); }`). On a constructed entity (`new Model()` → `add()` → INSERT) any field the caller never set still resolved to that **method** — a truthy function — so a guard like `if (!row.apiKey)` silently never fired (`typeof row.apiKey === 'function'`, not `undefined`). Queried rows were already correct (built from the DB row, not the class prototype); the trap was only on entities you build.
@@ -529,7 +529,12 @@ class schema{
529
529
  } else {
530
530
  defPart = ' DEFAULT NULL';
531
531
  }
532
- const alter = `ALTER TABLE ${tableName} MODIFY COLUMN ${d.name} ${type} ${nullPart}${defPart}`;
532
+ // Backtick-quote the identifiers so a reserved word (e.g. a
533
+ // column named `key`, `order`, `group`) doesn't produce a
534
+ // syntax error. createTable, addColumn (via the builder) and
535
+ // the Postgres branch below all quote; this inline MODIFY was
536
+ // the one path that didn't.
537
+ const alter = `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${d.name}\` ${type} ${nullPart}${defPart}`;
533
538
  await this.context._execute(alter);
534
539
  }
535
540
  }
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/insertManager.js CHANGED
@@ -413,12 +413,19 @@ class InsertManager {
413
413
  }
414
414
  }
415
415
 
416
- // check if there is a default value
417
- if (currentEntity.default) {
416
+ // Apply a declared default when the value is missing. Use an
417
+ // explicit undefined/null check, NOT truthiness — a legitimate
418
+ // default of `0`, `false`, or `''` is falsy and must still be
419
+ // applied. Write it into BOTH the clean model (what gets inserted)
420
+ // and the raw model (what the required-field check below reads),
421
+ // mirroring the timestamp auto-population.
422
+ if (currentEntity.default !== undefined && currentEntity.default !== null) {
418
423
  if (currentRealModel[entity] === undefined || currentRealModel[entity] === null) {
419
- // if its empty add the default value
420
424
  currentRealModel[entity] = currentEntity.default;
421
425
  }
426
+ if (currentModel[entity] === undefined || currentModel[entity] === null) {
427
+ currentModel[entity] = currentEntity.default;
428
+ }
422
429
  }
423
430
 
424
431
  // SKIP belongs too ----- // call sets for correct data for DB
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.5.7",
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",