96 lines
1.8 KiB
JavaScript
96 lines
1.8 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 grounds = sequelize.define(
|
|
'grounds',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
name: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
address: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
price: {
|
|
type: DataTypes.DECIMAL,
|
|
},
|
|
|
|
city: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
},
|
|
);
|
|
|
|
grounds.associate = (db) => {
|
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
|
|
|
db.grounds.hasMany(db.bookings, {
|
|
as: 'bookings_ground',
|
|
foreignKey: {
|
|
name: 'groundId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.grounds.hasMany(db.feedbacks, {
|
|
as: 'feedbacks_ground',
|
|
foreignKey: {
|
|
name: 'groundId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
//end loop
|
|
|
|
db.grounds.belongsTo(db.sports, {
|
|
as: 'sport',
|
|
foreignKey: {
|
|
name: 'sportId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.grounds.hasMany(db.file, {
|
|
as: 'images',
|
|
foreignKey: 'belongsToId',
|
|
constraints: false,
|
|
scope: {
|
|
belongsTo: db.grounds.getTableName(),
|
|
belongsToColumn: 'images',
|
|
},
|
|
});
|
|
|
|
db.grounds.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.grounds.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
return grounds;
|
|
};
|