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.
Files changed (103) 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 +30 -8
  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 +161 -16
  35. package/package.json +34 -5
  36. package/postgresEngine.js +74 -26
  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/count-chained-where.test.js +136 -0
  42. package/test/esm-imports.test.js +110 -0
  43. package/test/fixtures/count-chained-where/config/environments/env.countchained.json +1 -0
  44. package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
  45. package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
  46. package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
  47. package/test/fixtures/query-builder-parity/config/environments/env.parity.json +1 -0
  48. package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
  49. package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
  50. package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
  51. package/test/migration-template.test.js +47 -0
  52. package/test/new-setter-large-string.test.js +191 -0
  53. package/test/pool-key.test.js +43 -0
  54. package/test/query-builder-parity.test.js +260 -0
  55. package/test/sqlite-lifecycle.test.js +76 -0
  56. package/.eslintrc.js +0 -290
  57. package/.prettierrc.js +0 -109
  58. package/test/anyCommaStringTest.js +0 -237
  59. package/test/anyMethodTest.js +0 -176
  60. package/test/attachDetached.test.js +0 -303
  61. package/test/bulk-operations-test.js +0 -235
  62. package/test/cache-toObject-test.js +0 -105
  63. package/test/cacheIntegration.test.js +0 -319
  64. package/test/config-glob-pattern-test.js +0 -232
  65. package/test/double-where-bug-test.js +0 -71
  66. package/test/ensure-ready-test.js +0 -251
  67. package/test/entity-deduplication-test.js +0 -174
  68. package/test/entity-methods-test.js +0 -269
  69. package/test/findByIdTest.js +0 -227
  70. package/test/foreign-key-string-value-test.js +0 -406
  71. package/test/global-model-registry-test.js +0 -538
  72. package/test/id-setting-validation.js +0 -202
  73. package/test/includesFeatureTest.js +0 -183
  74. package/test/includesTransformTest.js +0 -110
  75. package/test/index-bug-fix-test.js +0 -188
  76. package/test/insert-return-test.js +0 -39
  77. package/test/lifecycle-hooks-test.js +0 -258
  78. package/test/multiContextCache.test.js +0 -230
  79. package/test/new-entity-migration-test.js +0 -162
  80. package/test/newMethodTest.js +0 -330
  81. package/test/newMethodUnitTest.js +0 -320
  82. package/test/optInCache.test.js +0 -221
  83. package/test/parameterizedPlaceholderTest.js +0 -159
  84. package/test/pool-registry-test.js +0 -303
  85. package/test/post-insert-tracking-test.js +0 -258
  86. package/test/postgresEngineTest.js +0 -463
  87. package/test/postgresIntegrationTest.js +0 -381
  88. package/test/promise-detection-test.js +0 -290
  89. package/test/qa-context-pattern-test.js +0 -319
  90. package/test/query-helpers-test.js +0 -258
  91. package/test/query-isolation-test.js +0 -59
  92. package/test/queryCache.test.js +0 -148
  93. package/test/securityTest.js +0 -268
  94. package/test/seed-data-test.js +0 -212
  95. package/test/seed-deduplication-test.js +0 -313
  96. package/test/seed-features-integration-test.js +0 -418
  97. package/test/seed-migration-template-test.js +0 -220
  98. package/test/singleDollarPlaceholderTest.js +0 -238
  99. package/test/tablePrefixTest.js +0 -100
  100. package/test/transformerTest.js +0 -287
  101. package/test/v0.3.34-bug-fixes-test.js +0 -428
  102. package/test/validation-test.js +0 -302
  103. package/test/whereChainingTest.js +0 -88
@@ -1,428 +0,0 @@
1
- /**
2
- * Comprehensive Tests for v0.3.34 Bug Fixes
3
- *
4
- * Tests:
5
- * 1. Whitelist validation in query builders
6
- * 2. No duplicate index creation
7
- * 3. Seed API migration generation (no duplicates, correct API usage)
8
- * 4. All async operations have await keywords
9
- */
10
-
11
- console.log("╔════════════════════════════════════════════════════════════════╗");
12
- console.log("ā•‘ MasterRecord v0.3.34 Bug Fixes Test ā•‘");
13
- console.log("ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•\n");
14
-
15
- const Migrations = require('../Migrations/migrations');
16
- const MigrationSQLiteQuery = require('../Migrations/migrationSQLiteQuery');
17
- const MigrationMySQLQuery = require('../Migrations/migrationMySQLQuery');
18
- const MigrationPostgresQuery = require('../Migrations/migrationPostgresQuery');
19
-
20
- let passed = 0;
21
- let failed = 0;
22
-
23
- function assert(condition, message) {
24
- if (!condition) {
25
- console.log(`āŒ FAIL: ${message}`);
26
- failed++;
27
- return false;
28
- }
29
- return true;
30
- }
31
-
32
- function test(description, fn) {
33
- try {
34
- fn();
35
- console.log(`āœ“ ${description}`);
36
- passed++;
37
- } catch (e) {
38
- console.log(`āŒ FAIL: ${description}`);
39
- console.log(` Error: ${e.message}`);
40
- failed++;
41
- }
42
- }
43
-
44
- // =============================================================================
45
- // Test Suite 1: Whitelist Validation
46
- // =============================================================================
47
- console.log("\nšŸ“‹ Test Suite 1: Whitelist Validation in Query Builders\n");
48
-
49
- test('SQLite query builder skips objects without name property', () => {
50
- const queryBuilder = new MigrationSQLiteQuery();
51
- const table = {
52
- __name: 'TestTable',
53
- id: { name: 'id', type: 'integer', primaryKey: true },
54
- weirdMetadata: { foo: 'bar' }, // No name or type - should be skipped
55
- name: { name: 'name', type: 'string' }
56
- };
57
-
58
- const query = queryBuilder.createTable(table);
59
-
60
- assert(!query.includes('undefined'), 'SQLite: Should not contain undefined');
61
- assert(query.includes('id'), 'SQLite: Should contain id column');
62
- assert(query.includes('name'), 'SQLite: Should contain name column');
63
- assert(!query.includes('foo'), 'SQLite: Should not process metadata without name/type');
64
- });
65
-
66
- test('MySQL query builder skips objects without name property', () => {
67
- const queryBuilder = new MigrationMySQLQuery();
68
- const table = {
69
- __name: 'TestTable',
70
- id: { name: 'id', type: 'integer', primaryKey: true },
71
- weirdMetadata: { foo: 'bar' },
72
- name: { name: 'name', type: 'string' }
73
- };
74
-
75
- const query = queryBuilder.createTable(table);
76
-
77
- assert(!query.includes('undefined'), 'MySQL: Should not contain undefined');
78
- assert(query.includes('id'), 'MySQL: Should contain id column');
79
- assert(query.includes('name'), 'MySQL: Should contain name column');
80
- });
81
-
82
- test('PostgreSQL query builder skips objects without name property', () => {
83
- const queryBuilder = new MigrationPostgresQuery();
84
- const table = {
85
- __name: 'TestTable',
86
- id: { name: 'id', type: 'integer', primaryKey: true },
87
- weirdMetadata: { foo: 'bar' },
88
- name: { name: 'name', type: 'string' }
89
- };
90
-
91
- const query = queryBuilder.createTable(table);
92
-
93
- assert(!query.includes('undefined'), 'PostgreSQL: Should not contain undefined');
94
- assert(query.includes('id'), 'PostgreSQL: Should contain id column');
95
- assert(query.includes('name'), 'PostgreSQL: Should contain name column');
96
- });
97
-
98
- test('Query builders correctly skip indexes property', () => {
99
- const queryBuilder = new MigrationSQLiteQuery();
100
- const table = {
101
- __name: 'TestTable',
102
- id: { name: 'id', type: 'integer', primaryKey: true },
103
- email: { name: 'email', type: 'string', indexes: ['idx_email'] },
104
- indexes: ['some', 'array'] // This is metadata, should be skipped
105
- };
106
-
107
- const query = queryBuilder.createTable(table);
108
-
109
- assert(!query.includes('undefined'), 'Should not contain undefined');
110
- assert(query.includes('email'), 'Should contain email column');
111
- assert(!query.match(/some.*NOT NULL/), 'Should not process indexes array as column');
112
- });
113
-
114
- // =============================================================================
115
- // Test Suite 2: Index Creation with Await
116
- // =============================================================================
117
- console.log("\nšŸ“‹ Test Suite 2: Index Creation with Await Keywords\n");
118
-
119
- test('New tables do NOT generate separate createIndex calls (createTable handles them)', () => {
120
- const migrations = new Migrations();
121
- const oldSchema = [];
122
- const newSchema = [{
123
- __name: 'User',
124
- id: { name: 'id', type: 'integer', primaryKey: true },
125
- email: { name: 'email', type: 'text', indexes: ['idx_user_email'] }
126
- }];
127
-
128
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema);
129
-
130
- // For new tables, createTable() in schema.js handles indexes - no separate calls needed
131
- const createIndexMatches = migrationCode.match(/await this\.createIndex/g);
132
- assert(!createIndexMatches, 'New tables should NOT have separate createIndex calls');
133
- });
134
-
135
- test('New tables do NOT generate separate createCompositeIndex calls (createTable handles them)', () => {
136
- const migrations = new Migrations();
137
- const oldSchema = [];
138
- const newSchema = [{
139
- __name: 'User',
140
- id: { name: 'id', type: 'integer' },
141
- __compositeIndexes: [{
142
- name: 'idx_composite',
143
- columns: ['col1', 'col2'],
144
- unique: false
145
- }]
146
- }];
147
-
148
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema);
149
-
150
- // For new tables, createTable() in schema.js handles composite indexes
151
- const compositeMatches = migrationCode.match(/await this\.createCompositeIndex/g);
152
- assert(!compositeMatches, 'New tables should NOT have separate createCompositeIndex calls');
153
- });
154
-
155
- test('Adding index to EXISTING table generates createIndex with await', () => {
156
- const migrations = new Migrations();
157
- const oldSchema = [{
158
- __name: 'User',
159
- id: { name: 'id', type: 'integer', primaryKey: true },
160
- email: { name: 'email', type: 'text' }
161
- }];
162
- const newSchema = [{
163
- __name: 'User',
164
- id: { name: 'id', type: 'integer', primaryKey: true },
165
- email: { name: 'email', type: 'text', indexes: ['idx_user_email'] }
166
- }];
167
-
168
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema);
169
-
170
- const createIndexMatches = migrationCode.match(/await this\.createIndex/g);
171
- assert(createIndexMatches && createIndexMatches.length > 0, 'Existing table with new index should have createIndex with await');
172
- });
173
-
174
- test('Adding composite index to EXISTING table generates createCompositeIndex with await', () => {
175
- const migrations = new Migrations();
176
- const oldSchema = [{
177
- __name: 'User',
178
- id: { name: 'id', type: 'integer', primaryKey: true }
179
- }];
180
- const newSchema = [{
181
- __name: 'User',
182
- id: { name: 'id', type: 'integer', primaryKey: true },
183
- __compositeIndexes: [{
184
- name: 'idx_composite',
185
- columns: ['col1', 'col2'],
186
- unique: false
187
- }]
188
- }];
189
-
190
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema);
191
-
192
- const compositeMatches = migrationCode.match(/await this\.createCompositeIndex/g);
193
- assert(compositeMatches && compositeMatches.length > 0, 'Existing table with new composite index should have createCompositeIndex with await');
194
- });
195
-
196
- // =============================================================================
197
- // Test Suite 3: Seed API Migration Generation
198
- // =============================================================================
199
- console.log("\nšŸ“‹ Test Suite 3: Seed API Migration Generation\n");
200
-
201
- test('Seed data uses correct API (this.seed instead of table.EntityName.create)', () => {
202
- const migrations = new Migrations();
203
- const oldSchema = [];
204
- const newSchema = [{
205
- __name: 'Settings',
206
- id: { name: 'id', type: 'integer', primaryKey: true },
207
- disable_rag: { name: 'disable_rag', type: 'integer' }
208
- }];
209
- const seedData = {
210
- Settings: [{ disable_rag: 0 }]
211
- };
212
-
213
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema, seedData);
214
-
215
- assert(migrationCode.includes("this.seed('Settings'"), 'Should use this.seed() method');
216
- assert(!migrationCode.includes('table.Settings.create'), 'Should NOT use table.Settings.create()');
217
- });
218
-
219
- test('No duplicate createTable calls for tables with seed data', () => {
220
- const migrations = new Migrations();
221
- const oldSchema = [];
222
- const newSchema = [
223
- {
224
- __name: 'Document',
225
- id: { name: 'id', type: 'integer', primaryKey: true }
226
- },
227
- {
228
- __name: 'Settings',
229
- id: { name: 'id', type: 'integer', primaryKey: true }
230
- }
231
- ];
232
- const seedData = {
233
- Settings: [{ id: 1 }]
234
- };
235
-
236
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema, seedData);
237
-
238
- const settingsCreateMatches = migrationCode.match(/createTable\(table\.Settings\)/g);
239
- const settingsCreateCount = settingsCreateMatches ? settingsCreateMatches.length : 0;
240
-
241
- assert(settingsCreateCount === 1, `Expected 1 createTable for Settings, got ${settingsCreateCount}`);
242
- });
243
-
244
- test('No duplicate seed insertion calls', () => {
245
- const migrations = new Migrations();
246
- const oldSchema = [];
247
- const newSchema = [{
248
- __name: 'Settings',
249
- id: { name: 'id', type: 'integer', primaryKey: true }
250
- }];
251
- const seedData = {
252
- Settings: [{ id: 1, value: 'test' }]
253
- };
254
-
255
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema, seedData);
256
-
257
- const seedMatches = migrationCode.match(/this\.seed\('Settings'/g);
258
- const seedCount = seedMatches ? seedMatches.length : 0;
259
-
260
- assert(seedCount === 1, `Expected 1 seed call, got ${seedCount}`);
261
- });
262
-
263
- test('No duplicate dropTable calls in down migration', () => {
264
- const migrations = new Migrations();
265
- const oldSchema = [];
266
- const newSchema = [{
267
- __name: 'Settings',
268
- id: { name: 'id', type: 'integer', primaryKey: true }
269
- }];
270
- const seedData = {
271
- Settings: [{ id: 1 }]
272
- };
273
-
274
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema, seedData);
275
-
276
- const dropMatches = migrationCode.match(/dropTable\(table\.Settings\)/g);
277
- const dropCount = dropMatches ? dropMatches.length : 0;
278
-
279
- assert(dropCount === 1, `Expected 1 dropTable call, got ${dropCount}`);
280
- });
281
-
282
- test('Complex scenario: Multiple tables with seed data, indexes, and composite indexes', () => {
283
- const migrations = new Migrations();
284
- const oldSchema = [];
285
- const newSchema = [
286
- {
287
- __name: 'Document',
288
- id: { name: 'id', type: 'integer', primaryKey: true },
289
- embedding_model_id: { name: 'embedding_model_id', type: 'integer', indexes: ['idx_doc_embed'] }
290
- },
291
- {
292
- __name: 'DocumentChunk',
293
- id: { name: 'id', type: 'integer', primaryKey: true },
294
- __compositeIndexes: [{
295
- name: 'idx_chunk_unique',
296
- columns: ['doc_id', 'chunk_index'],
297
- unique: true
298
- }]
299
- },
300
- {
301
- __name: 'Settings',
302
- id: { name: 'id', type: 'integer', primaryKey: true },
303
- disable_rag: { name: 'disable_rag', type: 'integer' }
304
- },
305
- {
306
- __name: 'Job',
307
- id: { name: 'id', type: 'integer', primaryKey: true }
308
- }
309
- ];
310
- const seedData = {
311
- Settings: [{ disable_rag: 0 }]
312
- };
313
-
314
- const migrationCode = migrations.template('InitialCreate', oldSchema, newSchema, seedData);
315
-
316
- // Check each table appears exactly once
317
- const docCreate = (migrationCode.match(/createTable\(table\.Document\)/g) || []).length;
318
- const chunkCreate = (migrationCode.match(/createTable\(table\.DocumentChunk\)/g) || []).length;
319
- const settingsCreate = (migrationCode.match(/createTable\(table\.Settings\)/g) || []).length;
320
- const jobCreate = (migrationCode.match(/createTable\(table\.Job\)/g) || []).length;
321
-
322
- assert(docCreate === 1, `Document: Expected 1 createTable, got ${docCreate}`);
323
- assert(chunkCreate === 1, `DocumentChunk: Expected 1 createTable, got ${chunkCreate}`);
324
- assert(settingsCreate === 1, `Settings: Expected 1 createTable, got ${settingsCreate}`);
325
- assert(jobCreate === 1, `Job: Expected 1 createTable, got ${jobCreate}`);
326
-
327
- // Check seed uses correct API
328
- assert(migrationCode.includes("this.seed('Settings'"), 'Should use this.seed() for Settings');
329
- assert(!migrationCode.includes('table.Settings.create'), 'Should NOT use table.Settings.create');
330
-
331
- // For new tables, createTable() handles indexes — no separate calls in template
332
- assert(!migrationCode.includes('await this.createIndex'), 'New tables should not have separate createIndex calls');
333
- assert(!migrationCode.includes('await this.createCompositeIndex'), 'New tables should not have separate createCompositeIndex calls');
334
- });
335
-
336
- // =============================================================================
337
- // Test Suite 4: Duplicate Table Deduplication (v0.3.35)
338
- // =============================================================================
339
- console.log("\nšŸ“‹ Test Suite 4: Duplicate Table Deduplication\n");
340
-
341
- test('Deduplicates when snapshot has duplicate table definitions', () => {
342
- const migrations = new Migrations();
343
-
344
- // Simulate snapshot with duplicate Settings table (real-world bug from ragContext)
345
- const oldSchema = [];
346
- const newSchema = [
347
- {
348
- __name: 'Settings',
349
- id: { name: 'id', type: 'integer', primaryKey: true },
350
- disable_rag: { name: 'disable_rag', type: 'integer' }
351
- },
352
- {
353
- __name: 'Document',
354
- id: { name: 'id', type: 'integer', primaryKey: true }
355
- },
356
- {
357
- __name: 'Settings', // DUPLICATE - this caused the bug
358
- id: { name: 'id', type: 'integer', primaryKey: true },
359
- disable_rag: { name: 'disable_rag', type: 'integer' }
360
- }
361
- ];
362
-
363
- const seedData = {
364
- Settings: [{ disable_rag: 0 }]
365
- };
366
-
367
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema, seedData);
368
-
369
- const createTableCount = (migrationCode.match(/createTable\(table\.Settings\)/g) || []).length;
370
- const seedCount = (migrationCode.match(/this\.seed\('Settings'/g) || []).length;
371
-
372
- assert(createTableCount === 1, `Expected 1 createTable for Settings, got ${createTableCount}`);
373
- assert(seedCount === 1, `Expected 1 seed call for Settings, got ${seedCount}`);
374
- });
375
-
376
- test('Deduplicates multiple tables with multiple seeds (qaContext scenario)', () => {
377
- const migrations = new Migrations();
378
-
379
- const oldSchema = [];
380
- const newSchema = [
381
- {
382
- __name: 'TaxonomyTemplate',
383
- id: { name: 'id', type: 'integer', primaryKey: true },
384
- template_id: { name: 'template_id', type: 'string' }
385
- },
386
- {
387
- __name: 'Document',
388
- id: { name: 'id', type: 'integer', primaryKey: true }
389
- },
390
- {
391
- __name: 'TaxonomyTemplate', // DUPLICATE
392
- id: { name: 'id', type: 'integer', primaryKey: true },
393
- template_id: { name: 'template_id', type: 'string' }
394
- }
395
- ];
396
-
397
- const seedData = {
398
- TaxonomyTemplate: [
399
- { template_id: 'template1' },
400
- { template_id: 'template2' },
401
- { template_id: 'template3' },
402
- { template_id: 'template4' },
403
- { template_id: 'template5' }
404
- ]
405
- };
406
-
407
- const migrationCode = migrations.template('TestMigration', oldSchema, newSchema, seedData);
408
-
409
- const createTableCount = (migrationCode.match(/createTable\(table\.TaxonomyTemplate\)/g) || []).length;
410
- const seedCount = (migrationCode.match(/this\.seed\('TaxonomyTemplate'/g) || []).length;
411
-
412
- assert(createTableCount === 1, `Expected 1 createTable for TaxonomyTemplate, got ${createTableCount}`);
413
- assert(seedCount === 5, `Expected 5 seed calls for TaxonomyTemplate, got ${seedCount}`);
414
- });
415
-
416
- // =============================================================================
417
- // Test Results Summary
418
- // =============================================================================
419
- console.log("\n" + "=".repeat(68));
420
- console.log(`\nšŸ“Š Test Results: ${passed} passed, ${failed} failed\n`);
421
-
422
- if (failed === 0) {
423
- console.log("āœ… All tests passed! v0.3.34/0.3.35 fixes are working correctly.\n");
424
- process.exit(0);
425
- } else {
426
- console.log(`āš ļø ${failed} test(s) failed. Please review the output above.\n`);
427
- process.exit(1);
428
- }