segunolalive/helloBooks

View on GitHub
server/models/book.js

Summary

Maintainability
A
0 mins
Test Coverage
export default (sequelize, DataTypes) => {
  const Book = sequelize.define('Book', {
    title: {
      allowNull: false,
      type: DataTypes.STRING,
      unique: true,
    },
    authors: {
      allowNull: false,
      type: DataTypes.STRING,
    },
    description: {
      type: DataTypes.TEXT,
    },
    total: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
    },
    cover: {
      type: DataTypes.STRING,
    },
    bookFile: {
      type: DataTypes.STRING,
    },
    categoryId: {
      type: DataTypes.INTEGER,
    }
  });
  Book.associate = (models) => {
    Book.belongsToMany(models.User, {
      through: 'BorrowedBook',
      foreignKey: 'bookId',
      otherKey: 'userId',
      unique: false,
    });
  };
  Book.associate = (models) => {
    Book.belongsTo(models.BookCategory, {
      as: 'bookCategory',
      foreignKey: 'id',
    });
  };
  return Book;
};