2025-04-24 22:36:04 +00:00

84 lines
1.7 KiB
JavaScript

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 boxes = sequelize.define(
'boxes',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
boxes.associate = (db) => {
db.boxes.belongsToMany(db.journeys, {
as: 'journeys',
foreignKey: {
name: 'boxes_journeysId',
},
constraints: false,
through: 'boxesJourneysJourneys',
});
db.boxes.belongsToMany(db.journeys, {
as: 'journeys_filter',
foreignKey: {
name: 'boxes_journeysId',
},
constraints: false,
through: 'boxesJourneysJourneys',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.boxes.hasMany(db.journeys, {
as: 'journeys_box',
foreignKey: {
name: 'boxId',
},
constraints: false,
});
//end loop
db.boxes.belongsTo(db.users, {
as: 'owner',
foreignKey: {
name: 'ownerId',
},
constraints: false,
});
db.boxes.belongsTo(db.users, {
as: 'createdBy',
});
db.boxes.belongsTo(db.users, {
as: 'updatedBy',
});
};
return boxes;
};