masterrecord 0.3.68 → 1.0.0
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 +26 -7
- 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 +26 -6
- package/package.json +34 -5
- package/postgresEngine.js +29 -8
- 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/esm-imports.test.js +110 -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/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/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,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test to check what insert() returns
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const SQLLiteEngine = require('../SQLLiteEngine');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
|
|
9
|
-
const dbPath = path.join(__dirname, '..', 'database', 'insertReturnTest.db');
|
|
10
|
-
if (fs.existsSync(dbPath)) {
|
|
11
|
-
fs.unlinkSync(dbPath);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const engine = new SQLLiteEngine({ database: dbPath, prefix: '' });
|
|
15
|
-
|
|
16
|
-
// Create table
|
|
17
|
-
engine._run(`CREATE TABLE test_users (
|
|
18
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
19
|
-
name TEXT
|
|
20
|
-
)`);
|
|
21
|
-
|
|
22
|
-
// Test insert
|
|
23
|
-
const queryObject = {
|
|
24
|
-
__entity: {
|
|
25
|
-
__name: 'test_users',
|
|
26
|
-
id: { type: 'integer', primary: true, auto: true },
|
|
27
|
-
name: { type: 'string' }
|
|
28
|
-
},
|
|
29
|
-
name: 'Test User'
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
async function test() {
|
|
33
|
-
const result = await engine.insert(queryObject);
|
|
34
|
-
console.log('Insert result:', result);
|
|
35
|
-
console.log('result.id:', result.id);
|
|
36
|
-
console.log('typeof result.id:', typeof result.id);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
test().catch(console.error);
|
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: Lifecycle Hooks
|
|
3
|
-
* Tests: beforeSave, afterSave, beforeDelete, afterDelete
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const masterrecord = require('../MasterRecord.js');
|
|
7
|
-
const path = require('path');
|
|
8
|
-
const fs = require('fs');
|
|
9
|
-
|
|
10
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
11
|
-
console.log("║ Lifecycle Hooks Test ║");
|
|
12
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
13
|
-
|
|
14
|
-
let passed = 0;
|
|
15
|
-
let failed = 0;
|
|
16
|
-
|
|
17
|
-
// Track hook executions
|
|
18
|
-
const hookLog = [];
|
|
19
|
-
|
|
20
|
-
class User {
|
|
21
|
-
id(db) {
|
|
22
|
-
db.integer().primary().auto();
|
|
23
|
-
}
|
|
24
|
-
name(db) {
|
|
25
|
-
db.string();
|
|
26
|
-
}
|
|
27
|
-
email(db) {
|
|
28
|
-
db.string();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
beforeSave() {
|
|
32
|
-
hookLog.push('beforeSave');
|
|
33
|
-
// Automatically set email domain if missing
|
|
34
|
-
if (this.email && !this.email.includes('@')) {
|
|
35
|
-
this.email = this.email + '@example.com';
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
afterSave() {
|
|
40
|
-
hookLog.push('afterSave');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
beforeDelete() {
|
|
44
|
-
hookLog.push('beforeDelete');
|
|
45
|
-
// Prevent deletion of admin users
|
|
46
|
-
if (this.name === 'admin') {
|
|
47
|
-
throw new Error('Cannot delete admin user');
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
afterDelete() {
|
|
52
|
-
hookLog.push('afterDelete');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
class TestContext extends masterrecord.context {
|
|
57
|
-
constructor() {
|
|
58
|
-
super();
|
|
59
|
-
}
|
|
60
|
-
onConfig(db) {
|
|
61
|
-
this.dbset(User);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const dbPath = path.join(__dirname, '..', 'database', 'lifecycleHooks.db');
|
|
66
|
-
if (fs.existsSync(dbPath)) {
|
|
67
|
-
fs.unlinkSync(dbPath);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async function runTests() {
|
|
71
|
-
const db = new TestContext();
|
|
72
|
-
|
|
73
|
-
// Initialize SQLite database
|
|
74
|
-
const SQLLiteEngine = require('../SQLLiteEngine');
|
|
75
|
-
const sqlite3 = require('better-sqlite3');
|
|
76
|
-
db.isSQLite = true;
|
|
77
|
-
db.isMySQL = false;
|
|
78
|
-
db.isPostgres = false;
|
|
79
|
-
db._SQLEngine = new SQLLiteEngine();
|
|
80
|
-
db.db = new sqlite3(dbPath);
|
|
81
|
-
db._SQLEngine.setDB(db.db, 'better-sqlite3');
|
|
82
|
-
|
|
83
|
-
db.onConfig();
|
|
84
|
-
|
|
85
|
-
// Create table
|
|
86
|
-
db.db.exec('CREATE TABLE IF NOT EXISTS User (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT)');
|
|
87
|
-
|
|
88
|
-
// Test 1: beforeSave hook executes on insert
|
|
89
|
-
console.log("📝 Test 1: beforeSave hook executes on insert");
|
|
90
|
-
console.log("──────────────────────────────────────────────────");
|
|
91
|
-
try {
|
|
92
|
-
hookLog.length = 0;
|
|
93
|
-
|
|
94
|
-
const user = db.User.new();
|
|
95
|
-
user.name = 'Alice';
|
|
96
|
-
user.email = 'alice'; // No @ - should be fixed by hook
|
|
97
|
-
await user.save();
|
|
98
|
-
|
|
99
|
-
if (hookLog.includes('beforeSave') && user.email === 'alice@example.com') {
|
|
100
|
-
console.log(" ✓ beforeSave executed before insert");
|
|
101
|
-
console.log(" ✓ Email auto-completed to: alice@example.com");
|
|
102
|
-
passed++;
|
|
103
|
-
} else {
|
|
104
|
-
console.log(` ✗ Hook failed: ${hookLog.join(', ')}, email: ${user.email}`);
|
|
105
|
-
failed++;
|
|
106
|
-
}
|
|
107
|
-
} catch(err) {
|
|
108
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
109
|
-
failed++;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Test 2: afterSave hook executes after insert
|
|
113
|
-
console.log("\n📝 Test 2: afterSave hook executes after insert");
|
|
114
|
-
console.log("──────────────────────────────────────────────────");
|
|
115
|
-
try {
|
|
116
|
-
if (hookLog.includes('afterSave') && hookLog.indexOf('afterSave') > hookLog.indexOf('beforeSave')) {
|
|
117
|
-
console.log(" ✓ afterSave executed after insert");
|
|
118
|
-
console.log(" ✓ Hooks executed in order: beforeSave → afterSave");
|
|
119
|
-
passed++;
|
|
120
|
-
} else {
|
|
121
|
-
console.log(` ✗ Hook order incorrect: ${hookLog.join(' → ')}`);
|
|
122
|
-
failed++;
|
|
123
|
-
}
|
|
124
|
-
} catch(err) {
|
|
125
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
126
|
-
failed++;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Test 3: beforeSave hook executes on update
|
|
130
|
-
console.log("\n📝 Test 3: beforeSave hook executes on update");
|
|
131
|
-
console.log("──────────────────────────────────────────────────");
|
|
132
|
-
try {
|
|
133
|
-
hookLog.length = 0;
|
|
134
|
-
|
|
135
|
-
const user = await db.User.findById(1);
|
|
136
|
-
user.name = 'Alice Updated';
|
|
137
|
-
await user.save();
|
|
138
|
-
|
|
139
|
-
if (hookLog.includes('beforeSave')) {
|
|
140
|
-
console.log(" ✓ beforeSave executed on update");
|
|
141
|
-
passed++;
|
|
142
|
-
} else {
|
|
143
|
-
console.log(` ✗ beforeSave not called on update`);
|
|
144
|
-
failed++;
|
|
145
|
-
}
|
|
146
|
-
} catch(err) {
|
|
147
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
148
|
-
failed++;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Test 4: beforeDelete hook can prevent deletion
|
|
152
|
-
console.log("\n📝 Test 4: beforeDelete hook can prevent deletion");
|
|
153
|
-
console.log("──────────────────────────────────────────────────");
|
|
154
|
-
try {
|
|
155
|
-
const admin = db.User.new();
|
|
156
|
-
admin.name = 'admin';
|
|
157
|
-
admin.email = 'admin@example.com';
|
|
158
|
-
await admin.save();
|
|
159
|
-
|
|
160
|
-
const adminId = admin.id;
|
|
161
|
-
|
|
162
|
-
try {
|
|
163
|
-
await admin.delete();
|
|
164
|
-
console.log(` ✗ Deletion should have been prevented`);
|
|
165
|
-
failed++;
|
|
166
|
-
} catch (hookError) {
|
|
167
|
-
if (hookError.message.includes('Cannot delete admin user')) {
|
|
168
|
-
// Check that admin still exists
|
|
169
|
-
const check = await db.User.findById(adminId);
|
|
170
|
-
if (check && check.name === 'admin') {
|
|
171
|
-
console.log(" ✓ beforeDelete prevented deletion of admin user");
|
|
172
|
-
passed++;
|
|
173
|
-
} else {
|
|
174
|
-
console.log(` ✗ Admin was deleted despite hook`);
|
|
175
|
-
failed++;
|
|
176
|
-
}
|
|
177
|
-
} else {
|
|
178
|
-
console.log(` ✗ Unexpected error: ${hookError.message}`);
|
|
179
|
-
failed++;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
} catch(err) {
|
|
183
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
184
|
-
failed++;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Test 5: afterDelete hook executes after successful deletion
|
|
188
|
-
console.log("\n📝 Test 5: afterDelete hook executes after deletion");
|
|
189
|
-
console.log("──────────────────────────────────────────────────");
|
|
190
|
-
try {
|
|
191
|
-
hookLog.length = 0;
|
|
192
|
-
|
|
193
|
-
const user = db.User.new();
|
|
194
|
-
user.name = 'ToDelete';
|
|
195
|
-
user.email = 'delete@example.com';
|
|
196
|
-
await user.save();
|
|
197
|
-
|
|
198
|
-
await user.delete();
|
|
199
|
-
|
|
200
|
-
if (hookLog.includes('beforeDelete') && hookLog.includes('afterDelete')) {
|
|
201
|
-
console.log(" ✓ beforeDelete and afterDelete executed");
|
|
202
|
-
console.log(" ✓ Hooks executed in order: beforeDelete → afterDelete");
|
|
203
|
-
passed++;
|
|
204
|
-
} else {
|
|
205
|
-
console.log(` ✗ Delete hooks missing: ${hookLog.join(', ')}`);
|
|
206
|
-
failed++;
|
|
207
|
-
}
|
|
208
|
-
} catch(err) {
|
|
209
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
210
|
-
failed++;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Test 6: Batch operations execute hooks for all entities
|
|
214
|
-
console.log("\n📝 Test 6: Batch operations execute hooks for all entities");
|
|
215
|
-
console.log("──────────────────────────────────────────────────");
|
|
216
|
-
try {
|
|
217
|
-
hookLog.length = 0;
|
|
218
|
-
|
|
219
|
-
const user1 = db.User.new();
|
|
220
|
-
user1.name = 'Batch1';
|
|
221
|
-
user1.email = 'batch1';
|
|
222
|
-
|
|
223
|
-
const user2 = db.User.new();
|
|
224
|
-
user2.name = 'Batch2';
|
|
225
|
-
user2.email = 'batch2';
|
|
226
|
-
|
|
227
|
-
await db.saveChanges();
|
|
228
|
-
|
|
229
|
-
const beforeSaveCount = hookLog.filter(h => h === 'beforeSave').length;
|
|
230
|
-
const afterSaveCount = hookLog.filter(h => h === 'afterSave').length;
|
|
231
|
-
|
|
232
|
-
if (beforeSaveCount === 2 && afterSaveCount === 2) {
|
|
233
|
-
console.log(" ✓ Hooks executed for all entities in batch");
|
|
234
|
-
console.log(` ✓ beforeSave: ${beforeSaveCount}x, afterSave: ${afterSaveCount}x`);
|
|
235
|
-
passed++;
|
|
236
|
-
} else {
|
|
237
|
-
console.log(` ✗ Hook counts incorrect: beforeSave=${beforeSaveCount}, afterSave=${afterSaveCount}`);
|
|
238
|
-
failed++;
|
|
239
|
-
}
|
|
240
|
-
} catch(err) {
|
|
241
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
242
|
-
failed++;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Summary
|
|
246
|
-
console.log("\n" + "=".repeat(64));
|
|
247
|
-
console.log(`Test Results: ${passed} passed, ${failed} failed`);
|
|
248
|
-
console.log("=".repeat(64));
|
|
249
|
-
|
|
250
|
-
if (failed > 0) {
|
|
251
|
-
process.exit(1);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
runTests().catch(err => {
|
|
256
|
-
console.error('Fatal error:', err);
|
|
257
|
-
process.exit(1);
|
|
258
|
-
});
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: Multi-Context Cache Sharing
|
|
3
|
-
*
|
|
4
|
-
* Verifies that cache is shared across context instances
|
|
5
|
-
* so invalidation in one context affects all contexts
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const context = require('../context');
|
|
9
|
-
const QueryCache = require('../Cache/QueryCache');
|
|
10
|
-
|
|
11
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
12
|
-
console.log("║ Multi-Context Cache Sharing Test ║");
|
|
13
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
14
|
-
|
|
15
|
-
let passed = 0;
|
|
16
|
-
let failed = 0;
|
|
17
|
-
|
|
18
|
-
// Test 1: Multiple context instances share the same cache
|
|
19
|
-
console.log("📝 Test 1: Multiple contexts share same cache instance");
|
|
20
|
-
console.log("──────────────────────────────────────────────────");
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
class TestContext extends context {
|
|
24
|
-
constructor() {
|
|
25
|
-
super();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const db1 = new TestContext();
|
|
30
|
-
const db2 = new TestContext();
|
|
31
|
-
|
|
32
|
-
const areSameInstance = db1._queryCache === db2._queryCache;
|
|
33
|
-
const isSharedCache = db1._queryCache === context._sharedQueryCache;
|
|
34
|
-
|
|
35
|
-
if(areSameInstance && isSharedCache) {
|
|
36
|
-
console.log(" ✓ Both context instances share the same cache");
|
|
37
|
-
console.log(" ✓ Cache is stored in static property");
|
|
38
|
-
passed++;
|
|
39
|
-
} else {
|
|
40
|
-
console.log(` ✗ Contexts have separate caches (BUG!)`);
|
|
41
|
-
console.log(` ✗ db1._queryCache === db2._queryCache: ${areSameInstance}`);
|
|
42
|
-
console.log(` ✗ Shared cache exists: ${isSharedCache}`);
|
|
43
|
-
failed++;
|
|
44
|
-
}
|
|
45
|
-
} catch(err) {
|
|
46
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
47
|
-
failed++;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Test 2: Cache operations in one context affect another context
|
|
51
|
-
console.log("\n📝 Test 2: Cache operations are shared");
|
|
52
|
-
console.log("──────────────────────────────────────────────────");
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
class TestContext extends context {
|
|
56
|
-
constructor() {
|
|
57
|
-
super();
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const db1 = new TestContext();
|
|
62
|
-
const db2 = new TestContext();
|
|
63
|
-
|
|
64
|
-
// Context 1: Add to cache
|
|
65
|
-
const key = db1._queryCache.generateKey('SELECT * FROM users', [], 'users');
|
|
66
|
-
db1._queryCache.set(key, [{ id: 1, name: 'John' }], 'users');
|
|
67
|
-
|
|
68
|
-
// Context 2: Should see the same cached data
|
|
69
|
-
const cached = db2._queryCache.get(key);
|
|
70
|
-
|
|
71
|
-
if(cached && cached[0].name === 'John') {
|
|
72
|
-
console.log(" ✓ Cache data visible across contexts");
|
|
73
|
-
passed++;
|
|
74
|
-
} else {
|
|
75
|
-
console.log(` ✗ Cache data not shared across contexts`);
|
|
76
|
-
failed++;
|
|
77
|
-
}
|
|
78
|
-
} catch(err) {
|
|
79
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
80
|
-
failed++;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Test 3: Cache invalidation in one context affects another
|
|
84
|
-
console.log("\n📝 Test 3: Cache invalidation is shared");
|
|
85
|
-
console.log("──────────────────────────────────────────────────");
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
class TestContext extends context {
|
|
89
|
-
constructor() {
|
|
90
|
-
super();
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const db1 = new TestContext();
|
|
95
|
-
const db2 = new TestContext();
|
|
96
|
-
|
|
97
|
-
// Context 1: Add multiple entries to cache
|
|
98
|
-
const key1 = db1._queryCache.generateKey('SELECT * FROM users WHERE id=1', [], 'users');
|
|
99
|
-
const key2 = db1._queryCache.generateKey('SELECT * FROM users WHERE id=2', [], 'users');
|
|
100
|
-
db1._queryCache.set(key1, { id: 1, name: 'John' }, 'users');
|
|
101
|
-
db1._queryCache.set(key2, { id: 2, name: 'Jane' }, 'users');
|
|
102
|
-
|
|
103
|
-
// Verify both are cached
|
|
104
|
-
const beforeCached1 = db1._queryCache.get(key1);
|
|
105
|
-
const beforeCached2 = db1._queryCache.get(key2);
|
|
106
|
-
|
|
107
|
-
// Context 2: Invalidate User table
|
|
108
|
-
db2._queryCache.invalidateTable('users');
|
|
109
|
-
|
|
110
|
-
// Context 1: Should see invalidation
|
|
111
|
-
const afterCached1 = db1._queryCache.get(key1);
|
|
112
|
-
const afterCached2 = db1._queryCache.get(key2);
|
|
113
|
-
|
|
114
|
-
if(beforeCached1 !== null && beforeCached2 !== null && afterCached1 === null && afterCached2 === null) {
|
|
115
|
-
console.log(" ✓ Data was cached in context 1");
|
|
116
|
-
console.log(" ✓ Invalidation in context 2 affected context 1");
|
|
117
|
-
console.log(" ✓ Cache properly shared across contexts");
|
|
118
|
-
passed++;
|
|
119
|
-
} else {
|
|
120
|
-
console.log(` ✗ Invalidation not shared properly`);
|
|
121
|
-
console.log(` ✗ Before: cached1=${beforeCached1 !== null}, cached2=${beforeCached2 !== null}`);
|
|
122
|
-
console.log(` ✗ After: cached1=${afterCached1 !== null}, cached2=${afterCached2 !== null}`);
|
|
123
|
-
failed++;
|
|
124
|
-
}
|
|
125
|
-
} catch(err) {
|
|
126
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
127
|
-
failed++;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Test 4: Cache statistics are shared
|
|
131
|
-
console.log("\n📝 Test 4: Cache statistics are shared");
|
|
132
|
-
console.log("──────────────────────────────────────────────────");
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
// Clear existing cache for clean test
|
|
136
|
-
context._sharedQueryCache.clear();
|
|
137
|
-
|
|
138
|
-
class TestContext extends context {
|
|
139
|
-
constructor() {
|
|
140
|
-
super();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const db1 = new TestContext();
|
|
145
|
-
const db2 = new TestContext();
|
|
146
|
-
|
|
147
|
-
// Context 1: Generate hits/misses
|
|
148
|
-
const key = db1._queryCache.generateKey('query', [], 'users');
|
|
149
|
-
db1._queryCache.set(key, 'data', 'users');
|
|
150
|
-
db1._queryCache.get(key); // Hit
|
|
151
|
-
db1._queryCache.get('nonexistent'); // Miss
|
|
152
|
-
|
|
153
|
-
// Context 2: Should see same stats
|
|
154
|
-
const stats1 = db1.getCacheStats();
|
|
155
|
-
const stats2 = db2.getCacheStats();
|
|
156
|
-
|
|
157
|
-
if(stats1.hits === 1 && stats2.hits === 1 && stats1.misses === 1 && stats2.misses === 1) {
|
|
158
|
-
console.log(" ✓ Cache statistics shared across contexts");
|
|
159
|
-
console.log(` ✓ Both contexts see: ${stats1.hits} hit, ${stats1.misses} miss`);
|
|
160
|
-
passed++;
|
|
161
|
-
} else {
|
|
162
|
-
console.log(` ✗ Statistics not shared`);
|
|
163
|
-
console.log(` ✗ db1 stats: ${JSON.stringify(stats1)}`);
|
|
164
|
-
console.log(` ✗ db2 stats: ${JSON.stringify(stats2)}`);
|
|
165
|
-
failed++;
|
|
166
|
-
}
|
|
167
|
-
} catch(err) {
|
|
168
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
169
|
-
failed++;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Test 5: Clear cache from one context affects all
|
|
173
|
-
console.log("\n📝 Test 5: Clear cache affects all contexts");
|
|
174
|
-
console.log("──────────────────────────────────────────────────");
|
|
175
|
-
|
|
176
|
-
try {
|
|
177
|
-
class TestContext extends context {
|
|
178
|
-
constructor() {
|
|
179
|
-
super();
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const db1 = new TestContext();
|
|
184
|
-
const db2 = new TestContext();
|
|
185
|
-
|
|
186
|
-
// Context 1: Add data
|
|
187
|
-
const key = db1._queryCache.generateKey('query', [], 'users');
|
|
188
|
-
db1._queryCache.set(key, 'data', 'users');
|
|
189
|
-
|
|
190
|
-
// Verify cached in both
|
|
191
|
-
const before1 = db1._queryCache.get(key);
|
|
192
|
-
const before2 = db2._queryCache.get(key);
|
|
193
|
-
|
|
194
|
-
// Context 2: Clear cache
|
|
195
|
-
db2.clearQueryCache();
|
|
196
|
-
|
|
197
|
-
// Both contexts should see empty cache
|
|
198
|
-
const after1 = db1._queryCache.get(key);
|
|
199
|
-
const after2 = db2._queryCache.get(key);
|
|
200
|
-
|
|
201
|
-
if(before1 !== null && before2 !== null && after1 === null && after2 === null) {
|
|
202
|
-
console.log(" ✓ Data cached in both contexts initially");
|
|
203
|
-
console.log(" ✓ Clear from context 2 affected context 1");
|
|
204
|
-
passed++;
|
|
205
|
-
} else {
|
|
206
|
-
console.log(` ✗ Clear not shared across contexts`);
|
|
207
|
-
failed++;
|
|
208
|
-
}
|
|
209
|
-
} catch(err) {
|
|
210
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
211
|
-
failed++;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Summary
|
|
215
|
-
console.log("\n╔════════════════════════════════════════════════════════════════╗");
|
|
216
|
-
console.log("║ Test Summary ║");
|
|
217
|
-
console.log("╚════════════════════════════════════════════════════════════════╝");
|
|
218
|
-
console.log(`\n ✓ Passed: ${passed}`);
|
|
219
|
-
console.log(` ✗ Failed: ${failed}`);
|
|
220
|
-
console.log(` 📊 Total: ${passed + failed}\n`);
|
|
221
|
-
|
|
222
|
-
if(failed === 0) {
|
|
223
|
-
console.log(" 🎉 All tests passed!\n");
|
|
224
|
-
console.log(" ✅ Cache is properly shared across context instances");
|
|
225
|
-
console.log(" ✅ Bug fix verified: Multi-context cache invalidation works\n");
|
|
226
|
-
process.exit(0);
|
|
227
|
-
} else {
|
|
228
|
-
console.log(" ❌ Some tests failed\n");
|
|
229
|
-
process.exit(1);
|
|
230
|
-
}
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: New entity detection in migrations
|
|
3
|
-
*
|
|
4
|
-
* Verifies that when a new entity is added to a context that already has
|
|
5
|
-
* existing entities (non-empty oldSchema), the migration system correctly
|
|
6
|
-
* includes it in the tables array and detects it as a new table.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const path = require('path');
|
|
10
|
-
const Module = require('module');
|
|
11
|
-
const __MASTERRECORD_ROOT__ = path.join(__dirname, '..');
|
|
12
|
-
const __ORIGINAL_REQUIRE__ = Module.prototype.require;
|
|
13
|
-
Module.prototype.require = function(request) {
|
|
14
|
-
if (request === 'masterrecord' || request.startsWith('masterrecord/')) {
|
|
15
|
-
const resolved = request === 'masterrecord'
|
|
16
|
-
? __MASTERRECORD_ROOT__
|
|
17
|
-
: path.join(__MASTERRECORD_ROOT__, request.slice('masterrecord/'.length));
|
|
18
|
-
return __ORIGINAL_REQUIRE__.call(this, resolved);
|
|
19
|
-
}
|
|
20
|
-
return __ORIGINAL_REQUIRE__.call(this, request);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const Migrations = require('../Migrations/migrations');
|
|
24
|
-
|
|
25
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
26
|
-
console.log("║ New Entity Migration Detection Test ║");
|
|
27
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
28
|
-
|
|
29
|
-
let passed = 0;
|
|
30
|
-
let failed = 0;
|
|
31
|
-
|
|
32
|
-
function assert(condition, label) {
|
|
33
|
-
if (condition) {
|
|
34
|
-
console.log(` ✓ ${label}`);
|
|
35
|
-
passed++;
|
|
36
|
-
} else {
|
|
37
|
-
console.log(` ✗ ${label}`);
|
|
38
|
-
failed++;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const migration = new Migrations();
|
|
43
|
-
|
|
44
|
-
// Existing entity schema (already in the database)
|
|
45
|
-
const userSchema = {
|
|
46
|
-
__name: 'User',
|
|
47
|
-
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
48
|
-
name: { name: 'name', type: 'string' },
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// New entity schema (being added)
|
|
52
|
-
const customTaskTypeSchema = {
|
|
53
|
-
__name: 'CustomTaskType',
|
|
54
|
-
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
55
|
-
label: { name: 'label', type: 'string' },
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// -----------------------------------------------------------
|
|
59
|
-
// Test 1: First migration (empty oldSchema) — baseline
|
|
60
|
-
// -----------------------------------------------------------
|
|
61
|
-
console.log("Test 1: First migration (empty oldSchema) includes all entities");
|
|
62
|
-
console.log("──────────────────────────────────────────────────");
|
|
63
|
-
{
|
|
64
|
-
const oldSchema = [];
|
|
65
|
-
const newSchema = [userSchema, customTaskTypeSchema];
|
|
66
|
-
|
|
67
|
-
const result = migration.buildUpObject(oldSchema, newSchema);
|
|
68
|
-
|
|
69
|
-
assert(result.User !== undefined, 'User table is in result');
|
|
70
|
-
assert(result.CustomTaskType !== undefined, 'CustomTaskType table is in result');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// -----------------------------------------------------------
|
|
74
|
-
// Test 2: Subsequent migration — existing entity still works
|
|
75
|
-
// -----------------------------------------------------------
|
|
76
|
-
console.log("\nTest 2: Subsequent migration — existing entity included");
|
|
77
|
-
console.log("──────────────────────────────────────────────────");
|
|
78
|
-
{
|
|
79
|
-
const oldSchema = [userSchema];
|
|
80
|
-
const newSchema = [userSchema];
|
|
81
|
-
|
|
82
|
-
const result = migration.buildUpObject(oldSchema, newSchema);
|
|
83
|
-
|
|
84
|
-
assert(result.User !== undefined, 'User table is in result');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// -----------------------------------------------------------
|
|
88
|
-
// Test 3: Subsequent migration — NEW entity is included (the bug)
|
|
89
|
-
// -----------------------------------------------------------
|
|
90
|
-
console.log("\nTest 3: Subsequent migration — new entity is included");
|
|
91
|
-
console.log("──────────────────────────────────────────────────");
|
|
92
|
-
{
|
|
93
|
-
const oldSchema = [userSchema];
|
|
94
|
-
const newSchema = [userSchema, customTaskTypeSchema];
|
|
95
|
-
|
|
96
|
-
const result = migration.buildUpObject(oldSchema, newSchema);
|
|
97
|
-
|
|
98
|
-
assert(result.User !== undefined, 'User table is in result');
|
|
99
|
-
assert(result.CustomTaskType !== undefined, 'CustomTaskType table is in result (was undefined before fix)');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// -----------------------------------------------------------
|
|
103
|
-
// Test 4: hasChanges detects the new entity
|
|
104
|
-
// -----------------------------------------------------------
|
|
105
|
-
console.log("\nTest 4: hasChanges detects new entity");
|
|
106
|
-
console.log("──────────────────────────────────────────────────");
|
|
107
|
-
{
|
|
108
|
-
const oldSchema = [userSchema];
|
|
109
|
-
const newSchema = [userSchema, customTaskTypeSchema];
|
|
110
|
-
|
|
111
|
-
const changed = migration.hasChanges(oldSchema, newSchema);
|
|
112
|
-
|
|
113
|
-
assert(changed === true, 'hasChanges returns true for new entity');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// -----------------------------------------------------------
|
|
117
|
-
// Test 5: hasChanges returns false when nothing changed
|
|
118
|
-
// -----------------------------------------------------------
|
|
119
|
-
console.log("\nTest 5: hasChanges returns false when nothing changed");
|
|
120
|
-
console.log("──────────────────────────────────────────────────");
|
|
121
|
-
{
|
|
122
|
-
const oldSchema = [userSchema];
|
|
123
|
-
const newSchema = [userSchema];
|
|
124
|
-
|
|
125
|
-
const changed = migration.hasChanges(oldSchema, newSchema);
|
|
126
|
-
|
|
127
|
-
assert(changed === false, 'hasChanges returns false when schemas are identical');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// -----------------------------------------------------------
|
|
131
|
-
// Test 6: Multiple new entities in one migration
|
|
132
|
-
// -----------------------------------------------------------
|
|
133
|
-
console.log("\nTest 6: Multiple new entities in one migration");
|
|
134
|
-
console.log("──────────────────────────────────────────────────");
|
|
135
|
-
{
|
|
136
|
-
const tagSchema = {
|
|
137
|
-
__name: 'Tag',
|
|
138
|
-
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
139
|
-
value: { name: 'value', type: 'string' },
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const oldSchema = [userSchema];
|
|
143
|
-
const newSchema = [userSchema, customTaskTypeSchema, tagSchema];
|
|
144
|
-
|
|
145
|
-
const result = migration.buildUpObject(oldSchema, newSchema);
|
|
146
|
-
|
|
147
|
-
assert(result.User !== undefined, 'User table is in result');
|
|
148
|
-
assert(result.CustomTaskType !== undefined, 'CustomTaskType is in result');
|
|
149
|
-
assert(result.Tag !== undefined, 'Tag is in result');
|
|
150
|
-
|
|
151
|
-
const changed = migration.hasChanges(oldSchema, newSchema);
|
|
152
|
-
assert(changed === true, 'hasChanges detects multiple new entities');
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Summary
|
|
156
|
-
console.log("\n" + "=".repeat(64));
|
|
157
|
-
console.log(`Test Results: ${passed} passed, ${failed} failed`);
|
|
158
|
-
console.log("=".repeat(64));
|
|
159
|
-
|
|
160
|
-
if (failed > 0) {
|
|
161
|
-
process.exit(1);
|
|
162
|
-
}
|