Created
June 28, 2022 23:37
-
-
Save tatsuyasusukida/4ab46713fd589886bc26f25e0a7a6504 to your computer and use it in GitHub Desktop.
π How to migrate from CJS to ESM with Sequelize: CJS part
This file contains 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
module.exports = function (sequelize, DataTypes) { | |
const article = sequelize.define('article', {}) | |
article.associate = (models) => { | |
article.belongsTo(models.user) | |
} | |
return article | |
} |
This file contains 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
{ | |
"development": { | |
"username": "tech_blog_user", | |
"password": "tech_blog_pass", | |
"database": "tech_blog_db", | |
"host": "127.0.0.1", | |
"dialect": "mysql" | |
}, | |
"test": { | |
"username": "tech_blog_user", | |
"password": "tech_blog_pass", | |
"database": "tech_blog_db", | |
"host": "127.0.0.1", | |
"dialect": "mysql" | |
}, | |
"production": { | |
"username": "tech_blog_user", | |
"password": "tech_blog_pass", | |
"database": "tech_blog_db", | |
"host": "127.0.0.1", | |
"dialect": "mysql" | |
} | |
} |
This file contains 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
'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 = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); | |
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; |
This file contains 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
const models = require('./models-cjs') | |
main() | |
async function main () { | |
try { | |
await models.sequelize.sync({force: true}) | |
await models.sequelize.close() | |
} catch (err) { | |
console.error(err) | |
} | |
} |
This file contains 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
module.exports = function (sequelize, DataTypes) { | |
const user = sequelize.define('user', {}) | |
return user | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment