masterrecord 0.3.68 → 1.0.1
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/.claude/settings.local.json +3 -1
- package/CHANGES.md +31 -2
- package/Cache/QueryCache.js +10 -4
- package/Cache/RedisQueryCache.js +3 -2
- package/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +2 -2
- package/Entity/entityTrackerModel.js +3 -3
- package/Entity/fieldTransformer.js +1 -1
- package/FOREIGN_KEY_STRING_FIX.md +2 -2
- package/MasterRecord.js +6 -43
- package/Migrations/cli.js +44 -113
- package/Migrations/dependencyGraph.js +1 -1
- package/Migrations/migrationMySQLQuery.js +1 -1
- package/Migrations/migrationPostgresQuery.js +1 -1
- package/Migrations/migrationSQLiteQuery.js +1 -1
- package/Migrations/migrationTemplate.js +8 -39
- package/Migrations/migrations.js +16 -14
- package/Migrations/pathUtils.js +2 -5
- package/Migrations/pathUtils.test.js +33 -49
- package/Migrations/schema.js +52 -89
- package/QueryLanguage/queryMethods.js +4 -4
- package/QueryLanguage/queryParameters.js +1 -1
- package/QueryLanguage/queryScript.js +4 -3
- package/SQLLiteEngine.js +30 -8
- package/Tools.js +10 -8
- package/context.js +43 -42
- package/deleteManager.js +2 -2
- package/docs/MIGRATIONS_GUIDE.md +237 -532
- package/docs/POSTGRESQL_SETUP.md +4 -4
- package/docs/QUERY_CACHING_GUIDE.md +8 -7
- package/eslint.config.js +40 -0
- package/insertManager.js +28 -20
- package/mySQLConnect.js +2 -2
- package/mySQLEngine.js +161 -16
- package/package.json +34 -5
- package/postgresEngine.js +74 -26
- package/postgresSyncConnect.js +5 -3
- package/prettier.config.js +22 -0
- package/readme.md +35 -27
- package/test/context-errors.test.js +43 -0
- package/test/count-chained-where.test.js +136 -0
- package/test/esm-imports.test.js +110 -0
- package/test/fixtures/count-chained-where/config/environments/env.countchained.json +1 -0
- package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/query-builder-parity/config/environments/env.parity.json +1 -0
- package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/migration-template.test.js +47 -0
- package/test/new-setter-large-string.test.js +191 -0
- package/test/pool-key.test.js +43 -0
- package/test/query-builder-parity.test.js +260 -0
- package/test/sqlite-lifecycle.test.js +76 -0
- package/.eslintrc.js +0 -290
- package/.prettierrc.js +0 -109
- package/test/anyCommaStringTest.js +0 -237
- package/test/anyMethodTest.js +0 -176
- package/test/attachDetached.test.js +0 -303
- package/test/bulk-operations-test.js +0 -235
- package/test/cache-toObject-test.js +0 -105
- package/test/cacheIntegration.test.js +0 -319
- package/test/config-glob-pattern-test.js +0 -232
- package/test/double-where-bug-test.js +0 -71
- package/test/ensure-ready-test.js +0 -251
- package/test/entity-deduplication-test.js +0 -174
- package/test/entity-methods-test.js +0 -269
- package/test/findByIdTest.js +0 -227
- package/test/foreign-key-string-value-test.js +0 -406
- package/test/global-model-registry-test.js +0 -538
- package/test/id-setting-validation.js +0 -202
- package/test/includesFeatureTest.js +0 -183
- package/test/includesTransformTest.js +0 -110
- package/test/index-bug-fix-test.js +0 -188
- package/test/insert-return-test.js +0 -39
- package/test/lifecycle-hooks-test.js +0 -258
- package/test/multiContextCache.test.js +0 -230
- package/test/new-entity-migration-test.js +0 -162
- package/test/newMethodTest.js +0 -330
- package/test/newMethodUnitTest.js +0 -320
- package/test/optInCache.test.js +0 -221
- package/test/parameterizedPlaceholderTest.js +0 -159
- package/test/pool-registry-test.js +0 -303
- package/test/post-insert-tracking-test.js +0 -258
- package/test/postgresEngineTest.js +0 -463
- package/test/postgresIntegrationTest.js +0 -381
- package/test/promise-detection-test.js +0 -290
- package/test/qa-context-pattern-test.js +0 -319
- package/test/query-helpers-test.js +0 -258
- package/test/query-isolation-test.js +0 -59
- package/test/queryCache.test.js +0 -148
- package/test/securityTest.js +0 -268
- package/test/seed-data-test.js +0 -212
- package/test/seed-deduplication-test.js +0 -313
- package/test/seed-features-integration-test.js +0 -418
- package/test/seed-migration-template-test.js +0 -220
- package/test/singleDollarPlaceholderTest.js +0 -238
- package/test/tablePrefixTest.js +0 -100
- package/test/transformerTest.js +0 -287
- package/test/v0.3.34-bug-fixes-test.js +0 -428
- package/test/validation-test.js +0 -302
- package/test/whereChainingTest.js +0 -88
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: Post-INSERT entity tracking
|
|
3
|
-
*
|
|
4
|
-
* Verifies that after saveChanges() INSERTs a new entity, modifying
|
|
5
|
-
* properties on that same in-memory object correctly transitions to
|
|
6
|
-
* "modified" state and re-registers with the change tracker, so that
|
|
7
|
-
* a subsequent saveChanges() issues an UPDATE.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
// Alias 'masterrecord' to local root
|
|
11
|
-
const path = require('path');
|
|
12
|
-
const Module = require('module');
|
|
13
|
-
const __MASTERRECORD_ROOT__ = path.join(__dirname, '..');
|
|
14
|
-
const __ORIGINAL_REQUIRE__ = Module.prototype.require;
|
|
15
|
-
Module.prototype.require = function(request) {
|
|
16
|
-
if (request === 'masterrecord' || request.startsWith('masterrecord/')) {
|
|
17
|
-
const resolved = request === 'masterrecord'
|
|
18
|
-
? __MASTERRECORD_ROOT__
|
|
19
|
-
: path.join(__MASTERRECORD_ROOT__, request.slice('masterrecord/'.length));
|
|
20
|
-
return __ORIGINAL_REQUIRE__.call(this, resolved);
|
|
21
|
-
}
|
|
22
|
-
return __ORIGINAL_REQUIRE__.call(this, request);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const queryMethods = require('../QueryLanguage/queryMethods');
|
|
26
|
-
|
|
27
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
28
|
-
console.log("║ Post-INSERT Entity Tracking Test ║");
|
|
29
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
30
|
-
|
|
31
|
-
let passed = 0;
|
|
32
|
-
let failed = 0;
|
|
33
|
-
|
|
34
|
-
function assert(condition, label) {
|
|
35
|
-
if (condition) {
|
|
36
|
-
console.log(` ✓ ${label}`);
|
|
37
|
-
passed++;
|
|
38
|
-
} else {
|
|
39
|
-
console.log(` ✗ ${label}`);
|
|
40
|
-
failed++;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Minimal simulated context with tracking
|
|
45
|
-
function makeContext(entity) {
|
|
46
|
-
const ctx = {
|
|
47
|
-
__trackedEntities: [],
|
|
48
|
-
__trackedEntitiesMap: new Map(),
|
|
49
|
-
__entities: [entity],
|
|
50
|
-
__track(model) {
|
|
51
|
-
if (!this.__trackedEntitiesMap.has(model.__ID)) {
|
|
52
|
-
this.__trackedEntities.push(model);
|
|
53
|
-
this.__trackedEntitiesMap.set(model.__ID, model);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
__clearTracked() {
|
|
57
|
-
this.__trackedEntities = [];
|
|
58
|
-
this.__trackedEntitiesMap.clear();
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
return ctx;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Entity definition
|
|
65
|
-
const userEntity = {
|
|
66
|
-
__name: 'User',
|
|
67
|
-
id: { type: 'integer', primary: true, auto: true, nullable: true },
|
|
68
|
-
status: { type: 'string', nullable: true },
|
|
69
|
-
name: { type: 'string', nullable: true },
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
// Build a queryMethods-style dbset so we can call .new()
|
|
73
|
-
function makeDbSet(ctx, entity) {
|
|
74
|
-
const qs = new queryMethods();
|
|
75
|
-
qs.__entity = entity;
|
|
76
|
-
qs.__context = ctx;
|
|
77
|
-
return qs;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// -----------------------------------------------------------
|
|
81
|
-
// Test 1: .new() entity starts in "insert" state
|
|
82
|
-
// -----------------------------------------------------------
|
|
83
|
-
console.log("Test 1: .new() entity starts in insert state");
|
|
84
|
-
console.log("──────────────────────────────────────────────────");
|
|
85
|
-
{
|
|
86
|
-
const ctx = makeContext(userEntity);
|
|
87
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
88
|
-
const entity = dbset.new();
|
|
89
|
-
|
|
90
|
-
assert(entity.__state === 'insert', 'State is "insert"');
|
|
91
|
-
assert(Array.isArray(entity.__dirtyFields), 'Has dirtyFields array');
|
|
92
|
-
assert(ctx.__trackedEntitiesMap.has(entity.__ID), 'Entity is tracked');
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// -----------------------------------------------------------
|
|
96
|
-
// Test 2: Setting properties during "insert" state keeps state
|
|
97
|
-
// -----------------------------------------------------------
|
|
98
|
-
console.log("\nTest 2: Properties set during insert state keep insert state");
|
|
99
|
-
console.log("──────────────────────────────────────────────────");
|
|
100
|
-
{
|
|
101
|
-
const ctx = makeContext(userEntity);
|
|
102
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
103
|
-
const entity = dbset.new();
|
|
104
|
-
|
|
105
|
-
entity.status = 'queued';
|
|
106
|
-
entity.name = 'Test';
|
|
107
|
-
|
|
108
|
-
assert(entity.__state === 'insert', 'State stays "insert"');
|
|
109
|
-
assert(entity.__dirtyFields.includes('status'), 'status in dirtyFields');
|
|
110
|
-
assert(entity.__dirtyFields.includes('name'), 'name in dirtyFields');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// -----------------------------------------------------------
|
|
114
|
-
// Test 3: Simulating post-INSERT transition
|
|
115
|
-
// -----------------------------------------------------------
|
|
116
|
-
console.log("\nTest 3: After INSERT, entity transitions to track state");
|
|
117
|
-
console.log("──────────────────────────────────────────────────");
|
|
118
|
-
{
|
|
119
|
-
const ctx = makeContext(userEntity);
|
|
120
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
121
|
-
const entity = dbset.new();
|
|
122
|
-
entity.status = 'queued';
|
|
123
|
-
|
|
124
|
-
// Simulate what _processBatchInserts now does after INSERT
|
|
125
|
-
entity.__state = 'track';
|
|
126
|
-
entity.__dirtyFields = [];
|
|
127
|
-
|
|
128
|
-
// Simulate saveChanges clearing tracked entities
|
|
129
|
-
ctx.__clearTracked();
|
|
130
|
-
|
|
131
|
-
assert(entity.__state === 'track', 'State is "track" after INSERT');
|
|
132
|
-
assert(entity.__dirtyFields.length === 0, 'dirtyFields cleared');
|
|
133
|
-
assert(!ctx.__trackedEntitiesMap.has(entity.__ID), 'Entity removed from tracker');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// -----------------------------------------------------------
|
|
137
|
-
// Test 4: Modifying after INSERT transitions to "modified" and re-tracks
|
|
138
|
-
// -----------------------------------------------------------
|
|
139
|
-
console.log("\nTest 4: Modification after INSERT transitions to modified + re-tracks");
|
|
140
|
-
console.log("──────────────────────────────────────────────────");
|
|
141
|
-
{
|
|
142
|
-
const ctx = makeContext(userEntity);
|
|
143
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
144
|
-
const entity = dbset.new();
|
|
145
|
-
entity.status = 'queued';
|
|
146
|
-
|
|
147
|
-
// Simulate post-INSERT transition + clear
|
|
148
|
-
entity.__state = 'track';
|
|
149
|
-
entity.__dirtyFields = [];
|
|
150
|
-
ctx.__clearTracked();
|
|
151
|
-
|
|
152
|
-
// Now modify — this is the bug scenario
|
|
153
|
-
entity.status = 'completed';
|
|
154
|
-
|
|
155
|
-
assert(entity.__state === 'modified', 'State transitions to "modified"');
|
|
156
|
-
assert(entity.__dirtyFields.includes('status'), 'status in dirtyFields');
|
|
157
|
-
assert(entity.__dirtyFields.length === 1, 'Only modified field is dirty');
|
|
158
|
-
assert(ctx.__trackedEntitiesMap.has(entity.__ID), 'Entity re-registered with tracker');
|
|
159
|
-
assert(ctx.__trackedEntities.length === 1, 'Exactly one tracked entity');
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// -----------------------------------------------------------
|
|
163
|
-
// Test 5: Multiple modifications after INSERT
|
|
164
|
-
// -----------------------------------------------------------
|
|
165
|
-
console.log("\nTest 5: Multiple modifications after INSERT");
|
|
166
|
-
console.log("──────────────────────────────────────────────────");
|
|
167
|
-
{
|
|
168
|
-
const ctx = makeContext(userEntity);
|
|
169
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
170
|
-
const entity = dbset.new();
|
|
171
|
-
entity.status = 'queued';
|
|
172
|
-
entity.name = 'Job1';
|
|
173
|
-
|
|
174
|
-
// Simulate post-INSERT + clear
|
|
175
|
-
entity.__state = 'track';
|
|
176
|
-
entity.__dirtyFields = [];
|
|
177
|
-
ctx.__clearTracked();
|
|
178
|
-
|
|
179
|
-
// Multiple modifications
|
|
180
|
-
entity.status = 'completed';
|
|
181
|
-
entity.name = 'Job1-done';
|
|
182
|
-
|
|
183
|
-
assert(entity.__state === 'modified', 'State is "modified"');
|
|
184
|
-
assert(entity.__dirtyFields.includes('status'), 'status tracked');
|
|
185
|
-
assert(entity.__dirtyFields.includes('name'), 'name tracked');
|
|
186
|
-
assert(ctx.__trackedEntities.length === 1, 'Entity tracked only once (idempotent)');
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// -----------------------------------------------------------
|
|
190
|
-
// Test 6: Entity value is correct after post-INSERT modification
|
|
191
|
-
// -----------------------------------------------------------
|
|
192
|
-
console.log("\nTest 6: Values are correct through full lifecycle");
|
|
193
|
-
console.log("──────────────────────────────────────────────────");
|
|
194
|
-
{
|
|
195
|
-
const ctx = makeContext(userEntity);
|
|
196
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
197
|
-
const entity = dbset.new();
|
|
198
|
-
entity.status = 'queued';
|
|
199
|
-
|
|
200
|
-
assert(entity.status === 'queued', 'Initial value correct');
|
|
201
|
-
|
|
202
|
-
// Post-INSERT
|
|
203
|
-
entity.__state = 'track';
|
|
204
|
-
entity.__dirtyFields = [];
|
|
205
|
-
ctx.__clearTracked();
|
|
206
|
-
|
|
207
|
-
assert(entity.status === 'queued', 'Value preserved after INSERT transition');
|
|
208
|
-
|
|
209
|
-
entity.status = 'completed';
|
|
210
|
-
assert(entity.status === 'completed', 'Updated value correct');
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// -----------------------------------------------------------
|
|
214
|
-
// Test 7: _processTrackedEntities would route correctly
|
|
215
|
-
// -----------------------------------------------------------
|
|
216
|
-
console.log("\nTest 7: Simulated _processTrackedEntities routes to UPDATE");
|
|
217
|
-
console.log("──────────────────────────────────────────────────");
|
|
218
|
-
{
|
|
219
|
-
const ctx = makeContext(userEntity);
|
|
220
|
-
const dbset = makeDbSet(ctx, userEntity);
|
|
221
|
-
const entity = dbset.new();
|
|
222
|
-
entity.status = 'queued';
|
|
223
|
-
|
|
224
|
-
// Post-INSERT
|
|
225
|
-
entity.__state = 'track';
|
|
226
|
-
entity.__dirtyFields = [];
|
|
227
|
-
ctx.__clearTracked();
|
|
228
|
-
|
|
229
|
-
// Modify
|
|
230
|
-
entity.status = 'completed';
|
|
231
|
-
|
|
232
|
-
// Simulate _processTrackedEntities grouping
|
|
233
|
-
const toInsert = [];
|
|
234
|
-
const toUpdate = [];
|
|
235
|
-
for (const tracked of ctx.__trackedEntities) {
|
|
236
|
-
switch (tracked.__state) {
|
|
237
|
-
case 'insert': toInsert.push(tracked); break;
|
|
238
|
-
case 'modified':
|
|
239
|
-
if (tracked.__dirtyFields && tracked.__dirtyFields.length > 0) {
|
|
240
|
-
toUpdate.push(tracked);
|
|
241
|
-
}
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
assert(toInsert.length === 0, 'No entities routed to INSERT');
|
|
247
|
-
assert(toUpdate.length === 1, 'One entity routed to UPDATE');
|
|
248
|
-
assert(toUpdate[0] === entity, 'Correct entity routed to UPDATE');
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Summary
|
|
252
|
-
console.log("\n" + "=".repeat(64));
|
|
253
|
-
console.log(`Test Results: ${passed} passed, ${failed} failed`);
|
|
254
|
-
console.log("=".repeat(64));
|
|
255
|
-
|
|
256
|
-
if (failed > 0) {
|
|
257
|
-
process.exit(1);
|
|
258
|
-
}
|