Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Model } = require("sequelize");
- module.exports = (sequelize, DataTypes) => {
- class Category extends Model {
- static associate(models) {
- // define association here
- Category.hasMany(Device, { foreignKey: "categoryId" });
- Category.hasMany(Peripheral, { foreignKey: "categoryId" });
- }
- }
- class Device extends Model {
- static associate(models) {
- // define association here
- Device.belongsTo(Category);
- }
- }
- class Peripheral extends Model {
- static associate(models) {
- // define association here
- Peripheral.belongsTo(Category);
- }
- }
- // Initialize Category
- Category.init(
- {
- id: {
- type: DataTypes.INTEGER,
- primaryKey: true,
- autoIncrement: true,
- },
- name: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- low: {
- type: DataTypes.INTEGER,
- allowNull: true,
- },
- moderate: {
- type: DataTypes.INTEGER,
- allowNull: true,
- },
- high: {
- type: DataTypes.INTEGER,
- allowNull: true,
- },
- isPeripheral: {
- type: DataTypes.BOOLEAN,
- allowNull: true,
- defaultValue: false,
- },
- },
- {
- sequelize,
- modelName: "category",
- }
- );
- // Initialize Device
- Device.init(
- {
- id: {
- type: DataTypes.INTEGER,
- primaryKey: true,
- autoIncrement: true,
- },
- serialNumber: {
- type: DataTypes.STRING,
- allowNull: false,
- unique: true,
- },
- operatingSystem: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- model: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- price: {
- type: DataTypes.FLOAT,
- allowNull: false,
- validate: {
- min: 0,
- },
- },
- currency: {
- type: DataTypes.STRING,
- allowNull: true,
- },
- supplier: {
- type: DataTypes.STRING,
- allowNull: true,
- },
- complianceId: {
- type: DataTypes.STRING,
- allowNull: true,
- },
- archived: {
- type: DataTypes.BOOLEAN,
- allowNull: true,
- defaultValue: false,
- },
- storage: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- ram: {
- type: DataTypes.STRING,
- allowNull: false,
- },
- processor: {
- type: DataTypes.STRING,
- allowNull: true,
- },
- },
- {
- sequelize,
- modelName: "device",
- }
- );
- // Initialize Peripheral
- Peripheral.init(
- {
- id: {
- type: DataTypes.INTEGER,
- primaryKey: true,
- autoIncrement: true,
- },
- supplier: {
- type: DataTypes.STRING,
- allowNull: true,
- },
- quantity: {
- type: DataTypes.INTEGER,
- defaultValue: 0,
- validate: { min: 0 },
- },
- },
- {
- sequelize,
- modelName: "peripheral",
- }
- );
- // Define Associations
- // Return all models
- return Category, Device, Peripheral ;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement