2026-03-17 06:59:09 +00:00

174 lines
2.2 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 materi = sequelize.define(
'materi',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
judul: {
type: DataTypes.TEXT,
},
deskripsi: {
type: DataTypes.TEXT,
},
tipe_konten: {
type: DataTypes.ENUM,
values: [
"pdf",
"video",
"link",
"teks"
],
},
url_video: {
type: DataTypes.TEXT,
},
published_at: {
type: DataTypes.DATE,
},
is_published: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
materi.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.materi.belongsTo(db.mata_pelajaran, {
as: 'mata_pelajaran',
foreignKey: {
name: 'mata_pelajaranId',
},
constraints: false,
});
db.materi.belongsTo(db.kelas, {
as: 'kelas',
foreignKey: {
name: 'kelasId',
},
constraints: false,
});
db.materi.belongsTo(db.guru, {
as: 'guru',
foreignKey: {
name: 'guruId',
},
constraints: false,
});
db.materi.hasMany(db.file, {
as: 'lampiran',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.materi.getTableName(),
belongsToColumn: 'lampiran',
},
});
db.materi.belongsTo(db.users, {
as: 'createdBy',
});
db.materi.belongsTo(db.users, {
as: 'updatedBy',
});
};
return materi;
};