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.
Files changed (97) hide show
  1. package/.claude/settings.local.json +3 -1
  2. package/CHANGES.md +31 -2
  3. package/Cache/QueryCache.js +10 -4
  4. package/Cache/RedisQueryCache.js +3 -2
  5. package/Entity/entityModel.js +1 -1
  6. package/Entity/entityModelBuilder.js +2 -2
  7. package/Entity/entityTrackerModel.js +3 -3
  8. package/Entity/fieldTransformer.js +1 -1
  9. package/FOREIGN_KEY_STRING_FIX.md +2 -2
  10. package/MasterRecord.js +6 -43
  11. package/Migrations/cli.js +44 -113
  12. package/Migrations/dependencyGraph.js +1 -1
  13. package/Migrations/migrationMySQLQuery.js +1 -1
  14. package/Migrations/migrationPostgresQuery.js +1 -1
  15. package/Migrations/migrationSQLiteQuery.js +1 -1
  16. package/Migrations/migrationTemplate.js +8 -39
  17. package/Migrations/migrations.js +16 -14
  18. package/Migrations/pathUtils.js +2 -5
  19. package/Migrations/pathUtils.test.js +33 -49
  20. package/Migrations/schema.js +52 -89
  21. package/QueryLanguage/queryMethods.js +4 -4
  22. package/QueryLanguage/queryParameters.js +1 -1
  23. package/QueryLanguage/queryScript.js +4 -3
  24. package/SQLLiteEngine.js +26 -7
  25. package/Tools.js +10 -8
  26. package/context.js +43 -42
  27. package/deleteManager.js +2 -2
  28. package/docs/MIGRATIONS_GUIDE.md +237 -532
  29. package/docs/POSTGRESQL_SETUP.md +4 -4
  30. package/docs/QUERY_CACHING_GUIDE.md +8 -7
  31. package/eslint.config.js +40 -0
  32. package/insertManager.js +28 -20
  33. package/mySQLConnect.js +2 -2
  34. package/mySQLEngine.js +26 -6
  35. package/package.json +34 -5
  36. package/postgresEngine.js +29 -8
  37. package/postgresSyncConnect.js +5 -3
  38. package/prettier.config.js +22 -0
  39. package/readme.md +35 -27
  40. package/test/context-errors.test.js +43 -0
  41. package/test/esm-imports.test.js +110 -0
  42. package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
  43. package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
  44. package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
  45. package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
  46. package/test/migration-template.test.js +47 -0
  47. package/test/new-setter-large-string.test.js +191 -0
  48. package/test/pool-key.test.js +43 -0
  49. package/test/sqlite-lifecycle.test.js +76 -0
  50. package/.eslintrc.js +0 -290
  51. package/.prettierrc.js +0 -109
  52. package/test/anyCommaStringTest.js +0 -237
  53. package/test/anyMethodTest.js +0 -176
  54. package/test/attachDetached.test.js +0 -303
  55. package/test/bulk-operations-test.js +0 -235
  56. package/test/cache-toObject-test.js +0 -105
  57. package/test/cacheIntegration.test.js +0 -319
  58. package/test/config-glob-pattern-test.js +0 -232
  59. package/test/double-where-bug-test.js +0 -71
  60. package/test/ensure-ready-test.js +0 -251
  61. package/test/entity-deduplication-test.js +0 -174
  62. package/test/entity-methods-test.js +0 -269
  63. package/test/findByIdTest.js +0 -227
  64. package/test/foreign-key-string-value-test.js +0 -406
  65. package/test/global-model-registry-test.js +0 -538
  66. package/test/id-setting-validation.js +0 -202
  67. package/test/includesFeatureTest.js +0 -183
  68. package/test/includesTransformTest.js +0 -110
  69. package/test/index-bug-fix-test.js +0 -188
  70. package/test/insert-return-test.js +0 -39
  71. package/test/lifecycle-hooks-test.js +0 -258
  72. package/test/multiContextCache.test.js +0 -230
  73. package/test/new-entity-migration-test.js +0 -162
  74. package/test/newMethodTest.js +0 -330
  75. package/test/newMethodUnitTest.js +0 -320
  76. package/test/optInCache.test.js +0 -221
  77. package/test/parameterizedPlaceholderTest.js +0 -159
  78. package/test/pool-registry-test.js +0 -303
  79. package/test/post-insert-tracking-test.js +0 -258
  80. package/test/postgresEngineTest.js +0 -463
  81. package/test/postgresIntegrationTest.js +0 -381
  82. package/test/promise-detection-test.js +0 -290
  83. package/test/qa-context-pattern-test.js +0 -319
  84. package/test/query-helpers-test.js +0 -258
  85. package/test/query-isolation-test.js +0 -59
  86. package/test/queryCache.test.js +0 -148
  87. package/test/securityTest.js +0 -268
  88. package/test/seed-data-test.js +0 -212
  89. package/test/seed-deduplication-test.js +0 -313
  90. package/test/seed-features-integration-test.js +0 -418
  91. package/test/seed-migration-template-test.js +0 -220
  92. package/test/singleDollarPlaceholderTest.js +0 -238
  93. package/test/tablePrefixTest.js +0 -100
  94. package/test/transformerTest.js +0 -287
  95. package/test/v0.3.34-bug-fixes-test.js +0 -428
  96. package/test/validation-test.js +0 -302
  97. package/test/whereChainingTest.js +0 -88
@@ -1,302 +0,0 @@
1
- /**
2
- * Test: Business Logic Validation
3
- * Tests: required, email, minLength, maxLength, pattern, min, max, custom
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("║ Business Logic Validation Test ║");
12
- console.log("╚════════════════════════════════════════════════════════════════╝\n");
13
-
14
- let passed = 0;
15
- let failed = 0;
16
-
17
- class User {
18
- id(db) {
19
- db.integer().primary().auto();
20
- }
21
- name(db) {
22
- db.string().required('Name is required').minLength(3, 'Name must be at least 3 characters');
23
- }
24
- email(db) {
25
- db.string().required().email('Invalid email format');
26
- }
27
- password(db) {
28
- db.string().minLength(8, 'Password must be at least 8 characters').maxLength(100);
29
- }
30
- username(db) {
31
- db.string().pattern(/^[a-zA-Z0-9_]+$/, 'Username can only contain letters, numbers, and underscores');
32
- }
33
- age(db) {
34
- db.integer().min(18, 'Must be at least 18 years old').max(120, 'Age cannot exceed 120');
35
- }
36
- status(db) {
37
- db.string().custom((value) => {
38
- return ['active', 'inactive', 'pending'].includes(value);
39
- }, 'Status must be active, inactive, or pending');
40
- }
41
- }
42
-
43
- class TestContext extends masterrecord.context {
44
- constructor() {
45
- super();
46
- }
47
- onConfig(db) {
48
- this.dbset(User);
49
- }
50
- }
51
-
52
- const dbPath = path.join(__dirname, '..', 'database', 'validation.db');
53
- if (fs.existsSync(dbPath)) {
54
- fs.unlinkSync(dbPath);
55
- }
56
-
57
- async function runTests() {
58
- const db = new TestContext();
59
-
60
- // Initialize SQLite database
61
- const SQLLiteEngine = require('../SQLLiteEngine');
62
- const sqlite3 = require('better-sqlite3');
63
- db.isSQLite = true;
64
- db.isMySQL = false;
65
- db.isPostgres = false;
66
- db._SQLEngine = new SQLLiteEngine();
67
- db.db = new sqlite3(dbPath);
68
- db._SQLEngine.setDB(db.db, 'better-sqlite3');
69
-
70
- db.onConfig();
71
-
72
- // Create table
73
- db.db.exec('CREATE TABLE IF NOT EXISTS User (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, password TEXT, username TEXT, age INTEGER, status TEXT)');
74
-
75
- // Test 1: required() validation
76
- console.log("📝 Test 1: required() validation");
77
- console.log("──────────────────────────────────────────────────");
78
- try {
79
- const user = db.User.new();
80
- try {
81
- user.name = '';
82
- console.log(` ✗ Should have thrown validation error`);
83
- failed++;
84
- } catch (validationError) {
85
- if (validationError.message.includes('Name is required')) {
86
- console.log(" ✓ required() prevents empty values");
87
- passed++;
88
- } else {
89
- console.log(` ✗ Wrong error: ${validationError.message}`);
90
- failed++;
91
- }
92
- }
93
- } catch(err) {
94
- console.log(` ✗ Error: ${err.message}`);
95
- failed++;
96
- }
97
-
98
- // Test 2: email() validation
99
- console.log("\n📝 Test 2: email() validation");
100
- console.log("──────────────────────────────────────────────────");
101
- try {
102
- const user = db.User.new();
103
- try {
104
- user.email = 'not-an-email';
105
- console.log(` ✗ Should have thrown validation error`);
106
- failed++;
107
- } catch (validationError) {
108
- if (validationError.message.includes('Invalid email format')) {
109
- console.log(" ✓ email() rejects invalid format");
110
- passed++;
111
- } else {
112
- console.log(` ✗ Wrong error: ${validationError.message}`);
113
- failed++;
114
- }
115
- }
116
- } catch(err) {
117
- console.log(` ✗ Error: ${err.message}`);
118
- failed++;
119
- }
120
-
121
- // Test 3: minLength() validation
122
- console.log("\n📝 Test 3: minLength() validation");
123
- console.log("──────────────────────────────────────────────────");
124
- try {
125
- const user = db.User.new();
126
- try {
127
- user.name = 'AB'; // Too short
128
- console.log(` ✗ Should have thrown validation error`);
129
- failed++;
130
- } catch (validationError) {
131
- if (validationError.message.includes('at least 3 characters')) {
132
- console.log(" ✓ minLength() enforces minimum length");
133
- passed++;
134
- } else {
135
- console.log(` ✗ Wrong error: ${validationError.message}`);
136
- failed++;
137
- }
138
- }
139
- } catch(err) {
140
- console.log(` ✗ Error: ${err.message}`);
141
- failed++;
142
- }
143
-
144
- // Test 4: maxLength() validation
145
- console.log("\n📝 Test 4: maxLength() validation");
146
- console.log("──────────────────────────────────────────────────");
147
- try {
148
- const user = db.User.new();
149
- try {
150
- user.password = 'a'.repeat(101); // Too long
151
- console.log(` ✗ Should have thrown validation error`);
152
- failed++;
153
- } catch (validationError) {
154
- if (validationError.message.includes('at most 100 characters')) {
155
- console.log(" ✓ maxLength() enforces maximum length");
156
- passed++;
157
- } else {
158
- console.log(` ✗ Wrong error: ${validationError.message}`);
159
- failed++;
160
- }
161
- }
162
- } catch(err) {
163
- console.log(` ✗ Error: ${err.message}`);
164
- failed++;
165
- }
166
-
167
- // Test 5: pattern() validation
168
- console.log("\n📝 Test 5: pattern() validation");
169
- console.log("──────────────────────────────────────────────────");
170
- try {
171
- const user = db.User.new();
172
- try {
173
- user.username = 'user@name'; // Invalid character @
174
- console.log(` ✗ Should have thrown validation error`);
175
- failed++;
176
- } catch (validationError) {
177
- if (validationError.message.includes('letters, numbers, and underscores')) {
178
- console.log(" ✓ pattern() enforces regex pattern");
179
- passed++;
180
- } else {
181
- console.log(` ✗ Wrong error: ${validationError.message}`);
182
- failed++;
183
- }
184
- }
185
- } catch(err) {
186
- console.log(` ✗ Error: ${err.message}`);
187
- failed++;
188
- }
189
-
190
- // Test 6: min() validation for numbers
191
- console.log("\n📝 Test 6: min() validation for numbers");
192
- console.log("──────────────────────────────────────────────────");
193
- try {
194
- const user = db.User.new();
195
- try {
196
- user.age = 17; // Too young
197
- console.log(` ✗ Should have thrown validation error`);
198
- failed++;
199
- } catch (validationError) {
200
- if (validationError.message.includes('at least 18')) {
201
- console.log(" ✓ min() enforces minimum value");
202
- passed++;
203
- } else {
204
- console.log(` ✗ Wrong error: ${validationError.message}`);
205
- failed++;
206
- }
207
- }
208
- } catch(err) {
209
- console.log(` ✗ Error: ${err.message}`);
210
- failed++;
211
- }
212
-
213
- // Test 7: max() validation for numbers
214
- console.log("\n📝 Test 7: max() validation for numbers");
215
- console.log("──────────────────────────────────────────────────");
216
- try {
217
- const user = db.User.new();
218
- try {
219
- user.age = 121; // Too old
220
- console.log(` ✗ Should have thrown validation error`);
221
- failed++;
222
- } catch (validationError) {
223
- if (validationError.message.includes('cannot exceed 120')) {
224
- console.log(" ✓ max() enforces maximum value");
225
- passed++;
226
- } else {
227
- console.log(` ✗ Wrong error: ${validationError.message}`);
228
- failed++;
229
- }
230
- }
231
- } catch(err) {
232
- console.log(` ✗ Error: ${err.message}`);
233
- failed++;
234
- }
235
-
236
- // Test 8: custom() validation
237
- console.log("\n📝 Test 8: custom() validation");
238
- console.log("──────────────────────────────────────────────────");
239
- try {
240
- const user = db.User.new();
241
- try {
242
- user.status = 'invalid-status';
243
- console.log(` ✗ Should have thrown validation error`);
244
- failed++;
245
- } catch (validationError) {
246
- if (validationError.message.includes('active, inactive, or pending')) {
247
- console.log(" ✓ custom() executes custom validation function");
248
- passed++;
249
- } else {
250
- console.log(` ✗ Wrong error: ${validationError.message}`);
251
- failed++;
252
- }
253
- }
254
- } catch(err) {
255
- console.log(` ✗ Error: ${err.message}`);
256
- failed++;
257
- }
258
-
259
- // Test 9: Valid data passes all validations
260
- console.log("\n📝 Test 9: Valid data passes all validations");
261
- console.log("──────────────────────────────────────────────────");
262
- try {
263
- // Clear tracked entities from previous tests (they have invalid/missing values)
264
- db.__trackedEntities = [];
265
- db.__trackedEntitiesMap = new Map();
266
-
267
- const user = db.User.new();
268
- user.name = 'John Doe';
269
- user.email = 'john@example.com';
270
- user.password = 'secure-password-123';
271
- user.username = 'john_doe_123';
272
- user.age = 25;
273
- user.status = 'active';
274
- await user.save();
275
-
276
- if (user.id) {
277
- console.log(" ✓ Valid data passes all validations");
278
- console.log(` ✓ Entity saved with ID: ${user.id}`);
279
- passed++;
280
- } else {
281
- console.log(` ✗ Entity not saved`);
282
- failed++;
283
- }
284
- } catch(err) {
285
- console.log(` ✗ Error: ${err.message}`);
286
- failed++;
287
- }
288
-
289
- // Summary
290
- console.log("\n" + "=".repeat(64));
291
- console.log(`Test Results: ${passed} passed, ${failed} failed`);
292
- console.log("=".repeat(64));
293
-
294
- if (failed > 0) {
295
- process.exit(1);
296
- }
297
- }
298
-
299
- runTests().catch(err => {
300
- console.error('Fatal error:', err);
301
- process.exit(1);
302
- });
@@ -1,88 +0,0 @@
1
- // Test for where() chaining functionality
2
- // This test verifies that multiple .where() calls combine properly with AND
3
-
4
- var queryScript = require('../QueryLanguage/queryScript');
5
-
6
- console.log('=== MasterRecord where() Chaining Test ===\n');
7
-
8
- // Simulate what happens in the query builder
9
- const qs = new queryScript();
10
-
11
- // Test Case: Two chained where() calls (like the user's example)
12
- // let query = this._qaContext.QaTask;
13
- // query = query.where(t => t.assigned_worker_id == $$, this._currentUser.id);
14
- // query = query.where(t => t.status == $$, status);
15
-
16
- console.log('Test: Chaining two where() calls');
17
- console.log(' First: where(t => t.assigned_worker_id == 123)');
18
- console.log(' Second: where(t => t.status == "pending")');
19
- console.log();
20
-
21
- // First where call
22
- qs.where('t => t.assigned_worker_id == 123', 'QaTask');
23
-
24
- console.log('After first where():');
25
- console.log(' script.where exists:', qs.script.where !== false);
26
- console.log(' script.where.QaTask exists:', qs.script.where && qs.script.where.QaTask !== undefined);
27
- if (qs.script.where && qs.script.where.QaTask && qs.script.where.QaTask.query) {
28
- const exprs1 = qs.script.where.QaTask.query.expressions || [];
29
- console.log(' Expressions count:', exprs1.length);
30
- console.log(' Expression 1:', JSON.stringify(exprs1[0]));
31
- }
32
- console.log();
33
-
34
- // Second where call (this should MERGE, not overwrite)
35
- qs.where('t => t.status == "pending"', 'QaTask');
36
-
37
- console.log('After second where():');
38
- console.log(' script.where exists:', qs.script.where !== false);
39
- console.log(' script.where.QaTask exists:', qs.script.where && qs.script.where.QaTask !== undefined);
40
-
41
- if (qs.script.where && qs.script.where.QaTask && qs.script.where.QaTask.query) {
42
- const exprs2 = qs.script.where.QaTask.query.expressions || [];
43
- console.log(' Expressions count:', exprs2.length);
44
- console.log(' Expression 1:', JSON.stringify(exprs2[0]));
45
- console.log(' Expression 2:', JSON.stringify(exprs2[1]));
46
- console.log();
47
-
48
- // Verify results
49
- console.log('=== Test Results ===');
50
- if (exprs2.length === 2) {
51
- console.log('✓ PASS: Both where conditions are present');
52
- console.log(' - First condition: assigned_worker_id == 123');
53
- console.log(' - Second condition: status == "pending"');
54
- console.log(' - These should be combined with AND in the SQL');
55
- } else {
56
- console.log('✗ FAIL: Expected 2 expressions, got', exprs2.length);
57
- if (exprs2.length === 1) {
58
- console.log(' - Only the last where() was applied (bug not fixed)');
59
- }
60
- }
61
- } else {
62
- console.log('✗ FAIL: script.where structure is invalid');
63
- }
64
-
65
- console.log();
66
- console.log('=== Additional Test: Three where() calls ===');
67
-
68
- // Test with three chained where calls
69
- const qs2 = new queryScript();
70
- qs2.where('t => t.user_id == 1', 'Task');
71
- qs2.where('t => t.status == "active"', 'Task');
72
- qs2.where('t => t.priority == "high"', 'Task');
73
-
74
- if (qs2.script.where && qs2.script.where.Task && qs2.script.where.Task.query) {
75
- const exprs3 = qs2.script.where.Task.query.expressions || [];
76
- console.log('Expressions count:', exprs3.length);
77
- if (exprs3.length === 3) {
78
- console.log('✓ PASS: All three where conditions are present');
79
- exprs3.forEach((expr, idx) => {
80
- console.log(` ${idx + 1}. ${expr.field} ${expr.func} ${expr.arg}`);
81
- });
82
- } else {
83
- console.log('✗ FAIL: Expected 3 expressions, got', exprs3.length);
84
- }
85
- }
86
-
87
- console.log();
88
- console.log('=== Test Complete ===');