35 lines
950 B
JavaScript
35 lines
950 B
JavaScript
const { DataTypes } = require("sequelize");
|
|
const sequelize = require("../util/database");
|
|
const slugify = require("slugify");
|
|
|
|
const Article = sequelize.define("Article", {
|
|
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
|
slug: { type: DataTypes.STRING, allowNull: false },
|
|
title: { type: DataTypes.STRING, allowNull: false },
|
|
description: { type: DataTypes.TEXT },
|
|
body: { type: DataTypes.TEXT, allowNull: false }
|
|
}, {
|
|
sequelize,
|
|
timestamps: true,
|
|
modelName: 'Article',
|
|
tableName: 'article',
|
|
createdAt: true,
|
|
updatedAt: true
|
|
});
|
|
|
|
Article.beforeValidate((article) => {
|
|
article.slug = slugify(article.title, { lower: true });
|
|
});
|
|
|
|
Article.prototype.toJSONFor = function () {
|
|
return {
|
|
id: this.id,
|
|
slug: this.slug,
|
|
title: this.title,
|
|
description: this.description,
|
|
body: this.body
|
|
};
|
|
};
|
|
|
|
module.exports = Article;
|