Created
June 28, 2022 23:39
-
-
Save tatsuyasusukida/1e16db81ca8b38e8d77ccd88cb121f31 to your computer and use it in GitHub Desktop.
π How to migrate from CJS to ESM with Sequelize: ESM 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
export default function article (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
import {Sequelize, DataTypes} from 'sequelize' | |
import article from './article.mjs' | |
import user from './user.mjs' | |
const sequelize = new Sequelize(process.env.DB_URL) | |
const models = { | |
article: article(sequelize, DataTypes), | |
user: user(sequelize, DataTypes), | |
} | |
Object.keys(models).forEach(key => { | |
if (models[key].associate) { | |
models[key].associate(models) | |
} | |
}) | |
models.sequelize = sequelize | |
export default models |
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
import models from './models-esm/index.mjs' | |
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
export default function user (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