In the very initial days of my work in Node JS if faced this issue. Though Sequelize provides a great ORM for MySQL, the association within the models is a bit tricky. You can find more about associations here
Before getting any further, if you want to get a setup for fully functional code setup here it is
Preface
For the sake of understanding, let's consider two Models please inside the model directory, models/author.js and models/post.js. Models will look as follows respectively.
Author Model (models/author.js)
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Author extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
Author.init({
slug: DataTypes.STRING,
name: DataTypes.STRING,
}, {
sequelize,
modelName: 'Author',
tableName: 'authors',
});
return Author;
};
Post Model (models/post.js)
'use strict';
const {
Model,
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Post extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
this.belongsTo(models.Author, {as: 'Author'});
}
}
Post.init({
slug: DataTypes.STRING,
title: DataTypes.STRING,
excerpt: DataTypes.STRING
}, {
sequelize,
modelName: 'Post',
tableName: 'posts',
});
return Post;
};
As shown in the post model, a belongs to association is made between the Post and the author. But as the comment suggests, the associate method is not a part of the Sequelize lifecycle. we have to call it manually.
To achieve that, we need to create models/index.js with the following content.
models/index.js
'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/database.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;
Ones you are done with this you can access all you models vie models/index.js
Accessing Models
const {Post, Author} = require('../../models');
You can find more about how to use association in accessing data here
GitHub Repo for the above code