masterrecord 1.5.6 → 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,12 +1,22 @@
1
1
  # MasterRecord Changelog
2
2
 
3
- ## v1.5.6 — re-bundle docs after clearing an npm-scanner false-positive
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.
4
10
 
5
- 1.5.5 published without the `docs/` folder or `CHANGES.md` in the tarball: npm's publish-time malware scanner returned `403 forbidden by security policy` on a documentation string. The trigger was a literal SQL-injection **example** — `'10; DROP TABLE users'` — in `readme.md` and `docs/SECURITY.md`, illustrating input the ORM *rejects*; its executable `; DROP TABLE users` shape reads as an attack payload to the scanner. 1.5.5 shipped the runtime with that string neutralized but the docs excluded to get the release out.
11
+ ## v1.5.7 — unset fields no longer read as their definition function
6
12
 
7
- This release **re-includes the full `docs/` folder and `CHANGES.md`** in the published package. A sweep of every markdown file (SQL-injection payloads, shell/command injection, XSS, obfuscation/encoded blobs, secrets, path traversal) found no other flaggable strings — the one payload example was the only real match. Runtime code is unchanged from 1.5.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.
14
+
15
+ **Fix:** `add()` blanks unset function-valued fields (a plain own-property write — the full accessor install can't run pre-INSERT), and `attachTrackingTo()` (run after INSERT) treats a function value as "unset" and backs it with `undefined`. So an unset field reads as `undefined`/`null` — and `!entity.field` behaves — after `add()`, after INSERT, and when queried. Relationship navigation properties are untouched. New test: `test/unset-field-not-function.test.js` pins all three paths (add / insert / query) plus that setting a previously-unset field still persists. Full suite green (0 fail, 293 pass, 20 gated skipped); 0 lint errors.
16
+
17
+ ## v1.5.6 — re-bundle docs after clearing an npm-scanner false-positive
8
18
 
9
- _(No functional change — packaging only. All 1.5.5 fixes remain in place.)_
19
+ 1.5.5 shipped without the `docs/` folder or `CHANGES.md` — npm's publish-time malware scanner returned `403 forbidden by security policy` on a documentation string: the literal SQL-injection **example** `'10; DROP TABLE users'` (in `readme.md` and `docs/SECURITY.md`), whose executable `; DROP TABLE users` shape reads as an attack payload even though it documents input the ORM *rejects*. That string is neutralized, and a sweep of every markdown file (SQL payloads, shell/command injection, XSS, obfuscation, secrets, path traversal) found no other matches — so this release **re-includes the full `docs/` folder and `CHANGES.md`** in the tarball. Packaging only; runtime unchanged from 1.5.5.
10
20
 
11
21
  ## v1.5.5 — three shared-connection concurrency bugs: cross-instance saves, poisoned empty cache, dropped post-insert edits
12
22
 
@@ -454,7 +454,13 @@ class EntityTrackerModel {
454
454
  if (this._isRelationship(currentEntity[modelField])) continue;
455
455
  const desc = Object.getOwnPropertyDescriptor(target, modelField);
456
456
  if (desc && !('value' in desc)) return target; // already tracked
457
- captured[modelField] = target[modelField];
457
+ // An UNSET declared field still resolves to its definition method on
458
+ // the class prototype (e.g. `apiKey(db){...}`) — a truthy function.
459
+ // Capturing that verbatim would make `!entity.apiKey` false and read
460
+ // back a function. An entity field value is never a function, so
461
+ // treat a function here as "unset" and back it with undefined.
462
+ const cur = target[modelField];
463
+ captured[modelField] = (typeof cur === 'function') ? undefined : cur;
458
464
  scalarFields.push(modelField);
459
465
  }
460
466
 
@@ -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
  }
@@ -1017,6 +1017,21 @@ class queryMethods{
1017
1017
  entityValue.__entity = this.__entity;
1018
1018
  entityValue.__context = this.__context;
1019
1019
  entityValue.__name = this.__entity.__name;
1020
+ // An UNSET declared field still resolves to its definition method on the
1021
+ // class prototype (e.g. `apiKey(db){...}`) — a truthy function — so
1022
+ // `!entity.apiKey` never fires. Blank those so an added entity reads
1023
+ // cleanly even before save. (This is a plain own-property write, not the
1024
+ // full accessor install, which the insert path can't take pre-save; full
1025
+ // change-tracking accessors are attached after INSERT.) Relationship
1026
+ // navigation properties are left alone.
1027
+ for (const f in this.__entity) {
1028
+ const def = this.__entity[f];
1029
+ const isRel = def && (def.type === 'hasOne' || def.type === 'hasMany'
1030
+ || def.type === 'hasManyThrough' || def.relationshipType === 'belongsTo');
1031
+ if (!isRel && typeof entityValue[f] === 'function') {
1032
+ entityValue[f] = undefined;
1033
+ }
1034
+ }
1020
1035
  this.__context.__track(entityValue);
1021
1036
  }
1022
1037
 
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.6",
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",