const slug = require("slug"); 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({ article_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, article_slug: { type: DataTypes.STRING, allowNull: false }, article_title: { type: DataTypes.STRING, allowNull: false }, article_description: { type: DataTypes.TEXT }, article_body: { type: DataTypes.TEXT, allowNull: false } }, { sequelize, timestamps: false, modelName: 'Article', tableName: 'article', createdAt: true, updatedAt: true }); Article.beforeValidate((article) => { article.slug = slug(article.title) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36); }); Article.prototype.toJSONFor = function () { return { id: this.article_id, slug: this.article_slug, title: this.article_title, description: this.article_description, body: this.article_body }; }; return Article; };