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
|
@@ -66,7 +66,9 @@
|
|
|
66
66
|
"Bash(git -C /Users/alexanderrich/Documents/development/bookbaghq/bookbag-training log --oneline --all -- *MASTERRECORD_ISSUE*)",
|
|
67
67
|
"Bash(npm ls -g masterrecord 2>&1 | head -5)",
|
|
68
68
|
"Bash(gh pr:*)",
|
|
69
|
-
"Read(//tmp/**)"
|
|
69
|
+
"Read(//tmp/**)",
|
|
70
|
+
"Bash(wait)",
|
|
71
|
+
"Bash(pkill -f \"node --test\")"
|
|
70
72
|
],
|
|
71
73
|
"deny": [],
|
|
72
74
|
"ask": []
|
package/CHANGES.md
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
|
-
# MasterRecord
|
|
1
|
+
# MasterRecord Changelog
|
|
2
|
+
|
|
3
|
+
## v1.0.0 — ESM-only
|
|
4
|
+
|
|
5
|
+
**BREAKING:** MasterRecord is now a pure ECMAScript Modules (ESM) package.
|
|
6
|
+
|
|
7
|
+
- `package.json` declares `"type": "module"` and publishes an `exports` map.
|
|
8
|
+
- Requires **Node.js 20 or newer**.
|
|
9
|
+
- All sources converted from `require` / `module.exports` to `import` / `export default`.
|
|
10
|
+
- `context.js` imports the SQLite driver (`better-sqlite3`) statically at the top of the module instead of `require(sqlName)` at runtime.
|
|
11
|
+
- Environment config files (`env.*.json`) and migration snapshot files (`*_contextSnapShot.json`) are now loaded with `fs.readFileSync` + `JSON.parse` instead of `require()`, keeping `context.env()` synchronous.
|
|
12
|
+
- User-authored migration files are loaded via `await import(pathToFileURL(file).href)` — `Migrations/cli.js` has always had this path; the CJS `Module.prototype.require` hook that aliased `require('masterrecord')` to the globally installed package has been removed. Host projects must install masterrecord as a local dependency.
|
|
13
|
+
- `Migrations/migrationTemplate.js` always emits ESM migrations (`import masterrecord from 'masterrecord'; … export default <Name>;`). The CJS branch and `moduleType` parameter are gone.
|
|
14
|
+
- `context.js` exports the default class plus named error classes (`ContextError`, `ConfigurationError`, `DatabaseConnectionError`, `EntityValidationError`) and the internal `_poolKey` helper.
|
|
15
|
+
- `Cache/QueryCache.js` background cleanup timer is now `.unref()`'d so it no longer keeps the Node event loop alive after user code finishes.
|
|
16
|
+
- `.eslintrc.js` / `.prettierrc.js` replaced with flat ESM configs (`eslint.config.js`, `prettier.config.js`).
|
|
17
|
+
- Test suite rewritten on top of Node's built-in `node --test` runner (`npm test`).
|
|
18
|
+
- Documentation (`readme.md`, `docs/MIGRATIONS_GUIDE.md`, `docs/POSTGRESQL_SETUP.md`, `docs/QUERY_CACHING_GUIDE.md`, `FOREIGN_KEY_STRING_FIX.md`) updated to ESM syntax throughout.
|
|
19
|
+
|
|
20
|
+
### Migration guide for consumers
|
|
21
|
+
|
|
22
|
+
1. Bump your Node.js runtime to 20+.
|
|
23
|
+
2. Add `"type": "module"` to your application's `package.json`.
|
|
24
|
+
3. Replace `const X = require('masterrecord/…')` with `import X from 'masterrecord/…'`.
|
|
25
|
+
4. Replace `module.exports = MyContext` with `export default MyContext`.
|
|
26
|
+
5. Add `.js` extensions to all relative import paths (ESM requires explicit extensions).
|
|
27
|
+
6. Regenerate any existing migration files (or rewrite the top/bottom: `import masterrecord from 'masterrecord';` … `export default <Name>;`).
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Previous: v0.x — FAANG-Level Improvements
|
|
2
32
|
|
|
3
|
-
## Summary
|
|
4
33
|
Updated `context.js` and `deleteManager.js` to meet Google/Meta/Amazon engineering standards with critical bug fixes, input validation, and performance improvements.
|
|
5
34
|
|
|
6
35
|
## Critical Fixes Applied
|
package/Cache/QueryCache.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Production-Grade Query Result Cache
|
|
3
5
|
* Similar to EF Core's EFCoreSecondLevelCacheInterceptor
|
|
@@ -20,7 +22,6 @@ class QueryCache {
|
|
|
20
22
|
* Like Hibernate's query cache regions
|
|
21
23
|
*/
|
|
22
24
|
generateKey(query, params, tableName) {
|
|
23
|
-
const crypto = require('crypto');
|
|
24
25
|
const normalizedQuery = query.trim().toLowerCase().replace(/\s+/g, ' ');
|
|
25
26
|
const keyData = {
|
|
26
27
|
query: normalizedQuery,
|
|
@@ -154,7 +155,10 @@ class QueryCache {
|
|
|
154
155
|
* Background cleanup of expired entries
|
|
155
156
|
*/
|
|
156
157
|
_startCleanupTimer() {
|
|
157
|
-
|
|
158
|
+
// .unref() so the interval does not keep the Node process alive
|
|
159
|
+
// after user code finishes — callers should not have to explicitly
|
|
160
|
+
// stop the cleanup timer to let their script exit.
|
|
161
|
+
const timer = setInterval(() => {
|
|
158
162
|
const now = Date.now();
|
|
159
163
|
let cleaned = 0;
|
|
160
164
|
|
|
@@ -168,8 +172,10 @@ class QueryCache {
|
|
|
168
172
|
if (cleaned > 0 && process.env.NODE_ENV !== 'production') {
|
|
169
173
|
console.debug(`[QueryCache CLEANUP] Removed ${cleaned} expired entries`);
|
|
170
174
|
}
|
|
171
|
-
}, 60000);
|
|
175
|
+
}, 60000);
|
|
176
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
177
|
+
this._cleanupTimer = timer;
|
|
172
178
|
}
|
|
173
179
|
}
|
|
174
180
|
|
|
175
|
-
|
|
181
|
+
export default QueryCache;
|
package/Cache/RedisQueryCache.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Distributed Query Cache using Redis
|
|
3
5
|
* For multi-process/clustered deployments
|
|
@@ -15,7 +17,6 @@ class RedisQueryCache {
|
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
generateKey(query, params, tableName) {
|
|
18
|
-
const crypto = require('crypto');
|
|
19
20
|
const hash = crypto.createHash('sha256')
|
|
20
21
|
.update(JSON.stringify({query, params}))
|
|
21
22
|
.digest('hex');
|
|
@@ -152,4 +153,4 @@ class RedisQueryCache {
|
|
|
152
153
|
}
|
|
153
154
|
}
|
|
154
155
|
|
|
155
|
-
|
|
156
|
+
export default RedisQueryCache;
|
package/Entity/entityModel.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// version 0.0.3
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import modelDB from './entityModel.js';
|
|
4
4
|
|
|
5
5
|
// creates new instance if entity model and calls inner functions to build out a valid entity
|
|
6
6
|
class EntityModelBuilder {
|
|
@@ -102,4 +102,4 @@ class EntityModelBuilder {
|
|
|
102
102
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
export default EntityModelBuilder;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
// version : 0.0.9
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import tools from '../Tools.js';
|
|
4
|
+
import FieldTransformer from './fieldTransformer.js';
|
|
5
5
|
|
|
6
6
|
class EntityTrackerModel {
|
|
7
7
|
|
|
@@ -523,4 +523,4 @@ class EntityTrackerModel {
|
|
|
523
523
|
|
|
524
524
|
}
|
|
525
525
|
|
|
526
|
-
|
|
526
|
+
export default EntityTrackerModel;
|
|
@@ -243,8 +243,8 @@ This bug was reported by Bookbag.ai Engineering Team and affects any application
|
|
|
243
243
|
To verify the fix works with your code:
|
|
244
244
|
|
|
245
245
|
```javascript
|
|
246
|
-
|
|
247
|
-
|
|
246
|
+
import UserOrganizationRole from './models/userOrganizationRole.js';
|
|
247
|
+
import userContext from './models/userContext.js';
|
|
248
248
|
|
|
249
249
|
async function testFix() {
|
|
250
250
|
const ctx = new userContext();
|
package/MasterRecord.js
CHANGED
|
@@ -1,49 +1,12 @@
|
|
|
1
|
+
// version 1.1.0
|
|
2
|
+
import context from './context.js';
|
|
3
|
+
import schema from './Migrations/schema.js';
|
|
1
4
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// version 1.0.20
|
|
5
|
-
var context = require("./context");
|
|
6
|
-
var schema = require("./Migrations/schema");
|
|
7
|
-
|
|
8
|
-
class masterrecord{
|
|
9
|
-
|
|
10
|
-
constructor(){
|
|
5
|
+
class masterrecord {
|
|
6
|
+
constructor() {
|
|
11
7
|
this.context = context;
|
|
12
8
|
this.schema = schema;
|
|
13
9
|
}
|
|
14
10
|
}
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
module.exports = new masterrecord();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
/*
|
|
22
|
-
|
|
23
|
-
//Create new standard
|
|
24
|
-
var standard = new Standard();
|
|
25
|
-
standard.StandardName = "Standard1";
|
|
26
|
-
|
|
27
|
-
//create three new teachers
|
|
28
|
-
var teacher1 = new Teacher();
|
|
29
|
-
teacher1.TeacherName = "New Teacher1";
|
|
30
|
-
|
|
31
|
-
var teacher2 = new Teacher();
|
|
32
|
-
teacher2.TeacherName = "New Teacher2";
|
|
33
|
-
|
|
34
|
-
var teacher3 = new Teacher();
|
|
35
|
-
teacher3.TeacherName = "New Teacher3";
|
|
36
|
-
|
|
37
|
-
//add teachers for new standard
|
|
38
|
-
standard.Teachers.Add(teacher1);
|
|
39
|
-
standard.Teachers.Add(teacher2);
|
|
40
|
-
standard.Teachers.Add(teacher3);
|
|
41
|
-
|
|
42
|
-
using (var dbCtx = new SchoolDBEntities())
|
|
43
|
-
{
|
|
44
|
-
//add standard entity into standards entitySet
|
|
45
|
-
dbCtx.Standards.Add(standard);
|
|
46
|
-
//Save whole entity graph to the database
|
|
47
|
-
dbCtx.SaveChanges();
|
|
48
|
-
}
|
|
49
|
-
*/
|
|
12
|
+
export default new masterrecord();
|
package/Migrations/cli.js
CHANGED
|
@@ -1,28 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// version 0.0
|
|
3
|
+
// version 0.1.0 - ESM only
|
|
4
4
|
// https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/
|
|
5
5
|
// how to add environment variables on cli call example - master=development masterrecord add-migration auth authContext
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
import { program } from 'commander';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
11
|
+
import { globSync } from 'glob';
|
|
12
|
+
import { resolveMigrationsDirectory } from './pathUtils.js';
|
|
13
|
+
import Migration from './migrations.js';
|
|
14
|
+
|
|
15
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
16
|
+
const __dirname = path.dirname(__filename);
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
19
|
* Load a user module (context, migration) via dynamic import.
|
|
16
|
-
*
|
|
17
|
-
* Handles both CJS and ESM targets. Required because:
|
|
18
|
-
* - CJS require() of an ESM file throws on older Node, or returns a Module
|
|
19
|
-
* namespace on newer Node (22.12+) — neither shape matches the
|
|
20
|
-
* `new ContextCtor()` pattern downstream.
|
|
21
|
-
* - await import() works in both directions and is consistent across Node
|
|
22
|
-
* versions.
|
|
23
|
-
*
|
|
24
|
-
* The returned value is unwrapped: ESM `export default X` -> X;
|
|
25
|
-
* CJS `module.exports = X` -> X; mixed shapes are handled via `.default ?? mod`.
|
|
20
|
+
* Unwraps `export default X` -> X; returns whole namespace if no default.
|
|
26
21
|
*
|
|
27
22
|
* @param {string} filePath - Absolute path to the user file
|
|
28
23
|
* @returns {Promise<*>} The default export (or whole module if no default)
|
|
@@ -32,47 +27,7 @@ async function __loadUserModule(filePath) {
|
|
|
32
27
|
return (mod && mod.default !== undefined) ? mod.default : mod;
|
|
33
28
|
}
|
|
34
29
|
|
|
35
|
-
|
|
36
|
-
* Walk up from a given directory looking for the host project's package.json
|
|
37
|
-
* and return whether it declares ESM (`"type": "module"`) or CJS.
|
|
38
|
-
* Used when generating migration files so the emitted syntax matches the
|
|
39
|
-
* host project's module type. Masterrecord's own package.json is skipped.
|
|
40
|
-
*
|
|
41
|
-
* @param {string} startDir - Directory to walk up from
|
|
42
|
-
* @returns {'esm' | 'cjs'}
|
|
43
|
-
*/
|
|
44
|
-
function __detectHostModuleType(startDir) {
|
|
45
|
-
let dir = startDir;
|
|
46
|
-
for (let i = 0; i < 12; i++) {
|
|
47
|
-
const pkgPath = path.join(dir, 'package.json');
|
|
48
|
-
if (fs.existsSync(pkgPath)) {
|
|
49
|
-
try {
|
|
50
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
51
|
-
if (pkg && pkg.name === 'masterrecord') {
|
|
52
|
-
// Skip our own package.json — keep walking up to the host project
|
|
53
|
-
} else {
|
|
54
|
-
return (pkg && pkg.type === 'module') ? 'esm' : 'cjs';
|
|
55
|
-
}
|
|
56
|
-
} catch (_) { /* keep walking */ }
|
|
57
|
-
}
|
|
58
|
-
const parent = path.dirname(dir);
|
|
59
|
-
if (parent === dir) break;
|
|
60
|
-
dir = parent;
|
|
61
|
-
}
|
|
62
|
-
return 'cjs';
|
|
63
|
-
}
|
|
64
|
-
// Alias require('masterrecord') to this global package so project files don't need a local install
|
|
65
|
-
const __MASTERRECORD_ROOT__ = path.join(__dirname, '..');
|
|
66
|
-
const __ORIGINAL_REQUIRE__ = Module.prototype.require;
|
|
67
|
-
Module.prototype.require = function(request) {
|
|
68
|
-
if (request === 'masterrecord') {
|
|
69
|
-
return __ORIGINAL_REQUIRE__.call(this, __MASTERRECORD_ROOT__);
|
|
70
|
-
}
|
|
71
|
-
return __ORIGINAL_REQUIRE__.call(this, request);
|
|
72
|
-
};
|
|
73
|
-
var Migration = require('./migrations');
|
|
74
|
-
var globSearch = require("glob");
|
|
75
|
-
const pkg = require(path.join(__dirname, '..', 'package.json'));
|
|
30
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));
|
|
76
31
|
|
|
77
32
|
// Extract numeric timestamp from migration filename (e.g., 1737999999999_name_migration.js)
|
|
78
33
|
function __getMigrationTimestamp(file){
|
|
@@ -156,7 +111,7 @@ program.option('-V', 'output the version');
|
|
|
156
111
|
var migration = new Migration();
|
|
157
112
|
var contextInstance = null;
|
|
158
113
|
try{
|
|
159
|
-
var files =
|
|
114
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
160
115
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
161
116
|
if(!file){
|
|
162
117
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -164,7 +119,7 @@ program.option('-V', 'output the version');
|
|
|
164
119
|
}
|
|
165
120
|
var contextSnapshot;
|
|
166
121
|
try{
|
|
167
|
-
contextSnapshot =
|
|
122
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
168
123
|
}catch(_){
|
|
169
124
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
170
125
|
return;
|
|
@@ -178,7 +133,7 @@ program.option('-V', 'output the version');
|
|
|
178
133
|
migBase = snapDir;
|
|
179
134
|
}
|
|
180
135
|
// Find latest migration file (so we can use its class which extends schema)
|
|
181
|
-
var migrationFiles =
|
|
136
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
182
137
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
183
138
|
if(!(migrationFiles && migrationFiles.length)){
|
|
184
139
|
console.log("Error - Cannot read or find migration file");
|
|
@@ -247,26 +202,6 @@ program.option('-V', 'output the version');
|
|
|
247
202
|
// contextFileName = contextFileName.toLowerCase();
|
|
248
203
|
|
|
249
204
|
// try{
|
|
250
|
-
// // find context file from main folder location
|
|
251
|
-
// // find context file from main folder location
|
|
252
|
-
// var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
253
|
-
// var files = globSearch.sync(search, executedLocation);
|
|
254
|
-
// var file = files[0];
|
|
255
|
-
|
|
256
|
-
// if(file){
|
|
257
|
-
// var contextSnapshot = require(file);
|
|
258
|
-
// var context = require(contextSnapshot.contextLocation);
|
|
259
|
-
// var newSchema = new schema(context);
|
|
260
|
-
// newSchema.createDatabase(dbName);
|
|
261
|
-
// }
|
|
262
|
-
// else{
|
|
263
|
-
// console.log("Error - Cannot read or find Context file");
|
|
264
|
-
// }
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
// }catch (e){
|
|
268
|
-
// console.log("Error - Cannot read or find file ", e);
|
|
269
|
-
// }
|
|
270
205
|
// console.log("Database Created");
|
|
271
206
|
|
|
272
207
|
// });
|
|
@@ -282,7 +217,7 @@ program.option('-V', 'output the version');
|
|
|
282
217
|
var migration = new Migration();
|
|
283
218
|
try{
|
|
284
219
|
// find context file from main folder location
|
|
285
|
-
var files =
|
|
220
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
286
221
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
287
222
|
if(!file){
|
|
288
223
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'. Run 'masterrecord enable-migrations ${contextFileName}'.`);
|
|
@@ -290,7 +225,7 @@ program.option('-V', 'output the version');
|
|
|
290
225
|
}
|
|
291
226
|
var contextSnapshot = null;
|
|
292
227
|
try{
|
|
293
|
-
contextSnapshot =
|
|
228
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
294
229
|
}catch(_){
|
|
295
230
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
296
231
|
return;
|
|
@@ -350,10 +285,7 @@ program.option('-V', 'output the version');
|
|
|
350
285
|
return;
|
|
351
286
|
}
|
|
352
287
|
|
|
353
|
-
|
|
354
|
-
// so the generated .js file parses correctly when loaded by update-database.
|
|
355
|
-
var moduleType = __detectHostModuleType(path.dirname(contextAbs));
|
|
356
|
-
var newEntity = migration.template(name, contextSnapshot.schema, cleanEntities, seedData, seedConfig, null, moduleType);
|
|
288
|
+
var newEntity = migration.template(name, contextSnapshot.schema, cleanEntities, seedData, seedConfig, null);
|
|
357
289
|
if(!fs.existsSync(migBase)){
|
|
358
290
|
try{ fs.mkdirSync(migBase, { recursive: true }); }catch(_){ /* ignore */ }
|
|
359
291
|
}
|
|
@@ -385,7 +317,7 @@ program.option('-V', 'output the version');
|
|
|
385
317
|
try{
|
|
386
318
|
console.log(`\n🔍 Searching for context snapshot '${contextFileName}_contextSnapShot.json'...`);
|
|
387
319
|
// find context snapshot (cwd-based glob)
|
|
388
|
-
var files =
|
|
320
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
389
321
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
390
322
|
|
|
391
323
|
if(!file){
|
|
@@ -400,7 +332,7 @@ program.option('-V', 'output the version');
|
|
|
400
332
|
|
|
401
333
|
var contextSnapshot;
|
|
402
334
|
try{
|
|
403
|
-
contextSnapshot =
|
|
335
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
404
336
|
}catch(err){
|
|
405
337
|
console.error(`\n❌ Error - Cannot load context snapshot`);
|
|
406
338
|
console.error(`\nFile: ${file}`);
|
|
@@ -418,7 +350,7 @@ program.option('-V', 'output the version');
|
|
|
418
350
|
}
|
|
419
351
|
|
|
420
352
|
console.log(`\n🔍 Searching for migration files in: ${migBase}`);
|
|
421
|
-
var migrationFiles =
|
|
353
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
422
354
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
423
355
|
|
|
424
356
|
if(!(migrationFiles && migrationFiles.length)){
|
|
@@ -571,7 +503,7 @@ program.option('-V', 'output the version');
|
|
|
571
503
|
var migration = new Migration();
|
|
572
504
|
var contextInstance = null;
|
|
573
505
|
try{
|
|
574
|
-
var files =
|
|
506
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
575
507
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
576
508
|
if(!file){
|
|
577
509
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -579,7 +511,7 @@ program.option('-V', 'output the version');
|
|
|
579
511
|
}
|
|
580
512
|
var contextSnapshot;
|
|
581
513
|
try{
|
|
582
|
-
contextSnapshot =
|
|
514
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
583
515
|
}catch(_){
|
|
584
516
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
585
517
|
return;
|
|
@@ -587,7 +519,7 @@ program.option('-V', 'output the version');
|
|
|
587
519
|
const snapDir = path.dirname(file);
|
|
588
520
|
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
589
521
|
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
590
|
-
var migrationFiles =
|
|
522
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
591
523
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
592
524
|
if(!(migrationFiles && migrationFiles.length)){
|
|
593
525
|
console.log("Error - Cannot read or find migration file");
|
|
@@ -673,7 +605,7 @@ program.option('-V', 'output the version');
|
|
|
673
605
|
var contextInstance = null;
|
|
674
606
|
try{
|
|
675
607
|
// find context snapshot (cwd-based glob)
|
|
676
|
-
var files =
|
|
608
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
677
609
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
678
610
|
if(!file){
|
|
679
611
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -681,7 +613,7 @@ program.option('-V', 'output the version');
|
|
|
681
613
|
}
|
|
682
614
|
var contextSnapshot;
|
|
683
615
|
try{
|
|
684
|
-
contextSnapshot =
|
|
616
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
685
617
|
}catch(_){
|
|
686
618
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
687
619
|
return;
|
|
@@ -689,7 +621,7 @@ program.option('-V', 'output the version');
|
|
|
689
621
|
const snapDir = path.dirname(file);
|
|
690
622
|
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
691
623
|
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
692
|
-
var migrationFiles =
|
|
624
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
693
625
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
694
626
|
if(!(migrationFiles && migrationFiles.length)){
|
|
695
627
|
console.log("Error - Cannot read or find migration file");
|
|
@@ -764,7 +696,7 @@ program.option('-V', 'output the version');
|
|
|
764
696
|
.action(function(contextFileName){
|
|
765
697
|
var executedLocation = process.cwd();
|
|
766
698
|
contextFileName = contextFileName.toLowerCase();
|
|
767
|
-
var files =
|
|
699
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
768
700
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
769
701
|
if(!file){
|
|
770
702
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -772,12 +704,12 @@ program.option('-V', 'output the version');
|
|
|
772
704
|
}
|
|
773
705
|
var contextSnapshot;
|
|
774
706
|
try{
|
|
775
|
-
contextSnapshot =
|
|
707
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
776
708
|
}catch(_){
|
|
777
709
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
778
710
|
return;
|
|
779
711
|
}
|
|
780
|
-
var migrationFiles =
|
|
712
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: contextSnapshot.migrationFolder, dot: true, windowsPathsNoEscape: true });
|
|
781
713
|
if(!(migrationFiles && migrationFiles.length)){
|
|
782
714
|
console.log("No migration files found.");
|
|
783
715
|
return;
|
|
@@ -804,7 +736,7 @@ program.option('-V', 'output the version');
|
|
|
804
736
|
var targetName = path.basename(migrationFileName);
|
|
805
737
|
|
|
806
738
|
// Locate the target migration file anywhere under the current folder
|
|
807
|
-
var targetMatches =
|
|
739
|
+
var targetMatches = globSync(`**/${targetName}`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true });
|
|
808
740
|
if(!(targetMatches && targetMatches.length)){
|
|
809
741
|
console.log(`Error - Cannot read or find migration file '${targetName}' in '${executedLocation}'.`);
|
|
810
742
|
return;
|
|
@@ -813,7 +745,7 @@ program.option('-V', 'output the version');
|
|
|
813
745
|
var migrationFolder = path.dirname(targetFilePath);
|
|
814
746
|
|
|
815
747
|
// Find the context snapshot within the same migrations folder
|
|
816
|
-
var snapshotMatches =
|
|
748
|
+
var snapshotMatches = globSync(`**/*_contextSnapShot.json`, { cwd: migrationFolder, dot: true, windowsPathsNoEscape: true });
|
|
817
749
|
var snapshotFile = snapshotMatches && snapshotMatches[0] ? path.resolve(migrationFolder, snapshotMatches[0]) : null;
|
|
818
750
|
if(!snapshotFile){
|
|
819
751
|
console.log("Error - Cannot read or find Context snapshot in migration folder.");
|
|
@@ -822,14 +754,14 @@ program.option('-V', 'output the version');
|
|
|
822
754
|
|
|
823
755
|
var contextSnapshot;
|
|
824
756
|
try{
|
|
825
|
-
contextSnapshot =
|
|
757
|
+
contextSnapshot = JSON.parse(fs.readFileSync(snapshotFile, 'utf8'));
|
|
826
758
|
}catch(_){
|
|
827
759
|
console.log(`Error - Cannot read context snapshot at '${snapshotFile}'.`);
|
|
828
760
|
return;
|
|
829
761
|
}
|
|
830
762
|
|
|
831
763
|
// Get all migration files in this folder
|
|
832
|
-
var allMigrationFiles =
|
|
764
|
+
var allMigrationFiles = globSync(`**/*_migration.js`, { cwd: migrationFolder, dot: true, windowsPathsNoEscape: true });
|
|
833
765
|
if(!(allMigrationFiles && allMigrationFiles.length)){
|
|
834
766
|
console.log("Error - Cannot read or find migration file");
|
|
835
767
|
return;
|
|
@@ -922,7 +854,7 @@ program.option('-V', 'output the version');
|
|
|
922
854
|
var executedLocation = process.cwd();
|
|
923
855
|
var contextInstances = [];
|
|
924
856
|
try{
|
|
925
|
-
var snapshotFiles =
|
|
857
|
+
var snapshotFiles = globSync('**/*_contextSnapShot.json', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
926
858
|
if(!(snapshotFiles && snapshotFiles.length)){
|
|
927
859
|
console.log('No context snapshots found. Run enable-migrations-all first.');
|
|
928
860
|
return;
|
|
@@ -932,7 +864,7 @@ program.option('-V', 'output the version');
|
|
|
932
864
|
try{
|
|
933
865
|
const snapFile = path.resolve(executedLocation, snapRel);
|
|
934
866
|
let cs;
|
|
935
|
-
try{ cs =
|
|
867
|
+
try{ cs = JSON.parse(fs.readFileSync(snapFile, 'utf8')); }catch(_){ continue; }
|
|
936
868
|
const snapDir = path.dirname(snapFile);
|
|
937
869
|
const contextAbs = path.resolve(snapDir, cs.contextLocation || '');
|
|
938
870
|
const migBase = path.resolve(snapDir, cs.migrationFolder || '.');
|
|
@@ -964,8 +896,7 @@ program.option('-V', 'output the version');
|
|
|
964
896
|
console.log(`No changes detected for ${path.basename(contextAbs)}. Skipping.`);
|
|
965
897
|
continue;
|
|
966
898
|
}
|
|
967
|
-
var
|
|
968
|
-
var newEntity = migration.template(name, cs.schema, cleanEntities, seedData, seedConfig, null, moduleType);
|
|
899
|
+
var newEntity = migration.template(name, cs.schema, cleanEntities, seedData, seedConfig, null);
|
|
969
900
|
if(!fs.existsSync(migBase)){
|
|
970
901
|
try{ fs.mkdirSync(migBase, { recursive: true }); }catch(_){ /* ignore */ }
|
|
971
902
|
}
|
|
@@ -1009,7 +940,7 @@ program.option('-V', 'output the version');
|
|
|
1009
940
|
var contextInstances = [];
|
|
1010
941
|
try{
|
|
1011
942
|
// Find all context snapshots and run update per snapshot (avoids unrelated framework contexts)
|
|
1012
|
-
var snapshotFiles =
|
|
943
|
+
var snapshotFiles = globSync('**/*_contextSnapShot.json', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
1013
944
|
if(!(snapshotFiles && snapshotFiles.length)){
|
|
1014
945
|
console.log('No context snapshots found. Run enable-migrations for each context first.');
|
|
1015
946
|
return;
|
|
@@ -1019,18 +950,18 @@ program.option('-V', 'output the version');
|
|
|
1019
950
|
for(const snapRel of snapshotFiles){
|
|
1020
951
|
const snapFile = path.resolve(executedLocation, snapRel);
|
|
1021
952
|
let cs;
|
|
1022
|
-
try{ cs =
|
|
953
|
+
try{ cs = JSON.parse(fs.readFileSync(snapFile, 'utf8')); }catch(_){ continue; }
|
|
1023
954
|
const snapDir = path.dirname(snapFile);
|
|
1024
955
|
const contextAbs = path.resolve(snapDir, cs.contextLocation || '');
|
|
1025
956
|
let migBase = path.resolve(snapDir, cs.migrationFolder || '.');
|
|
1026
957
|
const nameFromPath = path.basename(snapFile).replace(/_contextSnapShot\.json$/i, '').toLowerCase();
|
|
1027
958
|
const ctxName = contextAbs ? path.basename(contextAbs).replace(/\.js$/i, '').toLowerCase() : nameFromPath;
|
|
1028
959
|
// Find migrations in snapshot's migrationFolder; fallback to <ContextDir>/db/migrations
|
|
1029
|
-
let migRel =
|
|
960
|
+
let migRel = globSync('**/*_migration.js', { cwd: migBase, dot: true, windowsPathsNoEscape: true, nocase: true }) || [];
|
|
1030
961
|
if(!(migRel && migRel.length)){
|
|
1031
962
|
// Fallback: find migrations directory using shared utility (prevents duplicate paths)
|
|
1032
963
|
const defaultFolder = resolveMigrationsDirectory(contextAbs || snapFile);
|
|
1033
|
-
migRel =
|
|
964
|
+
migRel = globSync('**/*_migration.js', { cwd: defaultFolder, dot: true, windowsPathsNoEscape: true, nocase: true }) || [];
|
|
1034
965
|
if(migRel && migRel.length){ migBase = defaultFolder; }
|
|
1035
966
|
}
|
|
1036
967
|
const migs = migRel.map(f => path.resolve(migBase, f));
|
|
@@ -1119,7 +1050,7 @@ program.option('-V', 'output the version');
|
|
|
1119
1050
|
var executedLocation = process.cwd();
|
|
1120
1051
|
try{
|
|
1121
1052
|
// Find candidate Context files
|
|
1122
|
-
var candidates =
|
|
1053
|
+
var candidates = globSync('**/*Context.js', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true }) || [];
|
|
1123
1054
|
if(!(candidates && candidates.length)){
|
|
1124
1055
|
console.log('No Context files found.');
|
|
1125
1056
|
return;
|
|
@@ -1135,7 +1066,7 @@ program.option('-V', 'output the version');
|
|
|
1135
1066
|
// Heuristic filter: file must look like a MasterRecord context
|
|
1136
1067
|
let text = '';
|
|
1137
1068
|
try{ text = fs.readFileSync(abs, 'utf8'); }catch(_){ continue; }
|
|
1138
|
-
const looksLikeContext = /extends\s+masterrecord\.context/i.test(text) || /
|
|
1069
|
+
const looksLikeContext = /extends\s+masterrecord\.context/i.test(text) || /import\s+masterrecord\s+from\s+['"]masterrecord['"]/i.test(text);
|
|
1139
1070
|
if(!looksLikeContext){ continue; }
|
|
1140
1071
|
const ctxName = path.basename(abs).replace(/\.js$/i,'');
|
|
1141
1072
|
const key = ctxName.toLowerCase();
|