Ajout et mise à jour de models mysql

This commit is contained in:
Rampeur
2025-08-29 16:08:53 +02:00
parent b1513dccdd
commit 6510c55458
10 changed files with 566 additions and 20 deletions
+51
View File
@@ -0,0 +1,51 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class Brand extends Model { }
const BrandModel = () => {
Brand.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(200),
allowNull: true
},
description: {
type: DataTypes.TEXT,
allowNull: true
}
},
{
sequelize,
tableName: 'brand',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
]
}
);
Brand.prototype.toJSONFor = function () {
return {
id: this.id,
name: this.name,
description: this.description
};
};
return Brand;
};
module.exports = BrandModel;
+52
View File
@@ -0,0 +1,52 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class Category extends Model { }
const CategoryModel = () => {
Category.init(
{
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(255),
allowNull: false
},
parentId: {
type: DataTypes.INTEGER,
allowNull: true
}
},
{
sequelize,
tableName: 'category',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
]
}
);
Category.prototype.toJSONFor = function () {
return {
id: this.id,
name: this.name,
parentId: this.parentId
};
};
return Category;
};
module.exports = CategoryModel;
+47
View File
@@ -0,0 +1,47 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class Packaging extends Model { }
const PackagingModel = () => {
Packaging.init(
{
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(255),
allowNull: false
}
},
{
sequelize,
tableName: 'packaging',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
]
}
);
Packaging.prototype.toJSONFor = function () {
return {
id: this.id,
name: this.name
};
};
return Packaging;
};
module.exports = PackagingModel;
+82
View File
@@ -0,0 +1,82 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
const slug = require("slug");
class Product extends Model { }
const ProductModel = () => {
Product.init(
{
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
slug: {
type: DataTypes.STRING(255),
allowNull: false
},
title: {
type: DataTypes.STRING(255),
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
body: {
type: DataTypes.TEXT,
allowNull: false
},
authorId: {
type: DataTypes.STRING(24),
allowNull: true,
references: {
model: 'user',
key: 'id'
}
}
},
{
sequelize,
tableName: 'product',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
{
name: "authorId",
using: "BTREE",
fields: [
{ name: "authorId" },
]
},
]
}
);
Product.beforeValidate((product) => {
product.slug = slug(`${product.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
});
Product.prototype.toJSONFor = function () {
return {
id: this.id,
slug: this.slug,
title: this.title,
description: this.description,
body: this.body
};
};
return Product;
};
module.exports = ProductModel;
+88 -18
View File
@@ -1,6 +1,5 @@
const { DataTypes, Model } = require("sequelize"); const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize"); const sequelize = require("../../config/sequelize");
const slug = require("slug");
class Product extends Model { } class Product extends Model { }
@@ -13,11 +12,12 @@ const ProductModel = () => {
allowNull: false, allowNull: false,
primaryKey: true primaryKey: true
}, },
slug: { eancode: {
type: DataTypes.STRING(255), type: DataTypes.STRING(13),
allowNull: false allowNull: false,
unique: "eancode"
}, },
title: { name: {
type: DataTypes.STRING(255), type: DataTypes.STRING(255),
allowNull: false allowNull: false
}, },
@@ -25,17 +25,59 @@ const ProductModel = () => {
type: DataTypes.TEXT, type: DataTypes.TEXT,
allowNull: true allowNull: true
}, },
body: { stock: {
type: DataTypes.TEXT, type: DataTypes.INTEGER,
allowNull: false allowNull: true,
defaultValue: 0
}, },
authorId: { weightGross: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00
},
weightNet: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00
},
priceBuy: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00
},
priceRetail: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00
},
priceTaxe: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00
},
brandId: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'brand',
key: 'id'
}
},
ownerId: {
type: DataTypes.STRING(24), type: DataTypes.STRING(24),
allowNull: true, allowNull: true,
references: { references: {
model: 'user', model: 'user',
key: 'id' key: 'id'
} }
},
packagingId: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'packaging',
key: 'id'
}
} }
}, },
{ {
@@ -52,27 +94,53 @@ const ProductModel = () => {
] ]
}, },
{ {
name: "authorId", name: "eancode",
unique: true,
using: "BTREE", using: "BTREE",
fields: [ fields: [
{ name: "authorId" }, { name: "eancode" },
]
},
{
name: "brandId",
using: "BTREE",
fields: [
{ name: "brandId" },
]
},
{
name: "ownerId",
using: "BTREE",
fields: [
{ name: "ownerId" },
]
},
{
name: "packagingId",
using: "BTREE",
fields: [
{ name: "packagingId" },
] ]
}, },
] ]
} }
); );
Product.beforeValidate((product) => {
product.slug = slug(`${product.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
});
Product.prototype.toJSONFor = function () { Product.prototype.toJSONFor = function () {
return { return {
id: this.id, id: this.id,
slug: this.slug, eancode: this.eancode,
title: this.title, name: this.name,
description: this.description, description: this.description,
body: this.body stock: this.stock,
weightGross: this.weightGross,
weightNet: this.weightNet,
priceBuy: this.priceBuy,
priceRetail: this.priceRetail,
priceTaxe: this.priceTaxe,
brandId: this.brandId,
ownerId: this.ownerId,
packagingId: this.packagingId,
}; };
}; };
@@ -80,3 +148,5 @@ const ProductModel = () => {
}; };
module.exports = ProductModel; module.exports = ProductModel;
@@ -0,0 +1,63 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class ProductCategoryXref extends Model { }
const ProductCategoryXrefModel = () => {
ProductCategoryXref.init(
{
productId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'product',
key: 'id'
}
},
categoryId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'category',
key: 'id'
}
}
},
{
sequelize,
tableName: 'productCategoryXref',
timestamps: false,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "productId" },
{ name: "categoryId" },
]
},
{
name: "categoryId",
using: "BTREE",
fields: [
{ name: "categoryId" },
]
},
]
}
);
ProductCategoryXref.prototype.toJSONFor = function () {
return {
productId: this.productId,
categoryId: this.categoryId
};
};
return ProductCategoryXref;
};
module.exports = ProductCategoryXrefModel;
@@ -0,0 +1,63 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class ProductTagXref extends Model { }
const ProductTagXrefModel = () => {
ProductTagXref.init(
{
productId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'product',
key: 'id'
}
},
tagName: {
type: DataTypes.STRING(255),
allowNull: false,
primaryKey: true,
references: {
model: 'tag',
key: 'name'
}
}
},
{
sequelize,
tableName: 'productTagXref',
timestamps: false,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "productId" },
{ name: "tagName" },
]
},
{
name: "tagName",
using: "BTREE",
fields: [
{ name: "tagName" },
]
},
]
}
);
ProductTagXref.prototype.toJSONFor = function () {
return {
productId: this.productId,
tagName: this.tagName
};
};
return ProductTagXref;
};
module.exports = ProductTagXrefModel;
+47
View File
@@ -0,0 +1,47 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class Role extends Model { }
const RoleModel = () => {
Role.init(
{
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(255),
allowNull: false
}
},
{
sequelize,
tableName: 'role',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "id" },
]
},
]
}
);
Role.prototype.toJSONFor = function () {
return {
id: this.id,
name: this.name
};
};
return Role;
};
module.exports = RoleModel;
+63
View File
@@ -0,0 +1,63 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../../config/sequelize");
class UserRoleXref extends Model { }
const UserRoleXrefModel = () => {
UserRoleXref.init(
{
userId: {
type: DataTypes.STRING(24),
allowNull: false,
primaryKey: true,
references: {
model: 'user',
key: 'id'
}
},
roleId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'role',
key: 'id'
}
}
},
{
sequelize,
tableName: 'userRoleXref',
timestamps: true,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "userId" },
{ name: "roleId" },
]
},
{
name: "roleId",
using: "BTREE",
fields: [
{ name: "roleId" },
]
},
]
}
);
UserRoleXref.prototype.toJSONFor = function () {
return {
userId: this.userId,
roleId: this.roleId
};
};
return UserRoleXref;
};
module.exports = UserRoleXrefModel;
+10 -2
View File
@@ -1,8 +1,16 @@
exports.Article = require('./Article'); exports.Article = require('./Article');
exports.ArticleTag = require('./ArticleTag'); //exports.ArticleTag = require('./ArticleTag');
exports.Brand = require('./Brand');
exports.Category = require('./Category');
exports.Comment = require('./Comment'); exports.Comment = require('./Comment');
exports.Favorite = require('./Favorite'); exports.Favorite = require('./Favorite');
exports.Follower = require('./Follower'); exports.Follower = require('./Follower');
exports.Packaging = require('./Packaging');
exports.Product = require('./Product');
exports.ProductCategoryXref = require('./ProductCategoryXref');
exports.ProductTagXref = require('./ProductTagXref');
exports.Role = require('./Role');
exports.Tag = require('./Tag'); exports.Tag = require('./Tag');
exports.TagList = require('./TagList'); exports.TagList = require('./TagList');
exports.User = require('./User'); exports.User = require('./User');
exports.UserRoleXref = require('./UserRoleXref');