Ajout de la base de données MySQL
This commit is contained in:
@@ -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;
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user