Forked from manuelbieh/sequelize-migration-file-generator.js
Created
July 25, 2017 00:22
-
-
Save shomanishikawa/4e15abc0ec8b515042e2f042f584dd6f to your computer and use it in GitHub Desktop.
Creates migration files for existing sequelize models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment