2026-02-01 17:16:41 +00:00

192 lines
2.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 items = sequelize.define(
'items',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
slug: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
rarity: {
type: DataTypes.ENUM,
values: [
"Common",
"Uncommon",
"Rare",
"Legendary",
"Exotic"
],
},
craftable: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
item_code: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
items.associate = (db) => {
db.items.belongsToMany(db.tags, {
as: 'tags',
foreignKey: {
name: 'items_tagsId',
},
constraints: false,
through: 'itemsTagsTags',
});
db.items.belongsToMany(db.tags, {
as: 'tags_filter',
foreignKey: {
name: 'items_tagsId',
},
constraints: false,
through: 'itemsTagsTags',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.items.hasMany(db.recipes, {
as: 'recipes_result',
foreignKey: {
name: 'resultId',
},
constraints: false,
});
db.items.hasMany(db.recycled_outputs, {
as: 'recycled_outputs_source_item',
foreignKey: {
name: 'source_itemId',
},
constraints: false,
});
//end loop
db.items.belongsTo(db.categories, {
as: 'category',
foreignKey: {
name: 'categoryId',
},
constraints: false,
});
db.items.belongsTo(db.manufacturers, {
as: 'manufacturer',
foreignKey: {
name: 'manufacturerId',
},
constraints: false,
});
db.items.hasMany(db.file, {
as: 'images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.items.getTableName(),
belongsToColumn: 'images',
},
});
db.items.belongsTo(db.users, {
as: 'createdBy',
});
db.items.belongsTo(db.users, {
as: 'updatedBy',
});
};
return items;
};