38920-vm/backend/src/db/models/instruments.js
2026-03-01 20:12:54 +00:00

190 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 instruments = sequelize.define(
'instruments',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
family: {
type: DataTypes.TEXT,
},
type: {
type: DataTypes.ENUM,
values: [
"acoustic",
"electric",
"electronic",
"percussion",
"vocal",
"other"
],
},
description: {
type: DataTypes.TEXT,
},
is_active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
instruments.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.instruments.hasMany(db.instrument_presets, {
as: 'instrument_presets_instrument',
foreignKey: {
name: 'instrumentId',
},
constraints: false,
});
db.instruments.hasMany(db.rhythm_patterns, {
as: 'rhythm_patterns_instrument',
foreignKey: {
name: 'instrumentId',
},
constraints: false,
});
db.instruments.hasMany(db.tracks, {
as: 'tracks_instrument',
foreignKey: {
name: 'instrumentId',
},
constraints: false,
});
db.instruments.hasMany(db.project_assets, {
as: 'project_assets_instrument',
foreignKey: {
name: 'instrumentId',
},
constraints: false,
});
//end loop
db.instruments.hasMany(db.file, {
as: 'images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.instruments.getTableName(),
belongsToColumn: 'images',
},
});
db.instruments.hasMany(db.file, {
as: 'sample_pack',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.instruments.getTableName(),
belongsToColumn: 'sample_pack',
},
});
db.instruments.belongsTo(db.users, {
as: 'createdBy',
});
db.instruments.belongsTo(db.users, {
as: 'updatedBy',
});
};
return instruments;
};