Skip to content

Instantly share code, notes, and snippets.

@derduskenga
Last active July 29, 2020 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derduskenga/6745e8347276d916d4068637c2184a73 to your computer and use it in GitHub Desktop.
Save derduskenga/6745e8347276d916d4068637c2184a73 to your computer and use it in GitHub Desktop.
'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) {
// define association here
Post.belongsTo(models.User);
//post many to many association with Tag
Post.belongsToMany(models.Tag,{
through: models.PostTag,
foreignKey:'tag_id'
});
}
};
Post.init({
post_title: DataTypes.STRING,
post_text: DataTypes.TEXT,
status: DataTypes.BOOLEAN,
userId: {
type: DataTypes.INTEGER,
references:{
model:'User',
key:'id'
}
}
}, {
sequelize,
modelName: 'Post',
});
return Post;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment