37803-vm/backend/src/db/models/virtual_machines.js
2026-01-25 12:26:17 +00:00

189 lines
2.3 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 virtual_machines = sequelize.define(
'virtual_machines',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"stopped",
"running",
"pending",
"error"
],
},
region: {
type: DataTypes.TEXT,
},
ip_address: {
type: DataTypes.TEXT,
},
cpu_cores: {
type: DataTypes.INTEGER,
},
ram_gb: {
type: DataTypes.INTEGER,
},
disk_gb: {
type: DataTypes.INTEGER,
},
provisioned_at: {
type: DataTypes.DATE,
},
last_started: {
type: DataTypes.DATE,
},
is_public: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
virtual_machines.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.virtual_machines.hasMany(db.sessions, {
as: 'sessions_vm',
foreignKey: {
name: 'vmId',
},
constraints: false,
});
//end loop
db.virtual_machines.belongsTo(db.vm_templates, {
as: 'template',
foreignKey: {
name: 'templateId',
},
constraints: false,
});
db.virtual_machines.belongsTo(db.users, {
as: 'owner',
foreignKey: {
name: 'ownerId',
},
constraints: false,
});
db.virtual_machines.belongsTo(db.users, {
as: 'createdBy',
});
db.virtual_machines.belongsTo(db.users, {
as: 'updatedBy',
});
};
return virtual_machines;
};