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;
};
+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;
};
+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("bcryptjs");
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;
};
+4 -5
View File
@@ -1,5 +1,4 @@
exports.User = require('./mysql/User');
exports.Application = require('./mysql/Article');
exports.Aeronef = require('./mysql/Comment');
exports.Canopy = require('./mysql/Membre');
exports.Dropzone = require('./mysql/Tag');
exports.User = require('./User');
exports.Article = require('./Article');
exports.Comment = require('./Comment');
exports.Tag = require('./Tag');
+2 -2
View File
@@ -1,5 +1,5 @@
const slug = require("slug");
const slugify = require("slugify");
const {
Model
@@ -31,7 +31,7 @@ module.exports = (sequelize, DataTypes) => {
});
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 () {
-57
View File
@@ -1,57 +0,0 @@
'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;
};
+1 -1
View File
@@ -1,6 +1,6 @@
'use strict';
const bcrypt = require('bcrypt');
const bcrypt = require("bcryptjs");
const {
Model
+8 -6
View File
@@ -1,5 +1,5 @@
var appEnv = process.env.APP_ENV;
console.log(`Env: ${appEnv}`);
if (appEnv == undefined) {
appEnv = 'development';
}
@@ -13,11 +13,13 @@ const colors = require("colors");
const { errorHandler } = require("./middlewares/errorHandler");
// Import Models
/*
const User = require("./models/User");
const Article = require("./models/Article");
const Tag = require("./models/Tag");
const Comment = require("./models/Comment");
*/
var db = require("./models/mysql");
const app = express();
@@ -61,22 +63,22 @@ app.use(tags);
const PORT = process.env.PORT || process.env.SERVER_PORT;
app.use(errorHandler);
console.log(`User: ${User}`);
// Relations
User.belongsToMany(User, {
db.User.belongsToMany(User, {
as: "followers",
through: "Followers",
foreignKey: "userId",
timestamps: false,
});
User.belongsToMany(User, {
db.User.belongsToMany(User, {
as: "following",
through: "Followers",
foreignKey: "followerId",
timestamps: false,
});
User.hasMany(Article, {
db.User.hasMany(Article, {
foreignKey: "authorId",
onDelete: "CASCADE",
});