masterrecord 0.2.8 → 0.2.9

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 (2) hide show
  1. package/Migrations/cli.js +78 -0
  2. package/package.json +1 -1
package/Migrations/cli.js CHANGED
@@ -218,6 +218,84 @@ program.option('-V', 'output the version');
218
218
  });
219
219
 
220
220
 
221
+ program
222
+ .command('update-database-down <contextFileName>')
223
+ .alias('udd')
224
+ .description('Run the latest migration down method for the given context')
225
+ .action(function(contextFileName){
226
+ var executedLocation = process.cwd();
227
+ contextFileName = contextFileName.toLowerCase();
228
+ var migration = new Migration();
229
+ try{
230
+ var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
231
+ var files = globSearch.sync(search, executedLocation);
232
+ var file = files && files[0];
233
+ if(!file){
234
+ console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
235
+ return;
236
+ }
237
+ var contextSnapshot;
238
+ try{
239
+ contextSnapshot = require(file);
240
+ }catch(_){
241
+ console.log(`Error - Cannot read context snapshot at '${file}'.`);
242
+ return;
243
+ }
244
+ var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
245
+ var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
246
+ if(!(migrationFiles && migrationFiles.length)){
247
+ console.log("Error - Cannot read or find migration file");
248
+ return;
249
+ }
250
+ // Sort and select latest
251
+ var mFiles = migrationFiles.slice().sort(function(a, b){
252
+ return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
253
+ });
254
+ var latestFile = mFiles[mFiles.length - 1];
255
+
256
+ // Prepare context and table object
257
+ let ContextCtor;
258
+ try{
259
+ ContextCtor = require(contextSnapshot.contextLocation);
260
+ }catch(_){
261
+ console.log(`Error - Cannot load Context file at '${contextSnapshot.contextLocation}'.`);
262
+ return;
263
+ }
264
+ var contextInstance;
265
+ try{
266
+ contextInstance = new ContextCtor();
267
+ }catch(_){
268
+ console.log(`Error - Failed to construct Context from '${contextSnapshot.contextLocation}'.`);
269
+ return;
270
+ }
271
+ var cleanEntities = migration.cleanEntities(contextInstance.__entities);
272
+ var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
273
+
274
+ var MigCtor = require(latestFile);
275
+ var migInstance = new MigCtor(ContextCtor);
276
+ if(typeof migInstance.down === 'function'){
277
+ migInstance.down(tableObj);
278
+ }else{
279
+ console.log(`Warning - Migration '${path.basename(latestFile)}' has no down method; skipping.`);
280
+ }
281
+
282
+ // Update snapshot
283
+ var snap = {
284
+ file : contextSnapshot.contextLocation,
285
+ executedLocation : executedLocation,
286
+ context : contextInstance,
287
+ contextEntities : cleanEntities,
288
+ contextFileName: contextFileName
289
+ }
290
+ migration.createSnapShot(snap);
291
+ console.log("database updated");
292
+
293
+ }catch (e){
294
+ console.log("Error - Cannot read or find file ", e);
295
+ }
296
+ });
297
+
298
+
221
299
  program
222
300
  .command('update-database-restart <contextFileName>')
223
301
  .alias('udr')
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.8",
12
+ "version": "0.2.9",
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": {