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 characters = sequelize.define( 'characters', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, personality: { type: DataTypes.TEXT, }, background: { type: DataTypes.TEXT, }, greeting_message: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); characters.associate = (db) => { db.characters.belongsToMany(db.tags, { as: 'tags', foreignKey: { name: 'characters_tagsId', }, constraints: false, through: 'charactersTagsTags', }); db.characters.belongsToMany(db.tags, { as: 'tags_filter', foreignKey: { name: 'characters_tagsId', }, constraints: false, through: 'charactersTagsTags', }); db.characters.belongsToMany(db.categories, { as: 'categories', foreignKey: { name: 'characters_categoriesId', }, constraints: false, through: 'charactersCategoriesCategories', }); db.characters.belongsToMany(db.categories, { as: 'categories_filter', foreignKey: { name: 'characters_categoriesId', }, constraints: false, through: 'charactersCategoriesCategories', }); db.characters.belongsToMany(db.conversations, { as: 'conversations', foreignKey: { name: 'characters_conversationsId', }, constraints: false, through: 'charactersConversationsConversations', }); db.characters.belongsToMany(db.conversations, { as: 'conversations_filter', foreignKey: { name: 'characters_conversationsId', }, constraints: false, through: 'charactersConversationsConversations', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.characters.hasMany(db.conversations, { as: 'conversations_character', foreignKey: { name: 'characterId', }, constraints: false, }); //end loop db.characters.hasMany(db.file, { as: 'avatar', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.characters.getTableName(), belongsToColumn: 'avatar', }, }); db.characters.belongsTo(db.users, { as: 'createdBy', }); db.characters.belongsTo(db.users, { as: 'updatedBy', }); }; return characters; };