Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save agodin3z/4b87a62863bf676cb6a8420fec0cd4b8 to your computer and use it in GitHub Desktop.
Save agodin3z/4b87a62863bf676cb6a8420fec0cd4b8 to your computer and use it in GitHub Desktop.
Creates migration files for existing sequelize models [custom]
#!/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 (
JSON.stringify(attributes[column].defaultValue)
.toString()
.match(/NOW/i) ||
JSON.stringify(attributes[column].defaultValue)
.toString()
.match(/CURRENT_TIMESTAMP/i)
) {
defaultValue = "Sequelize.literal('CURRENT_TIMESTAMP')";
} else {
defaultValue = attributes[column].defaultValue.toString();
}
}
if (attributes[column].onUpdate) {
if (
JSON.stringify(attributes[column].onUpdate).toString().match(/NOW/i) ||
JSON.stringify(attributes[column].onUpdate)
.toString()
.match(/CURRENT_TIMESTAMP/i)
) {
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.toString().match(/^_/i)) {
delete attributes[column][property];
}
if (property === 'type') {
template += ` type: ${type},\n`;
} else if (property === 'defaultValue') {
if (column === 'createdAt') {
template += ` defaultValue: ${defaultValue},\n`;
} else {
template += ` defaultValue: '${attributes[column].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]) {
if (typeof attributes[column][property][p] === 'object') {
template += ` ${p}: {\n`;
for (const sp in attributes[column][property][p]) {
template += ` ${sp}: '${attributes[column][property][p][sp]}',\n`;
}
template += ' },\n';
} else {
if (typeof attributes[column][property][p] === 'boolean') {
template += ` ${p}: ${attributes[column][property][p]},\n`;
} else {
template += ` ${p}: '${attributes[column][property][p]}',\n`;
}
}
}
template += ' },\n';
} else {
if (typeof attributes[column][property] === 'boolean') {
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,
);
}
}
@agodin3z
Copy link
Author

Tested with models generated with Sequelize 5.5.1 and dialect mysql 🚀

@epicbytes
Copy link

don`t use moment for this.
you can use now.toISOString().replace(/[^\d]/g,"").slice(0,-3)

@agodin3z
Copy link
Author

agodin3z commented May 3, 2020

don`t use moment for this.
you can use now.toISOString().replace(/[^\d]/g,"").slice(0,-3)

Fixed 😅
Thanks!

@MkDierz
Copy link

MkDierz commented Apr 14, 2023

hey does this work on relational model like a join table perhaps?

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