masterrecord 1.5.7 → 1.5.8

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,13 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.5.8 — falsy defaults (0/false/'') apply on insert; MySQL syncTable quotes reserved words
4
+
5
+ **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`.
6
+
7
+ **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`.
8
+
9
+ Full suite green (0 fail, 296 pass, 20 gated skipped); 0 lint errors.
10
+
3
11
  ## v1.5.7 — unset fields no longer read as their definition function
4
12
 
5
13
  **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/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.8",
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",