const config = require('../../config'); const providers = config.providers; const crypto = require('crypto'); const bcrypt = require('bcrypt'); const moment = require('moment'); module.exports = function(sequelize, DataTypes) { const posts = sequelize.define( 'posts', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, title: { type: DataTypes.TEXT, }, slug: { type: DataTypes.TEXT, }, excerpt: { type: DataTypes.TEXT, }, content: { type: DataTypes.TEXT, }, status: { type: DataTypes.ENUM, values: [ "draft", "published", "archived" ], }, published_at: { type: DataTypes.DATE, }, updated_on: { type: DataTypes.DATE, }, views: { type: DataTypes.INTEGER, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); posts.associate = (db) => { db.posts.belongsToMany(db.categories, { as: 'categories', foreignKey: { name: 'posts_categoriesId', }, constraints: false, through: 'postsCategoriesCategories', }); db.posts.belongsToMany(db.categories, { as: 'categories_filter', foreignKey: { name: 'posts_categoriesId', }, constraints: false, through: 'postsCategoriesCategories', }); db.posts.belongsToMany(db.tags, { as: 'tags', foreignKey: { name: 'posts_tagsId', }, constraints: false, through: 'postsTagsTags', }); db.posts.belongsToMany(db.tags, { as: 'tags_filter', foreignKey: { name: 'posts_tagsId', }, constraints: false, through: 'postsTagsTags', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.posts.hasMany(db.comments, { as: 'comments_post', foreignKey: { name: 'postId', }, constraints: false, }); //end loop db.posts.belongsTo(db.users, { as: 'author', foreignKey: { name: 'authorId', }, constraints: false, }); db.posts.hasMany(db.file, { as: 'featured_image', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.posts.getTableName(), belongsToColumn: 'featured_image', }, }); db.posts.belongsTo(db.users, { as: 'createdBy', }); db.posts.belongsTo(db.users, { as: 'updatedBy', }); }; return posts; };