masterrecord 0.2.22 → 0.2.24
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/Migrations/cli.js +33 -21
- package/context.js +21 -7
- package/package.json +1 -1
package/Migrations/cli.js
CHANGED
|
@@ -96,9 +96,12 @@ program.option('-V', 'output the version');
|
|
|
96
96
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
|
+
const snapDir = path.dirname(file);
|
|
100
|
+
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
101
|
+
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
99
102
|
// Find latest migration file (so we can use its class which extends schema)
|
|
100
|
-
var migrationFiles = globSearch.sync(`**/*_migration.js`, { cwd:
|
|
101
|
-
migrationFiles = (migrationFiles || []).map(f => path.resolve(
|
|
103
|
+
var migrationFiles = globSearch.sync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
104
|
+
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
102
105
|
if(!(migrationFiles && migrationFiles.length)){
|
|
103
106
|
console.log("Error - Cannot read or find migration file");
|
|
104
107
|
return;
|
|
@@ -110,9 +113,9 @@ program.option('-V', 'output the version');
|
|
|
110
113
|
|
|
111
114
|
let ContextCtor;
|
|
112
115
|
try{
|
|
113
|
-
ContextCtor = require(
|
|
116
|
+
ContextCtor = require(contextAbs);
|
|
114
117
|
}catch(_){
|
|
115
|
-
console.log(`Error - Cannot load Context file at '${
|
|
118
|
+
console.log(`Error - Cannot load Context file at '${contextAbs}'.`);
|
|
116
119
|
return;
|
|
117
120
|
}
|
|
118
121
|
|
|
@@ -248,8 +251,11 @@ program.option('-V', 'output the version');
|
|
|
248
251
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
249
252
|
if(file){
|
|
250
253
|
var contextSnapshot = require(file);
|
|
251
|
-
|
|
252
|
-
|
|
254
|
+
const snapDir = path.dirname(file);
|
|
255
|
+
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
256
|
+
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
257
|
+
var migrationFiles = globSearch.sync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
258
|
+
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
253
259
|
if( migrationFiles && migrationFiles.length){
|
|
254
260
|
// sort by timestamp prefix or file mtime as fallback
|
|
255
261
|
var mFiles = migrationFiles.slice().sort(function(a, b){
|
|
@@ -257,9 +263,9 @@ program.option('-V', 'output the version');
|
|
|
257
263
|
});
|
|
258
264
|
var mFile = mFiles[mFiles.length -1];
|
|
259
265
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
266
|
+
var migrationProjectFile = require(mFile);
|
|
267
|
+
var ContextCtor = require(contextAbs);
|
|
268
|
+
var contextInstance = new ContextCtor();
|
|
263
269
|
var newMigrationProjectInstance = new migrationProjectFile(context);
|
|
264
270
|
|
|
265
271
|
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
@@ -267,7 +273,7 @@ program.option('-V', 'output the version');
|
|
|
267
273
|
newMigrationProjectInstance.up(tableObj);
|
|
268
274
|
|
|
269
275
|
var snap = {
|
|
270
|
-
file :
|
|
276
|
+
file : contextAbs,
|
|
271
277
|
executedLocation : executedLocation,
|
|
272
278
|
context : contextInstance,
|
|
273
279
|
contextEntities : cleanEntities,
|
|
@@ -313,8 +319,11 @@ program.option('-V', 'output the version');
|
|
|
313
319
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
314
320
|
return;
|
|
315
321
|
}
|
|
316
|
-
|
|
317
|
-
|
|
322
|
+
const snapDir = path.dirname(file);
|
|
323
|
+
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
324
|
+
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
325
|
+
var migrationFiles = globSearch.sync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
326
|
+
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
318
327
|
if(!(migrationFiles && migrationFiles.length)){
|
|
319
328
|
console.log("Error - Cannot read or find migration file");
|
|
320
329
|
return;
|
|
@@ -328,9 +337,9 @@ program.option('-V', 'output the version');
|
|
|
328
337
|
// Prepare context and table object
|
|
329
338
|
let ContextCtor;
|
|
330
339
|
try{
|
|
331
|
-
ContextCtor = require(
|
|
340
|
+
ContextCtor = require(contextAbs);
|
|
332
341
|
}catch(_){
|
|
333
|
-
console.log(`Error - Cannot load Context file at '${
|
|
342
|
+
console.log(`Error - Cannot load Context file at '${contextAbs}'.`);
|
|
334
343
|
return;
|
|
335
344
|
}
|
|
336
345
|
var contextInstance;
|
|
@@ -353,7 +362,7 @@ program.option('-V', 'output the version');
|
|
|
353
362
|
|
|
354
363
|
// Update snapshot
|
|
355
364
|
var snap = {
|
|
356
|
-
file :
|
|
365
|
+
file : contextAbs,
|
|
357
366
|
executedLocation : executedLocation,
|
|
358
367
|
context : contextInstance,
|
|
359
368
|
contextEntities : cleanEntities,
|
|
@@ -384,15 +393,18 @@ program.option('-V', 'output the version');
|
|
|
384
393
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
385
394
|
return;
|
|
386
395
|
}
|
|
387
|
-
|
|
396
|
+
var contextSnapshot;
|
|
388
397
|
try{
|
|
389
398
|
contextSnapshot = require(file);
|
|
390
399
|
}catch(_){
|
|
391
400
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
392
401
|
return;
|
|
393
402
|
}
|
|
394
|
-
|
|
395
|
-
|
|
403
|
+
const snapDir = path.dirname(file);
|
|
404
|
+
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
405
|
+
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
406
|
+
var migrationFiles = globSearch.sync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
407
|
+
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
396
408
|
if(!(migrationFiles && migrationFiles.length)){
|
|
397
409
|
console.log("Error - Cannot read or find migration file");
|
|
398
410
|
return;
|
|
@@ -533,9 +545,9 @@ program.option('-V', 'output the version');
|
|
|
533
545
|
// Prepare context and table object
|
|
534
546
|
let ContextCtor;
|
|
535
547
|
try{
|
|
536
|
-
ContextCtor = require(
|
|
548
|
+
ContextCtor = require(contextAbs);
|
|
537
549
|
}catch(_){
|
|
538
|
-
console.log(`Error - Cannot load Context file at '${
|
|
550
|
+
console.log(`Error - Cannot load Context file at '${contextAbs}'.`);
|
|
539
551
|
return;
|
|
540
552
|
}
|
|
541
553
|
var contextInstance;
|
|
@@ -562,7 +574,7 @@ program.option('-V', 'output the version');
|
|
|
562
574
|
|
|
563
575
|
// Update snapshot
|
|
564
576
|
var snap = {
|
|
565
|
-
file :
|
|
577
|
+
file : contextAbs,
|
|
566
578
|
executedLocation : executedLocation,
|
|
567
579
|
context : contextInstance,
|
|
568
580
|
contextEntities : cleanEntities,
|
package/context.js
CHANGED
|
@@ -112,7 +112,7 @@ class context {
|
|
|
112
112
|
const abs = path.isAbsolute(rel) ? rel : path.resolve(currentRoot, rel);
|
|
113
113
|
return { file: abs, rootFolder: currentRoot };
|
|
114
114
|
}
|
|
115
|
-
const parent =
|
|
115
|
+
const parent = path.dirname(currentRoot);
|
|
116
116
|
if(parent === currentRoot || parent === ""){
|
|
117
117
|
break;
|
|
118
118
|
}
|
|
@@ -166,11 +166,15 @@ class context {
|
|
|
166
166
|
|
|
167
167
|
if(type === 'sqlite' || type === 'better-sqlite3'){
|
|
168
168
|
this.isSQLite = true; this.isMySQL = false;
|
|
169
|
-
//
|
|
169
|
+
// Treat leading project-style paths ('/components/...') as project-root relative across OSes
|
|
170
170
|
let dbPath = options.connection || '';
|
|
171
171
|
if(dbPath){
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
const looksProjectRootRelative = dbPath.startsWith('/') || dbPath.startsWith('\\');
|
|
173
|
+
const isAbsoluteFsPath = path.isAbsolute(dbPath);
|
|
174
|
+
if(looksProjectRootRelative || !isAbsoluteFsPath){
|
|
175
|
+
// Normalize leading separators to avoid duplicating separators on Windows
|
|
176
|
+
const trimmed = dbPath.replace(/^[/\\]+/, '');
|
|
177
|
+
dbPath = path.join(file.rootFolder, trimmed);
|
|
174
178
|
}
|
|
175
179
|
}
|
|
176
180
|
const dbDir = path.dirname(dbPath);
|
|
@@ -214,11 +218,21 @@ class context {
|
|
|
214
218
|
}
|
|
215
219
|
|
|
216
220
|
this.validateSQLiteOptions(options);
|
|
217
|
-
|
|
218
|
-
|
|
221
|
+
// Build DB path similarly to env(): project-root relative on leading slash
|
|
222
|
+
let dbPath = options.connection || '';
|
|
223
|
+
if(dbPath){
|
|
224
|
+
const looksProjectRootRelative = dbPath.startsWith('/') || dbPath.startsWith('\\');
|
|
225
|
+
const isAbsoluteFsPath = path.isAbsolute(dbPath);
|
|
226
|
+
if(looksProjectRootRelative || !isAbsoluteFsPath){
|
|
227
|
+
const trimmed = dbPath.replace(/^[/\\]+/, '');
|
|
228
|
+
dbPath = path.join(file.rootFolder, trimmed);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
options.completeConnection = dbPath;
|
|
232
|
+
var dbDirectory = path.dirname(options.completeConnection);
|
|
219
233
|
|
|
220
234
|
if (!fs.existsSync(dbDirectory)){
|
|
221
|
-
fs.mkdirSync(dbDirectory);
|
|
235
|
+
fs.mkdirSync(dbDirectory, { recursive: true });
|
|
222
236
|
}
|
|
223
237
|
|
|
224
238
|
this.db = this.__SQLiteInit(options, "better-sqlite3");
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"app-root-path": "^3.1.0",
|
|
10
10
|
"better-sqlite3": "^12.4.1"
|
|
11
11
|
},
|
|
12
|
-
"version": "0.2.
|
|
12
|
+
"version": "0.2.24",
|
|
13
13
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
14
14
|
"homepage": "https://github.com/Tailor/MasterRecord#readme",
|
|
15
15
|
"repository": {
|