119 lines
2.1 KiB
JavaScript
119 lines
2.1 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 books = sequelize.define(
|
|
'books',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
title: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
author: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
description: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
grade: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
subject: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
curriculum_code: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
language: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['en', 'tpi', 'tpi-en'],
|
|
},
|
|
|
|
file_name: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
cover_name: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
file_size_bytes: {
|
|
type: DataTypes.INTEGER,
|
|
},
|
|
|
|
year_published: {
|
|
type: DataTypes.INTEGER,
|
|
},
|
|
|
|
uploaded_at: {
|
|
type: DataTypes.DATE,
|
|
},
|
|
|
|
is_visible: {
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
},
|
|
);
|
|
|
|
books.associate = (db) => {
|
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
|
|
|
db.books.hasMany(db.book_categories, {
|
|
as: 'book_categories_book',
|
|
foreignKey: {
|
|
name: 'bookId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.books.hasMany(db.download_logs, {
|
|
as: 'download_logs_book',
|
|
foreignKey: {
|
|
name: 'bookId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
//end loop
|
|
|
|
db.books.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.books.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
return books;
|
|
};
|