Forked from agodin3z/sequelize-migration-file-generator.js
Created
August 18, 2020 19:22
-
-
Save timbuckley/6fff7631094e6697347791e544b27949 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
#!/usr/bin/env node | |
const fs = require('fs'); | |
const models = require('../models'); | |
for (const model in models) { | |
const tableName = models[model].tableName; | |
let defaultValue = ''; | |
let onUpdate = ''; | |
let type = ''; | |
let template = `'use strict'; | |
module.exports = { | |
up: (queryInterface, Sequelize) => { | |
return queryInterface.createTable('${tableName}', {\n`; | |
const attributes = models[model].tableAttributes; | |
for (const column in attributes) { | |
delete attributes[column].Model; | |
delete attributes[column].fieldName; | |
delete attributes[column].field; | |
delete attributes[column]._modelAttribute; | |
template += ` ${column}: {\n`; | |
if (attributes[column].defaultValue) { | |
if ((attributes[column].defaultValue).toString() === 'NOW') { | |
defaultValue = 'Sequelize.literal(\'CURRENT_TIMESTAMP\')'; | |
} else { | |
defaultValue = (attributes[column].defaultValue).toString(); | |
} | |
} | |
if (attributes[column].onUpdate) { | |
if ((attributes[column].onUpdate).toString() === 'NOW') { | |
defaultValue = 'Sequelize.literal(\'CURRENT_TIMESTAMP\')'; | |
onUpdate = 'Sequelize.literal(\'CURRENT_TIMESTAMP\')'; | |
} | |
} | |
if (attributes[column].type) { | |
let dataType = (attributes[column].type).toString(); | |
dataType = dataType.replace('DATETIME', 'DATE'); | |
dataType = dataType.replace('VARCHAR', 'STRING'); | |
dataType = dataType.split(' ').join('.'); | |
type = `Sequelize.${dataType}`; | |
} | |
for (const property in attributes[column]) { | |
if (property.startsWith('_')) { | |
delete attributes[column][property]; | |
} | |
if (property === 'type') { | |
template += ` type: ${type},\n`; | |
} else if (property === 'defaultValue') { | |
template += ` defaultValue: ${defaultValue},\n`; | |
} else if (property === 'onUpdate') { | |
template += ` defaultValue: ${defaultValue},\n`; | |
template += ` onUpdate: ${onUpdate},\n`; | |
} else { | |
if (typeof attributes[column][property] === 'object') { | |
template += ` ${property}: {\n`; | |
for (const p in attributes[column][property]) { | |
template += ` ${p}: '${attributes[column][property][p]}',\n`; | |
} | |
template += ' },\n'; | |
} else { | |
if (attributes[column][property] === true || attributes[column][property] === false) { | |
template += ` ${property}: ${attributes[column][property]},\n`; | |
} else { | |
template += ` ${property}: '${attributes[column][property]}',\n`; | |
} | |
} | |
} | |
} | |
template += ' },\n'; | |
} | |
template += ` }); | |
},\n | |
down: (queryInterface, Sequelize) => { | |
return queryInterface.dropTable('${tableName}'); | |
}, | |
};\n`; | |
if (models[model].tableName !== undefined) { | |
const now = new Date(); | |
fs.writeFileSync('./tmp/' + now.toISOString().replace(/[^\d]/g, '').slice(0, -3) + '-create-' + models[model].tableName + '.js', template); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment