Ajout initial des fichiers de la version mysal

This commit is contained in:
Rampeur
2025-08-08 18:34:15 +02:00
parent 29635cdf83
commit 9a534d5a17
30 changed files with 1381 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
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;
};
+35
View File
@@ -0,0 +1,35 @@
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Comment 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 */
}
}
Comment.init({
comment_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
comment_body: { type: DataTypes.TEXT, allowNull: false }
}, {
sequelize,
timestamps: false,
modelName: 'Comment',
tableName: 'comment',
createdAt: true,
updatedAt: true
});
Comment.prototype.toJSONFor = function () {
return {
id: this.comment_id,
body: this.comment_body
};
};
return Comment;
};
+57
View File
@@ -0,0 +1,57 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Membre 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 */
}
}
Membre.init({
mbr_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false},
mbr_pseudo: {type: DataTypes.STRING, allowNull: false},
mbr_nom: {type: DataTypes.STRING},
mbr_prenom: {type: DataTypes.STRING},
mbr_email: {type: DataTypes.STRING},
mbr_adresse: {type: DataTypes.STRING},
mbr_ville: {type: DataTypes.STRING},
mbr_region: {type: DataTypes.STRING},
mbr_cp: {type: DataTypes.STRING},
mbr_pays: {type: DataTypes.STRING},
mbr_tel: {type: DataTypes.STRING},
mbr_mobile: {type: DataTypes.STRING},
mbr_statut: {type: DataTypes.INTEGER}
}, {
sequelize,
timestamps: false,
modelName: 'Membre',
tableName: 'membre',
createdAt: false,
updatedAt: false
});
Membre.prototype.toJSONFor = function () {
return {
id: this.mbr_id,
pseudo: this.mbr_pseudo,
nom: this.mbr_nom,
prenom: this.mbr_prenom,
email: this.mbr_email,
adresse: this.mbr_adresse,
ville: this.mbr_ville,
region: this.mbr_region,
cp: this.mbr_cp,
pays: this.mbr_pays,
tel: this.mbr_tel,
mobile: this.mbr_mobile,
statut: this.mbr_statut
};
};
return Membre;
};
+34
View File
@@ -0,0 +1,34 @@
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Tag 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 */
}
}
Tag.init({
tag_name: { type: DataTypes.STRING, allowNull: false, primaryKey: true }
}, {
sequelize,
timestamps: false,
modelName: 'Tag',
tableName: 'tag',
createdAt: true,
updatedAt: true
});
Tag.prototype.toJSONFor = function () {
return {
id: this.tag_id,
name: this.tag_name
};
};
return Tag;
};
+74
View File
@@ -0,0 +1,74 @@
'use strict';
const bcrypt = require('bcrypt');
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User 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 */
}
}
User.init({
user_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false},
user_username: {type: DataTypes.STRING, allowNull: false},
user_lastname: {type: DataTypes.STRING},
user_firstname: {type: DataTypes.STRING},
user_email: {type: DataTypes.STRING},
user_adresse: {type: DataTypes.STRING},
user_ville: {type: DataTypes.STRING},
user_region: {type: DataTypes.STRING},
user_cp: {type: DataTypes.STRING},
user_pays: {type: DataTypes.STRING},
user_tel: {type: DataTypes.STRING},
user_mobile: {type: DataTypes.STRING},
user_role: {type: DataTypes.INTEGER}
}, {
sequelize,
timestamps: false,
modelName: 'User',
tableName: 'user',
createdAt: true,
updatedAt: true
});
User.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.mbr_id,
username: this.mbr_pseudo,
lastname: this.mbr_nom,
firstname: this.mbr_prenom,
email: this.mbr_email,
adresse: this.mbr_adresse,
ville: this.mbr_ville,
region: this.mbr_region,
cp: this.mbr_cp,
pays: this.mbr_pays,
tel: this.mbr_tel,
mobile: this.mbr_mobile,
statut: this.mbr_statut
};
};
return User;
};
+50
View File
@@ -0,0 +1,50 @@
const fs = require('fs'),
path = require('path'),
basename = path.basename(__filename),
chalk = require('chalk'),
utils = require('../../routes/utils');
const { Sequelize } = require('sequelize');
const db = {};
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
}
);
sequelize.authenticate().then(() => {
if (process.env.DEBUG) {
console.log(`${utils.getDateTimeISOString()}`, chalk.green('MySQL connection has been established successfully'));
}
}).catch((error) => {
console.log(`${utils.getDateTimeISOString()}`, chalk.red('Unable to connect to the MySQL database'));
console.error(error);
});
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
module.exports = db;