mirror of
https://github.com/harish2704/sequelize-migration-generator.git
synced 2026-09-10 06:31:07 +00:00
Cleanup
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
|||||||
|
var path = require('path');
|
||||||
|
var ect = require('ect')();
|
||||||
|
var fs = require( 'fs');
|
||||||
|
var Classes = require( '../index');
|
||||||
|
|
||||||
|
var log = console.log.bind( console, 'SMG: ' );
|
||||||
|
var TEMPLATE = path.resolve( __dirname + '/../data/migration-file.js.ect' );
|
||||||
|
var Model = Classes.Model;
|
||||||
|
var Field = Classes.Field;
|
||||||
|
var modelsDir = process.argv[2];
|
||||||
|
var migrationsDir = process.argv[3];
|
||||||
|
|
||||||
|
|
||||||
|
function getAbsPath( relPath ){
|
||||||
|
return path.resolve( relPath );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getTimeStamp( i ){
|
||||||
|
return new Date( Date.now() + i ).toISOString().replace(/[-:.TZ]/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function genMigration(model, i ){
|
||||||
|
log( 'Generating migration for ', model );
|
||||||
|
var m = new Model( model );
|
||||||
|
var out = ect.render( TEMPLATE, { model: m, fieldOpts: Field.opts });
|
||||||
|
var timestamp = getTimeStamp( i );
|
||||||
|
var fname = timestamp + '-create_table_' + model.name + '_mi-generated.js';
|
||||||
|
|
||||||
|
fs.writeFileSync( path.join( migrationsDir, fname ), out );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function createDir( pathName ){
|
||||||
|
if( !fs.existsSync( pathName ) ){
|
||||||
|
fs.mkdirSync( pathName );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function main(){
|
||||||
|
modelsDir = getAbsPath( modelsDir );
|
||||||
|
migrationsDir = getAbsPath( migrationsDir );
|
||||||
|
createDir( migrationsDir );
|
||||||
|
log( 'modelsDir', modelsDir );
|
||||||
|
var models = require( modelsDir );
|
||||||
|
|
||||||
|
Object.keys( models )
|
||||||
|
.forEach( function( modelName, i){
|
||||||
|
if( [ 'Sequelize', 'sequelize'].indexOf(modelName) === -1 ){
|
||||||
|
genMigration( models[ modelName ], i );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if( !( modelsDir && migrationsDir )){
|
||||||
|
console.log( 'Usage: sequelize-migration-generator <models_directory> <migration_directory>');
|
||||||
|
} else {
|
||||||
|
main();
|
||||||
|
}
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var appRoot = __dirname + '/';
|
|
||||||
var pwd = process.env.PWD + '/';
|
|
||||||
|
|
||||||
if( typeof exports == 'undefined' ){ global.exports = {}; }
|
|
||||||
|
|
||||||
|
|
||||||
function Val( val ){
|
function Val( val ){
|
||||||
this.val = val;
|
this.val = val;
|
||||||
@@ -80,6 +75,10 @@ function Field( attr ){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Field.create = function( attr ){
|
||||||
|
return new Field( attr );
|
||||||
|
}
|
||||||
|
|
||||||
Field.opts = [
|
Field.opts = [
|
||||||
'autoIncrement',
|
'autoIncrement',
|
||||||
'allowNull',
|
'allowNull',
|
||||||
@@ -93,32 +92,25 @@ exports.Field = Field;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Model( model ){
|
function Model( model ){
|
||||||
this.tableName = model.tableName;
|
this.tableName = model.tableName;
|
||||||
this.uniqueKeys = model.uniqueKeys;
|
this.uniqueKeys = model.uniqueKeys;
|
||||||
this.fields = Object.keys( model.attributes ).map( function( key ){
|
this.fields = Model.getReOrderedFields( model.attributes );
|
||||||
return new Field( model.attributes[key] );
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Model.getReOrderedFields = function( attsObj ){
|
||||||
|
var fieldNames = Object.keys( attsObj );
|
||||||
|
var indexOfIdField = fieldNames.indexOf( 'id' );
|
||||||
|
if( indexOfIdField > -1 ){
|
||||||
|
fieldNames = fieldNames.splice( indexOfIdField, 1 ).concat( fieldNames );
|
||||||
|
}
|
||||||
|
return fieldNames.map( function( name ){ return Field.create( attsObj[name] ); } );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
exports.Model = Model;
|
exports.Model = Model;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if( require.main == module ){
|
|
||||||
var modelsDir = process.argv[2];
|
|
||||||
var models = require( pwd + modelsDir );
|
|
||||||
var ect = require('ect')();
|
|
||||||
var fs = require( 'fs');
|
|
||||||
var modelNames = Object.keys( models ).filter( function(v){ return (['Sequelize', 'sequelize' ].indexOf(v) === -1) } );
|
|
||||||
modelNames.forEach( function(modelName, i ){
|
|
||||||
var m = new Model( models[modelName] );
|
|
||||||
var out = ect.render( appRoot + '../data/migration-file.js.ect', { model: m, fieldOpts: Field.opts });
|
|
||||||
var timestamp = new Date( Date.now() + i ).toISOString().replace(/[-:.TZ]/g, '');
|
|
||||||
fs.writeFileSync( pwd + 'migrations/' + timestamp + '-create_table_' + modelName + '_mi-generated.js', out );
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user