49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
|
|
const slugify = require("slugify");
|
|
|
|
const {
|
|
Model
|
|
} = require('sequelize');
|
|
module.exports = (sequelize, DataTypes) => {
|
|
class Article extends Model {
|
|
/**
|
|
* Helper method for defining associations.
|
|
* This method is not a part of Sequelize lifecycle.
|
|
* The `models/index` file will call this method automatically.
|
|
*/
|
|
static associate(models) {
|
|
/* define association here */
|
|
}
|
|
}
|
|
Article.init({
|
|
articleId: { 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: false,
|
|
modelName: 'Article',
|
|
tableName: 'article',
|
|
createdAt: true,
|
|
updatedAt: true
|
|
});
|
|
|
|
Article.beforeValidate((article) => {
|
|
article.slug = slugify(article.title, { lower: true });
|
|
});
|
|
|
|
Article.prototype.toJSONFor = function () {
|
|
return {
|
|
articleId: this.articleId,
|
|
slug: this.slug,
|
|
title: this.title,
|
|
description: this.description,
|
|
body: this.body
|
|
};
|
|
};
|
|
|
|
return Article;
|
|
};
|