Skip to content

Instantly share code, notes, and snippets.

@manuelbieh
Created January 14, 2016 16:42
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save manuelbieh/606710b003b5fe448100 to your computer and use it in GitHub Desktop.
Save manuelbieh/606710b003b5fe448100 to your computer and use it in GitHub Desktop.
Creates migration files for existing sequelize models
import * as models from "models";
import fs from "fs";
for(let model in models) {
let attributes = models[model].attributes;
for(let column in attributes) {
delete attributes[column].Model;
delete attributes[column].fieldName;
delete attributes[column].field;
for(let property in attributes[column]) {
if(property.startsWith('_')) {
delete attributes[column][property];
}
}
}
let schema = JSON.stringify(attributes, null, 2);
let tableName = models[model].tableName;
let template = `'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('${tableName}', ${schema});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable(${tableName});
}
};`
if(models[model].tableName !== undefined) {
fs.writeFileSync('./tmp/' + models[model].tableName + '.js', template);
}
};
@agodin3z
Copy link

Thanks, you saved my day! ^^

@ProMasoud
Copy link

It's not working with v5 :(

@theclabs
Copy link

It's not working with v5 :(

can post here you error message and stack to check it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment