Ajout de la base de données MySQL

This commit is contained in:
Rampeur
2025-08-13 21:51:07 +02:00
parent be44e4fdf0
commit 49b1a3f6b3
35 changed files with 1579 additions and 37 deletions
+43
View File
@@ -0,0 +1,43 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../config/sequelize");
const slug = require("slug");
class Article extends Model { }
const ArticleModel = () => {
Article.init(
{
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,
modelName: 'Article',
tableName: 'article',
timestamps: true,
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.id,
slug: this.slug,
title: this.title,
description: this.description,
body: this.body
};
};
return Article;
};
module.exports = ArticleModel;
View File
View File
+60
View File
@@ -0,0 +1,60 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../config/sequelize");
const bcrypt = require("bcrypt");
class User extends Model { }
const UserModel = () => {
User.init(
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
email: { type: DataTypes.STRING, allowNull: false, unique: true },
username: { type: DataTypes.STRING, allowNull: false, unique: true },
firstname: { type: DataTypes.STRING },
lastname: { type: DataTypes.STRING },
bio: { type: DataTypes.TEXT, allowNull: true },
image: { type: DataTypes.TEXT, allowNull: true },
role: { type: DataTypes.STRING },
password: { type: DataTypes.STRING, allowNull: false }
},
{
sequelize,
modelName: 'User',
tableName: 'user',
timestamps: false,
createdAt: true,
updatedAt: true
}
);
Model.prototype.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
const DEFAULT_SALT_ROUNDS = 10;
User.addHook("beforeCreate", async (user) => {
const encryptedPassword = await bcrypt.hash(
user.password,
DEFAULT_SALT_ROUNDS
);
user.password = encryptedPassword;
});
User.prototype.toJSONFor = function () {
return {
id: this.id,
email: this.email,
username: this.username,
firstname: this.firstname,
lastname: this.lastname,
bio: this.bio,
image: this.image,
role: this.role
};
};
return User;
};
module.exports = UserModel;