31168/backend/src/db/models/medications.js
2025-05-02 08:44:44 +00:00

88 lines
1.9 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 medications = sequelize.define(
'medications',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
medications.associate = (db) => {
db.medications.belongsToMany(db.prescriptions, {
as: 'prescriptions',
foreignKey: {
name: 'medications_prescriptionsId',
},
constraints: false,
through: 'medicationsPrescriptionsPrescriptions',
});
db.medications.belongsToMany(db.prescriptions, {
as: 'prescriptions_filter',
foreignKey: {
name: 'medications_prescriptionsId',
},
constraints: false,
through: 'medicationsPrescriptionsPrescriptions',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.medications.hasMany(db.prescriptions, {
as: 'prescriptions_medication',
foreignKey: {
name: 'medicationId',
},
constraints: false,
});
//end loop
db.medications.belongsTo(db.units, {
as: 'units',
foreignKey: {
name: 'unitsId',
},
constraints: false,
});
db.medications.belongsTo(db.users, {
as: 'createdBy',
});
db.medications.belongsTo(db.users, {
as: 'updatedBy',
});
};
return medications;
};