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
package/test/anyMethodTest.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: .any() Method Already Exists
|
|
3
|
-
*
|
|
4
|
-
* This test demonstrates that .any() is a working feature in MasterRecord.
|
|
5
|
-
* It's used inside WHERE clause lambda expressions, not as a dbset method.
|
|
6
|
-
*
|
|
7
|
-
* Usage: context.User.where(u => u.id.any($$), "1,2,3").toList()
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
const queryScript = require('../QueryLanguage/queryScript');
|
|
11
|
-
|
|
12
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
13
|
-
console.log("║ .any() Method Verification Test ║");
|
|
14
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
15
|
-
|
|
16
|
-
let passed = 0;
|
|
17
|
-
let failed = 0;
|
|
18
|
-
|
|
19
|
-
// Test 1: Verify .any() is whitelisted
|
|
20
|
-
console.log("📝 Test 1: Verify .any() is whitelisted in queryScript");
|
|
21
|
-
console.log("──────────────────────────────────────────────────");
|
|
22
|
-
|
|
23
|
-
try {
|
|
24
|
-
const script = new queryScript();
|
|
25
|
-
const isWhitelisted = script.isFunction('any');
|
|
26
|
-
|
|
27
|
-
if(isWhitelisted === true) {
|
|
28
|
-
console.log(" ✓ .any() is whitelisted");
|
|
29
|
-
console.log(" ✓ Can be used in lambda expressions");
|
|
30
|
-
passed++;
|
|
31
|
-
} else {
|
|
32
|
-
console.log(" ✗ .any() not whitelisted");
|
|
33
|
-
failed++;
|
|
34
|
-
}
|
|
35
|
-
} catch(err) {
|
|
36
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
37
|
-
failed++;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Test 2: Verify other whitelisted functions
|
|
41
|
-
console.log("\n📝 Test 2: Verify all whitelisted lambda functions");
|
|
42
|
-
console.log("──────────────────────────────────────────────────");
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
const script = new queryScript();
|
|
46
|
-
const functions = ['any', 'like', 'include'];
|
|
47
|
-
let allWhitelisted = true;
|
|
48
|
-
|
|
49
|
-
functions.forEach(func => {
|
|
50
|
-
const isWhitelisted = script.isFunction(func);
|
|
51
|
-
if(isWhitelisted) {
|
|
52
|
-
console.log(` ✓ ${func}() is whitelisted`);
|
|
53
|
-
} else {
|
|
54
|
-
console.log(` ✗ ${func}() not whitelisted`);
|
|
55
|
-
allWhitelisted = false;
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
if(allWhitelisted) {
|
|
60
|
-
passed++;
|
|
61
|
-
} else {
|
|
62
|
-
failed++;
|
|
63
|
-
}
|
|
64
|
-
} catch(err) {
|
|
65
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
66
|
-
failed++;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Test 3: Parse .any() in lambda expression
|
|
70
|
-
console.log("\n📝 Test 3: Parse .any() in WHERE clause");
|
|
71
|
-
console.log("──────────────────────────────────────────────────");
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
const script = new queryScript();
|
|
75
|
-
const lambdaExpr = "u => u.id.any($$)";
|
|
76
|
-
const result = script.where(lambdaExpr, "User");
|
|
77
|
-
|
|
78
|
-
// Check that functions were detected
|
|
79
|
-
if(result && result.where) {
|
|
80
|
-
console.log(" ✓ Lambda expression parsed");
|
|
81
|
-
console.log(` ✓ Entity: ${result.entity}`);
|
|
82
|
-
|
|
83
|
-
// The .any() should be detected and stored in query structure
|
|
84
|
-
console.log(" ℹ .any() will be converted to IN clause during SQL generation");
|
|
85
|
-
passed++;
|
|
86
|
-
} else {
|
|
87
|
-
console.log(" ✗ Failed to parse lambda expression");
|
|
88
|
-
failed++;
|
|
89
|
-
}
|
|
90
|
-
} catch(err) {
|
|
91
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
92
|
-
failed++;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Test 4: Verify .any() syntax examples
|
|
96
|
-
console.log("\n📝 Test 4: Valid .any() syntax examples");
|
|
97
|
-
console.log("──────────────────────────────────────────────────");
|
|
98
|
-
|
|
99
|
-
const examples = [
|
|
100
|
-
{ desc: "Simple field", lambda: "u => u.id.any($$)" },
|
|
101
|
-
{ desc: "String field", lambda: "u => u.name.any($$)" },
|
|
102
|
-
{ desc: "With AND condition", lambda: "u => u.id.any($$) && u.active == true" },
|
|
103
|
-
{ desc: "Multiple .any()", lambda: "u => u.id.any($$) && u.role_id.any($$)" }
|
|
104
|
-
];
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
const script = new queryScript();
|
|
108
|
-
let allValid = true;
|
|
109
|
-
|
|
110
|
-
examples.forEach(ex => {
|
|
111
|
-
try {
|
|
112
|
-
script.reset();
|
|
113
|
-
const result = script.where(ex.lambda, "User");
|
|
114
|
-
if(result && result.where) {
|
|
115
|
-
console.log(` ✓ ${ex.desc}: "${ex.lambda}"`);
|
|
116
|
-
} else {
|
|
117
|
-
console.log(` ✗ ${ex.desc}: Failed to parse`);
|
|
118
|
-
allValid = false;
|
|
119
|
-
}
|
|
120
|
-
} catch(err) {
|
|
121
|
-
console.log(` ✗ ${ex.desc}: ${err.message}`);
|
|
122
|
-
allValid = false;
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
if(allValid) {
|
|
127
|
-
passed++;
|
|
128
|
-
} else {
|
|
129
|
-
failed++;
|
|
130
|
-
}
|
|
131
|
-
} catch(err) {
|
|
132
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
133
|
-
failed++;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Summary
|
|
137
|
-
console.log("\n\n╔════════════════════════════════════════════════════════════════╗");
|
|
138
|
-
console.log("║ Test Summary ║");
|
|
139
|
-
console.log("╚════════════════════════════════════════════════════════════════╝");
|
|
140
|
-
|
|
141
|
-
const total = passed + failed;
|
|
142
|
-
const successRate = total > 0 ? Math.round((passed/total)*100) : 0;
|
|
143
|
-
|
|
144
|
-
console.log(`\n Total Tests: ${total}`);
|
|
145
|
-
console.log(` ✅ Passed: ${passed}`);
|
|
146
|
-
console.log(` ❌ Failed: ${failed}`);
|
|
147
|
-
console.log(` Success Rate: ${successRate}%\n`);
|
|
148
|
-
|
|
149
|
-
if(failed === 0){
|
|
150
|
-
console.log("🎉 All .any() tests passed!");
|
|
151
|
-
console.log("\n✨ .any() Already Works in MasterRecord!");
|
|
152
|
-
console.log("\n📖 How to Use .any():");
|
|
153
|
-
console.log("\n Option 1: .any() with comma-separated string");
|
|
154
|
-
console.log(" ─────────────────────────────────────────────");
|
|
155
|
-
console.log(" context.User.where(u => u.id.any($$), '1,2,3').toList();");
|
|
156
|
-
console.log(" context.User.where(u => u.name.any($$), 'Alice,Bob,Charlie').toList();");
|
|
157
|
-
console.log("\n Option 2: .includes() with JavaScript array (Recommended)");
|
|
158
|
-
console.log(" ──────────────────────────────────────────────────────────");
|
|
159
|
-
console.log(" const ids = [1, 2, 3];");
|
|
160
|
-
console.log(" context.User.where(u => $$.includes(u.id), ids).toList();");
|
|
161
|
-
console.log("\n📖 What .any() Does:");
|
|
162
|
-
console.log(" - Used inside WHERE clause lambda expressions");
|
|
163
|
-
console.log(" - Converts to SQL IN clause: WHERE id IN (?, ?, ?)");
|
|
164
|
-
console.log(" - Automatically parameterized for SQL injection protection");
|
|
165
|
-
console.log(" - Works with strings, numbers, and arrays");
|
|
166
|
-
console.log("\n📖 Implementation Location:");
|
|
167
|
-
console.log(" - File: QueryLanguage/queryScript.js");
|
|
168
|
-
console.log(" - Whitelisted: Line 506");
|
|
169
|
-
console.log(" - Parser: Lines 365-371");
|
|
170
|
-
console.log(" - Converts to: SQL IN clause with parameters");
|
|
171
|
-
console.log("\n✅ .any() is NOT missing - it already exists and works!\n");
|
|
172
|
-
process.exit(0);
|
|
173
|
-
} else {
|
|
174
|
-
console.log("⚠️ Some tests failed. Review implementation.");
|
|
175
|
-
process.exit(1);
|
|
176
|
-
}
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test: Attach Detached Entities
|
|
3
|
-
*
|
|
4
|
-
* Verifies that detached entities can be re-attached and tracked
|
|
5
|
-
* Like Entity Framework's context.Update() or Hibernate's session.merge()
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
console.log("╔════════════════════════════════════════════════════════════════╗");
|
|
9
|
-
console.log("║ Detached Entity Attachment Test ║");
|
|
10
|
-
console.log("╚════════════════════════════════════════════════════════════════╝\n");
|
|
11
|
-
|
|
12
|
-
let passed = 0;
|
|
13
|
-
let failed = 0;
|
|
14
|
-
|
|
15
|
-
// Simulate a context with attach functionality
|
|
16
|
-
class SimulatedContext {
|
|
17
|
-
constructor() {
|
|
18
|
-
this.__trackedEntities = [];
|
|
19
|
-
this.__trackedEntitiesMap = new Map();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
__track(model) {
|
|
23
|
-
if (!model.__ID) {
|
|
24
|
-
model.__ID = Math.floor((Math.random() * 100000) + 1);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (!this.__trackedEntitiesMap.has(model.__ID)) {
|
|
28
|
-
this.__trackedEntities.push(model);
|
|
29
|
-
this.__trackedEntitiesMap.set(model.__ID, model);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return model;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
attach(entity, changes = null) {
|
|
36
|
-
if (!entity) {
|
|
37
|
-
throw new Error('Cannot attach null or undefined entity');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (!entity.__entity || !entity.__entity.__name) {
|
|
41
|
-
throw new Error('Entity must have __entity metadata');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Mark entity as modified
|
|
45
|
-
entity.__state = 'modified';
|
|
46
|
-
|
|
47
|
-
// If specific changes provided, mark only those fields as dirty
|
|
48
|
-
if (changes) {
|
|
49
|
-
entity.__dirtyFields = entity.__dirtyFields || [];
|
|
50
|
-
for (const fieldName in changes) {
|
|
51
|
-
entity[fieldName] = changes[fieldName];
|
|
52
|
-
if (!entity.__dirtyFields.includes(fieldName)) {
|
|
53
|
-
entity.__dirtyFields.push(fieldName);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
// Mark all fields as potentially modified
|
|
58
|
-
entity.__dirtyFields = entity.__dirtyFields || [];
|
|
59
|
-
|
|
60
|
-
if (entity.__dirtyFields.length === 0) {
|
|
61
|
-
for (const fieldName in entity.__entity) {
|
|
62
|
-
if (!fieldName.startsWith('__') &&
|
|
63
|
-
entity.__entity[fieldName].type !== 'hasMany' &&
|
|
64
|
-
entity.__entity[fieldName].type !== 'hasOne') {
|
|
65
|
-
entity.__dirtyFields.push(fieldName);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
entity.__context = this;
|
|
72
|
-
this.__track(entity);
|
|
73
|
-
return entity;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
attachAll(entities) {
|
|
77
|
-
if (!Array.isArray(entities)) {
|
|
78
|
-
throw new Error('attachAll() requires an array');
|
|
79
|
-
}
|
|
80
|
-
return entities.map(entity => this.attach(entity));
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Create mock entity
|
|
85
|
-
function createMockEntity(id, name, status) {
|
|
86
|
-
return {
|
|
87
|
-
__ID: id,
|
|
88
|
-
__entity: {
|
|
89
|
-
__name: 'Task',
|
|
90
|
-
id: { type: 'integer', primary: true },
|
|
91
|
-
name: { type: 'string' },
|
|
92
|
-
status: { type: 'string' }
|
|
93
|
-
},
|
|
94
|
-
__dirtyFields: [],
|
|
95
|
-
__state: 'track',
|
|
96
|
-
id: id,
|
|
97
|
-
name: name,
|
|
98
|
-
status: status
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Test 1: Attach detached entity
|
|
103
|
-
console.log("📝 Test 1: Attach detached entity");
|
|
104
|
-
console.log("──────────────────────────────────────────────────");
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
const ctx = new SimulatedContext();
|
|
108
|
-
const task = createMockEntity(1, 'Task 1', 'pending');
|
|
109
|
-
|
|
110
|
-
// Simulate: entity loaded in different context (detached)
|
|
111
|
-
task.status = 'completed';
|
|
112
|
-
|
|
113
|
-
// Attach to current context
|
|
114
|
-
ctx.attach(task);
|
|
115
|
-
|
|
116
|
-
if (ctx.__trackedEntities.includes(task) &&
|
|
117
|
-
task.__state === 'modified' &&
|
|
118
|
-
task.__dirtyFields.length > 0) {
|
|
119
|
-
console.log(" ✓ Entity attached to context");
|
|
120
|
-
console.log(" ✓ Entity marked as 'modified'");
|
|
121
|
-
console.log(` ✓ Dirty fields marked: ${task.__dirtyFields.join(', ')}`);
|
|
122
|
-
passed++;
|
|
123
|
-
} else {
|
|
124
|
-
console.log(` ✗ Entity not properly attached`);
|
|
125
|
-
failed++;
|
|
126
|
-
}
|
|
127
|
-
} catch(err) {
|
|
128
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
129
|
-
failed++;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Test 2: Attach with specific field changes
|
|
133
|
-
console.log("\n📝 Test 2: Attach with specific field changes");
|
|
134
|
-
console.log("──────────────────────────────────────────────────");
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
const ctx = new SimulatedContext();
|
|
138
|
-
const task = createMockEntity(2, 'Task 2', 'pending');
|
|
139
|
-
|
|
140
|
-
// Attach with specific changes
|
|
141
|
-
ctx.attach(task, {
|
|
142
|
-
status: 'completed',
|
|
143
|
-
completed_at: new Date()
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
if (task.status === 'completed' &&
|
|
147
|
-
task.__dirtyFields.includes('status') &&
|
|
148
|
-
task.__dirtyFields.includes('completed_at') &&
|
|
149
|
-
task.__state === 'modified') {
|
|
150
|
-
console.log(" ✓ Specific fields applied");
|
|
151
|
-
console.log(" ✓ Only specified fields marked dirty");
|
|
152
|
-
console.log(` ✓ Dirty fields: ${task.__dirtyFields.join(', ')}`);
|
|
153
|
-
passed++;
|
|
154
|
-
} else {
|
|
155
|
-
console.log(` ✗ Specific changes not applied correctly`);
|
|
156
|
-
failed++;
|
|
157
|
-
}
|
|
158
|
-
} catch(err) {
|
|
159
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
160
|
-
failed++;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Test 3: attachAll() multiple entities
|
|
164
|
-
console.log("\n📝 Test 3: Attach multiple entities");
|
|
165
|
-
console.log("──────────────────────────────────────────────────");
|
|
166
|
-
|
|
167
|
-
try {
|
|
168
|
-
const ctx = new SimulatedContext();
|
|
169
|
-
const tasks = [
|
|
170
|
-
createMockEntity(3, 'Task 3', 'pending'),
|
|
171
|
-
createMockEntity(4, 'Task 4', 'pending'),
|
|
172
|
-
createMockEntity(5, 'Task 5', 'pending')
|
|
173
|
-
];
|
|
174
|
-
|
|
175
|
-
// Modify all
|
|
176
|
-
tasks.forEach(t => t.status = 'completed');
|
|
177
|
-
|
|
178
|
-
// Attach all
|
|
179
|
-
ctx.attachAll(tasks);
|
|
180
|
-
|
|
181
|
-
const allAttached = tasks.every(t =>
|
|
182
|
-
ctx.__trackedEntities.includes(t) &&
|
|
183
|
-
t.__state === 'modified'
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
if (allAttached && ctx.__trackedEntities.length === 3) {
|
|
187
|
-
console.log(" ✓ All entities attached");
|
|
188
|
-
console.log(` ✓ Tracked count: ${ctx.__trackedEntities.length}`);
|
|
189
|
-
console.log(" ✓ All marked as modified");
|
|
190
|
-
passed++;
|
|
191
|
-
} else {
|
|
192
|
-
console.log(` ✗ Not all entities attached correctly`);
|
|
193
|
-
failed++;
|
|
194
|
-
}
|
|
195
|
-
} catch(err) {
|
|
196
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
197
|
-
failed++;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Test 4: Attach throws error for invalid entity
|
|
201
|
-
console.log("\n📝 Test 4: Error handling for invalid entities");
|
|
202
|
-
console.log("──────────────────────────────────────────────────");
|
|
203
|
-
|
|
204
|
-
try {
|
|
205
|
-
const ctx = new SimulatedContext();
|
|
206
|
-
|
|
207
|
-
let error1 = null;
|
|
208
|
-
let error2 = null;
|
|
209
|
-
|
|
210
|
-
// Test null
|
|
211
|
-
try {
|
|
212
|
-
ctx.attach(null);
|
|
213
|
-
} catch(e) {
|
|
214
|
-
error1 = e.message;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Test entity without metadata
|
|
218
|
-
try {
|
|
219
|
-
ctx.attach({ id: 1, name: 'Test' });
|
|
220
|
-
} catch(e) {
|
|
221
|
-
error2 = e.message;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (error1 && error2) {
|
|
225
|
-
console.log(" ✓ Null entity rejected");
|
|
226
|
-
console.log(" ✓ Entity without metadata rejected");
|
|
227
|
-
console.log(` ✓ Error messages provided`);
|
|
228
|
-
passed++;
|
|
229
|
-
} else {
|
|
230
|
-
console.log(` ✗ Invalid entities should throw errors`);
|
|
231
|
-
failed++;
|
|
232
|
-
}
|
|
233
|
-
} catch(err) {
|
|
234
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
235
|
-
failed++;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Test 5: Attach doesn't duplicate entities
|
|
239
|
-
console.log("\n📝 Test 5: No duplicate tracking");
|
|
240
|
-
console.log("──────────────────────────────────────────────────");
|
|
241
|
-
|
|
242
|
-
try {
|
|
243
|
-
const ctx = new SimulatedContext();
|
|
244
|
-
const task = createMockEntity(6, 'Task 6', 'pending');
|
|
245
|
-
|
|
246
|
-
// Attach twice
|
|
247
|
-
ctx.attach(task);
|
|
248
|
-
ctx.attach(task);
|
|
249
|
-
|
|
250
|
-
if (ctx.__trackedEntities.length === 1) {
|
|
251
|
-
console.log(" ✓ Entity not duplicated in tracking");
|
|
252
|
-
console.log(` ✓ Tracked count: ${ctx.__trackedEntities.length}`);
|
|
253
|
-
passed++;
|
|
254
|
-
} else {
|
|
255
|
-
console.log(` ✗ Entity duplicated: ${ctx.__trackedEntities.length} entries`);
|
|
256
|
-
failed++;
|
|
257
|
-
}
|
|
258
|
-
} catch(err) {
|
|
259
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
260
|
-
failed++;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Test 6: Attach preserves entity reference
|
|
264
|
-
console.log("\n📝 Test 6: Entity reference preserved");
|
|
265
|
-
console.log("──────────────────────────────────────────────────");
|
|
266
|
-
|
|
267
|
-
try {
|
|
268
|
-
const ctx = new SimulatedContext();
|
|
269
|
-
const task = createMockEntity(7, 'Task 7', 'pending');
|
|
270
|
-
|
|
271
|
-
const returned = ctx.attach(task);
|
|
272
|
-
|
|
273
|
-
if (returned === task) {
|
|
274
|
-
console.log(" ✓ Same entity reference returned");
|
|
275
|
-
console.log(" ✓ No entity cloning");
|
|
276
|
-
passed++;
|
|
277
|
-
} else {
|
|
278
|
-
console.log(` ✗ Different entity reference returned`);
|
|
279
|
-
failed++;
|
|
280
|
-
}
|
|
281
|
-
} catch(err) {
|
|
282
|
-
console.log(` ✗ Error: ${err.message}`);
|
|
283
|
-
failed++;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// Summary
|
|
287
|
-
console.log("\n╔════════════════════════════════════════════════════════════════╗");
|
|
288
|
-
console.log("║ Test Summary ║");
|
|
289
|
-
console.log("╚════════════════════════════════════════════════════════════════╝");
|
|
290
|
-
console.log(`\n ✓ Passed: ${passed}`);
|
|
291
|
-
console.log(` ✗ Failed: ${failed}`);
|
|
292
|
-
console.log(` 📊 Total: ${passed + failed}\n`);
|
|
293
|
-
|
|
294
|
-
if(failed === 0) {
|
|
295
|
-
console.log(" 🎉 All tests passed!\n");
|
|
296
|
-
console.log(" ✅ Detached entity attachment works");
|
|
297
|
-
console.log(" ✅ Like Entity Framework's context.Update()");
|
|
298
|
-
console.log(" ✅ Like Hibernate's session.merge()\n");
|
|
299
|
-
process.exit(0);
|
|
300
|
-
} else {
|
|
301
|
-
console.log(" ❌ Some tests failed\n");
|
|
302
|
-
process.exit(1);
|
|
303
|
-
}
|