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 storage_buckets = sequelize.define( 'storage_buckets', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, provider: { type: DataTypes.TEXT, }, bucket_name: { type: DataTypes.TEXT, }, region: { type: DataTypes.TEXT, }, cdn_domain: { type: DataTypes.TEXT, }, default_bucket: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, status: { type: DataTypes.ENUM, values: [ "active", "disabled" ], }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); storage_buckets.associate = (db) => { db.storage_buckets.belongsToMany(db.storage_objects, { as: 'storage_objects', foreignKey: { name: 'storage_buckets_storage_objectsId', }, constraints: false, through: 'storage_bucketsStorage_objectsStorage_objects', }); db.storage_buckets.belongsToMany(db.storage_objects, { as: 'storage_objects_filter', foreignKey: { name: 'storage_buckets_storage_objectsId', }, constraints: false, through: 'storage_bucketsStorage_objectsStorage_objects', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.storage_buckets.hasMany(db.storage_objects, { as: 'storage_objects_bucket', foreignKey: { name: 'bucketId', }, constraints: false, }); //end loop db.storage_buckets.belongsTo(db.organizations, { as: 'organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.storage_buckets.belongsTo(db.users, { as: 'createdBy', }); db.storage_buckets.belongsTo(db.users, { as: 'updatedBy', }); }; return storage_buckets; };