Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mubaidr/1511d49b73c55d0288c1e1deb3c4e573 to your computer and use it in GitHub Desktop.
Save mubaidr/1511d49b73c55d0288c1e1deb3c4e573 to your computer and use it in GitHub Desktop.
Automatically generates migration files from your sequelize models
const fs = require('fs')
const { sequelize } = require('./api/db/models')
const { models } = sequelize
function toSnakeCase(str, upper) {
if (str.split().every(s => s.toLowerCase() === s)) return str
const snake = str.replace(/([A-Z])/g, $1 => `_${$1.toLowerCase()}`)
return upper ? snake.toLowerCase() : snake
}
Object.keys(models).forEach(model => {
const { attributes } = models[model]
Object.keys(attributes).forEach(column => {
delete attributes[column].Model
delete attributes[column].fieldName
delete attributes[column].field
// console.log(column, toSnakeCase(column))
attributes[column].field = `${toSnakeCase(column, true)}`
Object.keys(attributes[column]).forEach(property => {
if (property.startsWith('_')) {
delete attributes[column][property]
}
})
if (typeof attributes[column].type !== 'undefined') {
if (
typeof attributes[column].type.options !== 'undefined' &&
typeof attributes[column].type.options.toString === 'function'
) {
attributes[column].type.options = attributes[
column
].type.options.toString(sequelize)
}
if (typeof attributes[column].type.toString === 'function') {
attributes[column].type = attributes[column].type.toString(sequelize)
}
}
})
let schema = JSON.stringify(attributes, null, 4)
const { tableName } = models[model]
const indexes = ['\n']
if (models[model].options.indexes.length) {
models[model].options.indexes.forEach(obj => {
indexes.push(' .then(() => {')
indexes.push(' return queryInterface.addIndex(')
indexes.push(` '${tableName}',`)
indexes.push(` ['${obj.fields.join("','")}']`)
const opts = {}
if (obj.name) {
opts.indexName = obj.name
}
if (obj.unique === true) {
opts.indicesType = 'UNIQUE'
}
if (obj.method === true) {
opts.indexType = obj.method
}
if (Object.keys(opts).length) {
indexes.push(` , ${JSON.stringify(opts)}`)
}
indexes.push(' )')
indexes.push(' })')
})
}
schema = schema
.split('\n')
.map(line => ` ${line}`)
.join('\n')
const template = `module.exports = {
up: async queryInterface => {
await queryInterface.createTable('${tableName}', ${schema})
},
down: async queryInterface => {
await queryInterface.dropTable('${tableName}')
}
}
`
// const d = new Date()
// const filename = `${[
// d.getFullYear(),
// d.getMonth() + 1,
// d.getDate(),
// d.getHours(),
// d.getMinutes(),
// d.getSeconds(),
// ]
// .map(num => (num <= 60 && (num + 100).toString().substring(1)) || num)
// .join('')}-${models[model].tableName}`
fs.writeFileSync(`./migrations/000-${models[model].tableName}.js`, template)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment