I have a Sequelize migration script (built off sequelize-cli
's initial migration script generated from running sequelize-cli
) that dynamically creates tables based on a list of ~100 hardcoded table names imported from a .json file. Thus, it will be tedious and error-prone to hardcode the Sequelize model definitions by hand.
Question: How can we dynamically generate the Sequelize models by reading from the same .json file? I am mainly stuck how we should return multiple Sequelize models using module.exports
, based on the model definition file auto-generated from running sequelize-cli model:generate --name Base ...
. There appears to be a special script models/index.js
for reading the model definition file models/base.js
.
models/base.js
'use strict';
const universe = require('../config/universe.json');
const generateModel = (sequelize, DataTypes, tableName) => {
const schema = {
name: DataTypes.STRING,
power: DataTypes.DOUBLE,
};
const options = {
timestamps: false,
};
const Model = sequelize.define(tableName, schema, options);
Model.associate = function(models) {
// associations can be defined here
}
return Model;
}
module.exports = (sequelize, DataTypes) => {
models = []
for (let planet in universe) {
for (const species of universe[planet]) {
models.push(generateModel(species));
}
}
return models;
};
app.js
Attempting to load the dynamically created model Human
const Human = require('./models').base.human;
Human.create({name: 'Tom', power: 1.23});
config/universe.json
Example hardcoded data file
{
"earth": [
'human',
'cat',
'dog'
]
}
models/index.js
This file is auto-generated after running sequelize-cli init
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;