38278-vm/backend/src/db/models/instruments.js
2026-02-07 23:44:55 +00:00

188 lines
2.0 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,
},
symbol: {
type: DataTypes.TEXT,
},
name: {
type: DataTypes.TEXT,
},
asset_class: {
type: DataTypes.ENUM,
values: [
"forex",
"crypto",
"stocks",
"options",
"futures",
"indices",
"commodities",
"bonds",
"etf",
"other"
],
},
exchange: {
type: DataTypes.TEXT,
},
tick_size: {
type: DataTypes.TEXT,
},
pip_value_hint: {
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.trades, {
as: 'trades_instrument',
foreignKey: {
name: 'instrumentId',
},
constraints: false,
});
db.instruments.hasMany(db.watchlist_items, {
as: 'watchlist_items_instrument',
foreignKey: {
name: 'instrumentId',
},
constraints: false,
});
//end loop
db.instruments.belongsTo(db.users, {
as: 'createdBy',
});
db.instruments.belongsTo(db.users, {
as: 'updatedBy',
});
};
return instruments;
};