30233/backend/src/db/models/restaurants.js
2025-03-27 10:49:19 +00:00

100 lines
2.1 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 restaurants = sequelize.define(
'restaurants',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
address: {
type: DataTypes.TEXT,
},
cuisine: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
restaurants.associate = (db) => {
db.restaurants.belongsToMany(db.menu_items, {
as: 'menu_items',
foreignKey: {
name: 'restaurants_menu_itemsId',
},
constraints: false,
through: 'restaurantsMenu_itemsMenu_items',
});
db.restaurants.belongsToMany(db.menu_items, {
as: 'menu_items_filter',
foreignKey: {
name: 'restaurants_menu_itemsId',
},
constraints: false,
through: 'restaurantsMenu_itemsMenu_items',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.restaurants.hasMany(db.menu_items, {
as: 'menu_items_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.orders, {
as: 'orders_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
db.restaurants.hasMany(db.reviews, {
as: 'reviews_restaurant',
foreignKey: {
name: 'restaurantId',
},
constraints: false,
});
//end loop
db.restaurants.belongsTo(db.users, {
as: 'createdBy',
});
db.restaurants.belongsTo(db.users, {
as: 'updatedBy',
});
};
return restaurants;
};