33492/backend/src/db/models/bottles.js
2025-08-19 18:34:29 +00:00

129 lines
2.4 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 bottles = sequelize.define(
'bottles',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
proof: {
type: DataTypes.DECIMAL,
},
type: {
type: DataTypes.ENUM,
values: ['Bourbon', 'Scotch', 'Rye', 'Irish', 'Other'],
},
notes: {
type: DataTypes.TEXT,
},
tasting_notes: {
type: DataTypes.TEXT,
},
msrp_range: {
type: DataTypes.TEXT,
},
secondary_value_range: {
type: DataTypes.TEXT,
},
opened_bottle_indicator: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
quantity: {
type: DataTypes.INTEGER,
},
barcode: {
type: DataTypes.TEXT,
},
age: {
type: DataTypes.DECIMAL,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
bottles.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.bottles.belongsTo(db.brands, {
as: 'brand',
foreignKey: {
name: 'brandId',
},
constraints: false,
});
db.bottles.belongsTo(db.distilleries, {
as: 'distillery',
foreignKey: {
name: 'distilleryId',
},
constraints: false,
});
db.bottles.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.bottles.hasMany(db.file, {
as: 'picture',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.bottles.getTableName(),
belongsToColumn: 'picture',
},
});
db.bottles.belongsTo(db.users, {
as: 'createdBy',
});
db.bottles.belongsTo(db.users, {
as: 'updatedBy',
});
};
return bottles;
};