masterrecord 0.2.16 → 0.2.17

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 CHANGED
@@ -569,21 +569,23 @@ program.option('-V', 'output the version');
569
569
  var executedLocation = process.cwd();
570
570
  try{
571
571
  // Find all context snapshots and run update per snapshot (avoids unrelated framework contexts)
572
- var snapshotFiles = globSearch.sync(path.join(executedLocation, '**', '*_contextSnapShot.json'), executedLocation);
572
+ var snapshotFiles = globSearch.sync('**/*_contextSnapShot.json', executedLocation);
573
573
  if(!(snapshotFiles && snapshotFiles.length)){
574
574
  console.log('No context snapshots found. Run enable-migrations for each context first.');
575
575
  return;
576
576
  }
577
577
  // Group snapshots by context name (case-insensitive) and pick best per group
578
578
  var groups = {};
579
- for(const snapFile of snapshotFiles){
579
+ for(const snapRel of snapshotFiles){
580
+ const snapFile = path.resolve(executedLocation, snapRel);
580
581
  let cs;
581
582
  try{ cs = require(snapFile); }catch(_){ continue; }
582
583
  const nameFromPath = path.basename(snapFile).replace(/_contextSnapShot\.json$/i, '').toLowerCase();
583
584
  const ctxName = (cs && cs.contextLocation)
584
585
  ? path.basename(cs.contextLocation).replace(/\.js$/i, '').toLowerCase()
585
586
  : nameFromPath;
586
- const migs = globSearch.sync(path.join(cs.migrationFolder, '**', '*_migration.js'), cs.migrationFolder) || [];
587
+ const migRel = globSearch.sync('**/*_migration.js', cs.migrationFolder) || [];
588
+ const migs = migRel.map(f => path.resolve(cs.migrationFolder, f));
587
589
  if(!groups[ctxName]) groups[ctxName] = [];
588
590
  groups[ctxName].push({ snapFile, cs, ctxName, migs });
589
591
  }
@@ -5,6 +5,7 @@ var fs = require('fs');
5
5
  var diff = require("deep-object-diff");
6
6
  var MigrationTemplate = require("./migrationTemplate");
7
7
  var globSearch = require("glob");
8
+ var path = require('path');
8
9
 
9
10
  // https://blog.tekspace.io/code-first-multiple-db-context-migration/
10
11
 
@@ -203,28 +204,30 @@ class Migrations{
203
204
  }
204
205
 
205
206
  createSnapShot(snap){
206
-
207
- var dbFolder = `${snap.executedLocation}/db`;
207
+ // Place migrations alongside the Context file by default:
208
+ // <ContextDir>/db/migrations/<context>_contextSnapShot.json
209
+ const contextDir = path.dirname(snap.file);
210
+ const dbFolder = path.join(contextDir, 'db');
208
211
  if (!fs.existsSync(dbFolder)){
209
- fs.mkdirSync(dbFolder);
212
+ fs.mkdirSync(dbFolder, { recursive: true });
210
213
  }
211
214
 
212
- var migrationsDirectory = `${snap.executedLocation}/db/migrations`;
215
+ const migrationsDirectory = path.join(dbFolder, 'migrations');
213
216
  if (!fs.existsSync(migrationsDirectory)){
214
- fs.mkdirSync(migrationsDirectory);
217
+ fs.mkdirSync(migrationsDirectory, { recursive: true });
215
218
  }
216
219
 
220
+ const snapshotPath = path.join(migrationsDirectory, `${snap.contextFileName}_contextSnapShot.json`);
217
221
  var content = {
218
222
  contextLocation: snap.file,
219
- migrationFolder: `${snap.executedLocation}/db/migrations`,
220
- snapShotLocation: `${snap.executedLocation}/db/migrations/${snap.contextFileName}_contextSnapShot.json`,
223
+ migrationFolder: migrationsDirectory,
224
+ snapShotLocation: snapshotPath,
221
225
  schema : snap.contextEntities
222
226
  };
223
227
 
224
228
  const jsonContent = JSON.stringify(content, null, 2);
225
229
  try{
226
- // will replace the whole file if it exist
227
- fs.writeFileSync(`${migrationsDirectory}/${snap.contextFileName}_contextSnapShot.json`, jsonContent);
230
+ fs.writeFileSync(snapshotPath, jsonContent);
228
231
  }catch (e){
229
232
  console.log("Cannot write file ", e);
230
233
  }
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.16",
12
+ "version": "0.2.17",
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": {