Mise à jour de models

This commit is contained in:
Rampeur
2025-08-08 20:18:39 +02:00
parent e83b2e831f
commit 61fdac2434
9 changed files with 208 additions and 71 deletions
+50
View File
@@ -0,0 +1,50 @@
const slugify = require("slugify");
//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.slug = slugify(article.title, { lower: true });
});
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;
};