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
+33
View File
@@ -0,0 +1,33 @@
module.exports = {
"development": {
"username": process.env.MYSQL_DBUSER,
"password": process.env.MYSQL_DBPASS,
"database": process.env.MYSQL_DBNAME,
"host": process.env.MYSQL_DBHOST,
"port": process.env.MYSQL_DBPORT,
"dialect": process.env.MYSQL_DBTYPE
},
"test": {
"username": process.env.MYSQL_DBUSER,
"password": process.env.MYSQL_DBPASS,
"database": process.env.MYSQL_DBNAME,
"host": process.env.MYSQL_DBHOST,
"port": process.env.MYSQL_DBPORT,
"dialect": process.env.MYSQL_DBTYPE
},
"production": {
"username": process.env.MYSQL_DBUSER,
"password": process.env.MYSQL_DBPASS,
"database": process.env.MYSQL_DBNAME,
"host": process.env.MYSQL_DBHOST,
"port": process.env.MYSQL_DBPORT,
"dialect": process.env.MYSQL_DBTYPE,
"dialectOptions": {
"ssl": {
// enable this for production environment only if using a secure connection
"require": true,
"rejectUnauthorized": false
},
}
}
}
+43
View File
@@ -0,0 +1,43 @@
const { Sequelize } = require('sequelize');
const Op = Sequelize.Op;
const sequelize = new Sequelize(
process.env.MYSQL_DBNAME,
process.env.MYSQL_DBUSER,
process.env.MYSQL_DBPASS,
{
host: process.env.MYSQL_DBHOST,
port: process.env.MYSQL_DBPORT,
dialect: process.env.MYSQL_DBTYPE,
$like: Op.like,
$not: Op.not,
timezone: '+02:00',
define: {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
underscored: false,
freezeTableName: true,
},
pool: {
//explain this line of code? this means that the connection pool will have a minimum of 0 connections and a maximum of 5 connections
min: 0,
max: 5,
},
logQueryParameters: process.env.APP_ENV === 'development', // this line of code will log the query parameters if the environment is development
benchmark: true,
});
/*
const checkConnection = async () => {
try {
await sequelize.authenticate();
console.log(`DB Connected`.cyan.underline.bold);
} catch (error) {
console.error("Unable to connect to the database:".red.bold, error);
}
};
checkConnection();
*/
module.exports = sequelize;
+11
View File
@@ -0,0 +1,11 @@
const sequelize = require('./config/sequelize');
const ArticleModel = require('./models/article');
const UserModel = require('./models/user');
const DB = {
sequelize,
User: UserModel(sequelize),
Article: ArticleModel(sequelize),
};
module.exports = DB;
@@ -0,0 +1,33 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Users');
},
};
@@ -0,0 +1,41 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('article', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'User',
key: 'id',
},
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('article');
},
};
+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;
+29
View File
@@ -0,0 +1,29 @@
const DB = require('../index');
const associate = () => {
DB.User.hasMany(DB.Article, {
foreignKey: 'userId',
as: 'projects',
});
DB.Article.belongsTo(DB.User, {
foreignKey: 'userId',
as: 'user',
});
/*
DB.User.belongsToMany(DB.User, { as: "followers", through: "Followers", foreignKey: "userId", timestamps: false });
DB.User.belongsToMany(DB.User, { as: "following", through: "Followers", foreignKey: "followerId", timestamps: false });
DB.User.hasMany(DB.Article, { foreignKey: "authorId", onDelete: "CASCADE" });
DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
DB.User.hasMany(DB.Comment, { foreignKey: "authorId", onDelete: "CASCADE" });
DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
DB.Article.hasMany(DB.omment, { foreignKey: "articleId", onDelete: "CASCADE" });
DB.Comment.belongsTo(DB.Article, { foreignKey: "articleId" });
DB.User.belongsToMany(DB.Article, { as: "favorites", through: "Favorites", timestamps: false });
DB.Article.belongsToMany(DB.User, { through: "Favorites", foreignKey: "articleId", timestamps: false });
DB.Article.belongsToMany(DB.Tag, { through: "TagLists", as: "tagLists", foreignKey: "articleId", timestamps: false, onDelete: "CASCADE" });
DB.Tag.belongsToMany(DB.Article, { through: "ArticleTags", uniqueKey: false, timestamps: false });
*/
};
module.exports = associate;
+1
View File
@@ -0,0 +1 @@
console.log('this is placeholder for models');