Forked from manuelbieh/sequelize-schema-file-generator.js
Last active
October 18, 2019 20:32
-
-
Save giacomorebonato/ec7b3b7d258cb586cd596ba920a3e22f to your computer and use it in GitHub Desktop.
Automatically generates migration files from your 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 db from './models' | |
import del from 'del' | |
import fs from 'fs' | |
import path from 'path' | |
let { sequelize } = db | |
const migrations_path = path.join(__dirname, '/migrations/') | |
del([migrations_path]) | |
.then(() => { | |
fs.mkdirSync(migrations_path) | |
Object.keys(db).forEach((key) => { | |
let model = db[key] | |
if (!model.attributes) { return } | |
let { attributes, tableName } = model | |
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] | |
} | |
} | |
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) | |
let indexes = ['\n'] | |
if (model.options.indexes.length) { | |
model.options.indexes.forEach((obj) => { | |
indexes.push('.then(() => {') | |
indexes.push(' return queryInterface.addIndex(') | |
indexes.push(` '${tableName}',`) | |
indexes.push(` ['${obj.fields.join("','")}']`) | |
let 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') | |
let template = ( | |
`'use strict' | |
module.exports = { | |
up: (queryInterface, Sequelize) => { | |
return queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0').then(() => { | |
return queryInterface.createTable('${tableName}', ${schema})})${indexes.join('\n')}.then(() => { | |
return queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 1') | |
}) | |
}, | |
down: (queryInterface, Sequelize) => { | |
return queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0').then(() => { | |
return queryInterface.dropTable('${tableName}') | |
}).then(() => { | |
return queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 1') | |
}) | |
} | |
}` | |
) | |
let d = new Date() | |
let 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('') + `-${model.tableName}` | |
fs.writeFileSync(path.join(migrations_path, `./${filename}.js`), template) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
have this error in node 6.6.0
(function (exports, require, module, __filename, __dirname) { import db from './models'
SyntaxError: Unexpected token import