2025-04-28 10:17:10 +00:00

90 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 rooms = sequelize.define(
'rooms',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
price_per_hour: {
type: DataTypes.DECIMAL,
},
type: {
type: DataTypes.ENUM,
values: ['small', 'medium', 'large'],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
rooms.associate = (db) => {
db.rooms.belongsToMany(db.bookings, {
as: 'bookings',
foreignKey: {
name: 'rooms_bookingsId',
},
constraints: false,
through: 'roomsBookingsBookings',
});
db.rooms.belongsToMany(db.bookings, {
as: 'bookings_filter',
foreignKey: {
name: 'rooms_bookingsId',
},
constraints: false,
through: 'roomsBookingsBookings',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.rooms.hasMany(db.bookings, {
as: 'bookings_room',
foreignKey: {
name: 'roomId',
},
constraints: false,
});
//end loop
db.rooms.belongsTo(db.users, {
as: 'createdBy',
});
db.rooms.belongsTo(db.users, {
as: 'updatedBy',
});
};
return rooms;
};