masterrecord 1.0.1 → 1.0.2
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/Entity/entityTrackerModel.js +8 -1
- package/QueryLanguage/queryMethods.js +9 -1
- package/context.js +20 -3
- package/package.json +1 -1
- package/test/fixtures/setter-and-update-drop/config/environments/env.setterdrop.json +1 -0
- package/test/fixtures/setter-and-update-drop/db/setdropctx.sqlite +0 -0
- package/test/setter-and-update-drop.test.js +168 -0
|
@@ -108,7 +108,14 @@ class EntityTrackerModel {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
modelClass.__state = "modified";
|
|
111
|
-
|
|
111
|
+
// Deduplicate: setting the same field twice used to push
|
|
112
|
+
// the name twice, producing duplicate assignments in the
|
|
113
|
+
// UPDATE SET clause (`SET col = ?, col = ?`) which is a
|
|
114
|
+
// hard error in Postgres and may silently take-last in
|
|
115
|
+
// MySQL/SQLite.
|
|
116
|
+
if (!modelClass.__dirtyFields.includes(modelField)) {
|
|
117
|
+
modelClass.__dirtyFields.push(modelField);
|
|
118
|
+
}
|
|
112
119
|
// ensure this entity is tracked on any modification
|
|
113
120
|
if(modelClass.__context && typeof modelClass.__context.__track === 'function'){
|
|
114
121
|
modelClass.__context.__track(modelClass);
|
|
@@ -609,7 +609,15 @@ class queryMethods{
|
|
|
609
609
|
}
|
|
610
610
|
}
|
|
611
611
|
|
|
612
|
-
|
|
612
|
+
// Apply fieldDef.set transform if the entity defines one — the
|
|
613
|
+
// tracker-entity setter (entityTrackerModel.build) already does
|
|
614
|
+
// this on UPDATE; we need the same behavior on INSERT so values
|
|
615
|
+
// normalize consistently regardless of which path created them.
|
|
616
|
+
if (fieldDef && typeof fieldDef.set === 'function') {
|
|
617
|
+
this.__proto__["_" + fname] = fieldDef.set(value);
|
|
618
|
+
} else {
|
|
619
|
+
this.__proto__["_" + fname] = value;
|
|
620
|
+
}
|
|
613
621
|
if(!this.__dirtyFields.includes(fname)){
|
|
614
622
|
this.__dirtyFields.push(fname);
|
|
615
623
|
}
|
package/context.js
CHANGED
|
@@ -1699,9 +1699,14 @@ class context {
|
|
|
1699
1699
|
}
|
|
1700
1700
|
|
|
1701
1701
|
if (entities.length === 1) {
|
|
1702
|
-
// Single update - use existing logic
|
|
1702
|
+
// Single update - use existing logic.
|
|
1703
|
+
// NOTE: the second argument must be `__entity` (double underscore).
|
|
1704
|
+
// The previous code used `_entity` which is undefined, so
|
|
1705
|
+
// `removePrimarykeyandVirtual` received `undefined` and stripped
|
|
1706
|
+
// nothing — a latent bug that left primary-key/virtual fields in
|
|
1707
|
+
// the UPDATE SET clause if they ever landed in __dirtyFields.
|
|
1703
1708
|
const currentModel = entities[0];
|
|
1704
|
-
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.
|
|
1709
|
+
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.__entity);
|
|
1705
1710
|
const argu = this._SQLEngine._buildSQLEqualToParameterized(cleanCurrentModel);
|
|
1706
1711
|
|
|
1707
1712
|
if (argu !== -1) {
|
|
@@ -1721,7 +1726,7 @@ class context {
|
|
|
1721
1726
|
const updateQueries = [];
|
|
1722
1727
|
|
|
1723
1728
|
for (const currentModel of entities) {
|
|
1724
|
-
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.
|
|
1729
|
+
const cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel.__entity);
|
|
1725
1730
|
const argu = this._SQLEngine._buildSQLEqualToParameterized(cleanCurrentModel);
|
|
1726
1731
|
|
|
1727
1732
|
if (argu !== -1) {
|
|
@@ -1754,6 +1759,18 @@ class context {
|
|
|
1754
1759
|
await entity.afterSave.call(entity);
|
|
1755
1760
|
}
|
|
1756
1761
|
}
|
|
1762
|
+
|
|
1763
|
+
// Reset tracker state on the entity itself so a subsequent
|
|
1764
|
+
// saveChanges() doesn't re-emit the same UPDATE for the same fields.
|
|
1765
|
+
// Previously only _processBatchInserts did this, not updates —
|
|
1766
|
+
// leaving __state="modified" and __dirtyFields populated. On the
|
|
1767
|
+
// next modification of a different field, the stale fields would
|
|
1768
|
+
// be re-written, silently overwriting any concurrent external
|
|
1769
|
+
// changes to those columns.
|
|
1770
|
+
for (const entity of entities) {
|
|
1771
|
+
entity.__state = 'track';
|
|
1772
|
+
entity.__dirtyFields = [];
|
|
1773
|
+
}
|
|
1757
1774
|
}
|
|
1758
1775
|
|
|
1759
1776
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.002",
|
|
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",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"SetDropCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/setter-and-update-drop/db/"}}
|
|
Binary file
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reproducers for the "silently dropped writes" family of bugs that
|
|
3
|
+
* downstream consumers have been working around with raw _execute.
|
|
4
|
+
*
|
|
5
|
+
* 1. `.new()` setter does NOT apply `fieldDef.set` — tracker-entity setter
|
|
6
|
+
* does. Inconsistent behavior between INSERT and UPDATE paths.
|
|
7
|
+
* 2. `_processBatchUpdates` reads `currentModel._entity` (typo) instead of
|
|
8
|
+
* `__entity`, so `removePrimarykeyandVirtual` gets `undefined`.
|
|
9
|
+
* 3. After UPDATE, `__state` and `__dirtyFields` are not reset. On the next
|
|
10
|
+
* `saveChanges()` call, the same fields are re-written to the DB, which
|
|
11
|
+
* can silently overwrite concurrent changes from other processes.
|
|
12
|
+
* 4. Tracker setter pushes to `__dirtyFields` without de-duplication, so
|
|
13
|
+
* setting the same field multiple times produces duplicate assignments
|
|
14
|
+
* in the UPDATE SET clause. In Postgres this is a hard error
|
|
15
|
+
* ("multiple assignments to same column"); in SQLite/MySQL it may or
|
|
16
|
+
* may not work depending on driver version.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { test } from 'node:test';
|
|
20
|
+
import assert from 'node:assert/strict';
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
24
|
+
|
|
25
|
+
process.env.master = 'setterdrop';
|
|
26
|
+
|
|
27
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'setter-and-update-drop');
|
|
29
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
30
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
31
|
+
const dbFile = path.join(dbDir, 'setdropctx.sqlite');
|
|
32
|
+
|
|
33
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
34
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
35
|
+
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
36
|
+
|
|
37
|
+
fs.writeFileSync(
|
|
38
|
+
path.join(envDir, 'env.setterdrop.json'),
|
|
39
|
+
JSON.stringify({
|
|
40
|
+
SetDropCtx: {
|
|
41
|
+
type: 'better-sqlite3',
|
|
42
|
+
connection: dbDir + path.sep,
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const { default: context } = await import('../context.js');
|
|
48
|
+
|
|
49
|
+
class User {
|
|
50
|
+
id(db) { db.integer().primary().auto(); }
|
|
51
|
+
// `name` has a .set() transform that trims and lowercases — the
|
|
52
|
+
// canonical test for whether the setter path applies fieldDef.set.
|
|
53
|
+
name(db) { db.string().set(v => (typeof v === 'string' ? v.trim().toLowerCase() : v)); }
|
|
54
|
+
email(db) { db.string(); }
|
|
55
|
+
score(db) { db.integer(); }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class SetDropCtx extends context {
|
|
59
|
+
constructor() {
|
|
60
|
+
super();
|
|
61
|
+
this.env(envDir);
|
|
62
|
+
this.dbset(User);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
{
|
|
67
|
+
const ctx = new SetDropCtx();
|
|
68
|
+
ctx._SQLEngine.db.exec(`
|
|
69
|
+
CREATE TABLE IF NOT EXISTS User (
|
|
70
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
71
|
+
name TEXT,
|
|
72
|
+
email TEXT,
|
|
73
|
+
score INTEGER
|
|
74
|
+
);
|
|
75
|
+
`);
|
|
76
|
+
await ctx.close();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//
|
|
80
|
+
// Bug #1 — .new() setter must apply fieldDef.set
|
|
81
|
+
//
|
|
82
|
+
test('.new() setter applies fieldDef.set transform at write time', async () => {
|
|
83
|
+
const ctx = new SetDropCtx();
|
|
84
|
+
const u = ctx.User.new();
|
|
85
|
+
u.name = ' ALICE '; // whitespace + uppercase
|
|
86
|
+
u.email = 'alice@example.com';
|
|
87
|
+
u.score = 10;
|
|
88
|
+
await u.save();
|
|
89
|
+
|
|
90
|
+
const row = ctx._SQLEngine.db.prepare('SELECT name FROM User WHERE email = ?').get('alice@example.com');
|
|
91
|
+
assert.equal(row.name, 'alice', `.set() transform should have been applied on INSERT; got "${row.name}"`);
|
|
92
|
+
await ctx.close();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('Tracker setter applies fieldDef.set transform on UPDATE (baseline — already works)', async () => {
|
|
96
|
+
const ctx = new SetDropCtx();
|
|
97
|
+
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('bob', 'bob@example.com', 5);
|
|
98
|
+
|
|
99
|
+
const loaded = await ctx.User.where('u => u.email == $$', 'bob@example.com').single();
|
|
100
|
+
loaded.name = ' BOB-UPDATED ';
|
|
101
|
+
await loaded.save();
|
|
102
|
+
|
|
103
|
+
const row = ctx._SQLEngine.db.prepare('SELECT name FROM User WHERE email = ?').get('bob@example.com');
|
|
104
|
+
assert.equal(row.name, 'bob-updated', `.set() transform should apply on UPDATE; got "${row.name}"`);
|
|
105
|
+
await ctx.close();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
//
|
|
109
|
+
// Bug #2 — after UPDATE, __dirtyFields and __state are not reset;
|
|
110
|
+
// calling saveChanges twice for the same entity silently re-updates the
|
|
111
|
+
// same fields.
|
|
112
|
+
//
|
|
113
|
+
test('After UPDATE, __dirtyFields is cleared and __state is reset to track', async () => {
|
|
114
|
+
const ctx = new SetDropCtx();
|
|
115
|
+
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('carol', 'carol@example.com', 1);
|
|
116
|
+
|
|
117
|
+
const loaded = await ctx.User.where('u => u.email == $$', 'carol@example.com').single();
|
|
118
|
+
loaded.score = 2;
|
|
119
|
+
await loaded.save();
|
|
120
|
+
|
|
121
|
+
assert.deepEqual(loaded.__dirtyFields, [], '__dirtyFields should be empty after UPDATE');
|
|
122
|
+
assert.equal(loaded.__state, 'track', '__state should be "track" after UPDATE');
|
|
123
|
+
await ctx.close();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('saveChanges re-called with no modifications after UPDATE is a no-op', async () => {
|
|
127
|
+
const ctx = new SetDropCtx();
|
|
128
|
+
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('dave', 'dave@example.com', 1);
|
|
129
|
+
|
|
130
|
+
const loaded = await ctx.User.where('u => u.email == $$', 'dave@example.com').single();
|
|
131
|
+
loaded.score = 99;
|
|
132
|
+
await loaded.save();
|
|
133
|
+
|
|
134
|
+
// Simulate concurrent external write: another process updates score to 42.
|
|
135
|
+
ctx._SQLEngine.db.prepare('UPDATE User SET score = 42 WHERE email = ?').run('dave@example.com');
|
|
136
|
+
|
|
137
|
+
// Re-saving without touching the entity should NOT overwrite the external
|
|
138
|
+
// change; the entity is no longer modified.
|
|
139
|
+
await ctx.saveChanges();
|
|
140
|
+
|
|
141
|
+
const row = ctx._SQLEngine.db.prepare('SELECT score FROM User WHERE email = ?').get('dave@example.com');
|
|
142
|
+
assert.equal(row.score, 42, 'second saveChanges must not silently re-apply the prior write');
|
|
143
|
+
await ctx.close();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
//
|
|
147
|
+
// Bug #3 — setting the same field twice must not produce duplicate
|
|
148
|
+
// SET assignments (SQLite errors on duplicates; Postgres always errors).
|
|
149
|
+
//
|
|
150
|
+
test('Setting the same field twice produces only one assignment in UPDATE', async () => {
|
|
151
|
+
const ctx = new SetDropCtx();
|
|
152
|
+
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('eve', 'eve@example.com', 1);
|
|
153
|
+
|
|
154
|
+
const loaded = await ctx.User.where('u => u.email == $$', 'eve@example.com').single();
|
|
155
|
+
loaded.score = 2;
|
|
156
|
+
loaded.score = 3; // second assignment — must not push duplicate into __dirtyFields
|
|
157
|
+
loaded.score = 4;
|
|
158
|
+
|
|
159
|
+
// Only one occurrence of 'score' in dirtyFields
|
|
160
|
+
const dupCount = loaded.__dirtyFields.filter(f => f === 'score').length;
|
|
161
|
+
assert.equal(dupCount, 1, `score should appear exactly once in __dirtyFields; got ${dupCount} (${JSON.stringify(loaded.__dirtyFields)})`);
|
|
162
|
+
|
|
163
|
+
// And the UPDATE must succeed and persist the last value.
|
|
164
|
+
await loaded.save();
|
|
165
|
+
const row = ctx._SQLEngine.db.prepare('SELECT score FROM User WHERE email = ?').get('eve@example.com');
|
|
166
|
+
assert.equal(row.score, 4, 'last assigned value must be persisted');
|
|
167
|
+
await ctx.close();
|
|
168
|
+
});
|