Advertisement
Larrisa

device.js

Jan 8th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Model } = require("sequelize");
  2.  
  3. module.exports = (sequelize, DataTypes) => {
  4.   class Category extends Model {
  5.     static associate(models) {
  6.       // define association here
  7.       Category.hasMany(Device, { foreignKey: "categoryId" });
  8.   Category.hasMany(Peripheral, { foreignKey: "categoryId" });
  9.     }
  10.   }
  11.  
  12.   class Device extends Model {
  13.     static associate(models) {
  14.       // define association here
  15.       Device.belongsTo(Category);
  16.     }
  17.   }
  18.  
  19.   class Peripheral extends Model {
  20.     static associate(models) {
  21.       // define association here
  22.       Peripheral.belongsTo(Category);
  23.     }
  24.   }
  25.  
  26.   // Initialize Category
  27.   Category.init(
  28.     {
  29.       id: {
  30.         type: DataTypes.INTEGER,
  31.         primaryKey: true,
  32.         autoIncrement: true,
  33.       },
  34.       name: {
  35.         type: DataTypes.STRING,
  36.         allowNull: false,
  37.       },
  38.       low: {
  39.         type: DataTypes.INTEGER,
  40.         allowNull: true,
  41.       },
  42.       moderate: {
  43.         type: DataTypes.INTEGER,
  44.         allowNull: true,
  45.       },
  46.       high: {
  47.         type: DataTypes.INTEGER,
  48.         allowNull: true,
  49.       },
  50.       isPeripheral: {
  51.         type: DataTypes.BOOLEAN,
  52.         allowNull: true,
  53.         defaultValue: false,
  54.       },
  55.     },
  56.     {
  57.       sequelize,
  58.       modelName: "category",
  59.     }
  60.   );
  61.  
  62.   // Initialize Device
  63.   Device.init(
  64.     {
  65.       id: {
  66.         type: DataTypes.INTEGER,
  67.         primaryKey: true,
  68.         autoIncrement: true,
  69.       },
  70.       serialNumber: {
  71.         type: DataTypes.STRING,
  72.         allowNull: false,
  73.         unique: true,
  74.       },
  75.       operatingSystem: {
  76.         type: DataTypes.STRING,
  77.         allowNull: false,
  78.       },
  79.       model: {
  80.         type: DataTypes.STRING,
  81.         allowNull: false,
  82.       },
  83.       price: {
  84.         type: DataTypes.FLOAT,
  85.         allowNull: false,
  86.         validate: {
  87.           min: 0,
  88.         },
  89.       },
  90.       currency: {
  91.         type: DataTypes.STRING,
  92.         allowNull: true,
  93.       },
  94.       supplier: {
  95.         type: DataTypes.STRING,
  96.         allowNull: true,
  97.       },
  98.       complianceId: {
  99.         type: DataTypes.STRING,
  100.         allowNull: true,
  101.       },
  102.       archived: {
  103.         type: DataTypes.BOOLEAN,
  104.         allowNull: true,
  105.         defaultValue: false,
  106.       },
  107.       storage: {
  108.         type: DataTypes.STRING,
  109.         allowNull: false,
  110.       },
  111.       ram: {
  112.         type: DataTypes.STRING,
  113.         allowNull: false,
  114.       },
  115.       processor: {
  116.         type: DataTypes.STRING,
  117.         allowNull: true,
  118.       },
  119.     },
  120.     {
  121.       sequelize,
  122.       modelName: "device",
  123.     }
  124.   );
  125.  
  126.   // Initialize Peripheral
  127.   Peripheral.init(
  128.     {
  129.       id: {
  130.         type: DataTypes.INTEGER,
  131.         primaryKey: true,
  132.         autoIncrement: true,
  133.       },
  134.       supplier: {
  135.         type: DataTypes.STRING,
  136.         allowNull: true,
  137.       },
  138.       quantity: {
  139.         type: DataTypes.INTEGER,
  140.         defaultValue: 0,
  141.         validate: { min: 0 },
  142.       },
  143.     },
  144.     {
  145.       sequelize,
  146.       modelName: "peripheral",
  147.     }
  148.   );
  149.  
  150.   // Define Associations
  151.  
  152.  
  153.  
  154.  
  155.   // Return all models
  156.   return Category, Device, Peripheral ;
  157. };
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement