diff --git a/src/database/models/mysql/Brand.js b/src/database/models/mysql/Brand.js new file mode 100644 index 0000000..7030dce --- /dev/null +++ b/src/database/models/mysql/Brand.js @@ -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; diff --git a/src/database/models/mysql/Category.js b/src/database/models/mysql/Category.js new file mode 100644 index 0000000..0adfbc0 --- /dev/null +++ b/src/database/models/mysql/Category.js @@ -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; diff --git a/src/database/models/mysql/Packaging.js b/src/database/models/mysql/Packaging.js new file mode 100644 index 0000000..c6dcb1a --- /dev/null +++ b/src/database/models/mysql/Packaging.js @@ -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; diff --git a/src/database/models/mysql/Product-tmp.js b/src/database/models/mysql/Product-tmp.js new file mode 100644 index 0000000..49ad1b1 --- /dev/null +++ b/src/database/models/mysql/Product-tmp.js @@ -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; diff --git a/src/database/models/mysql/Product.js b/src/database/models/mysql/Product.js index 49ad1b1..4226f96 100644 --- a/src/database/models/mysql/Product.js +++ b/src/database/models/mysql/Product.js @@ -1,6 +1,5 @@ const { DataTypes, Model } = require("sequelize"); const sequelize = require("../../config/sequelize"); -const slug = require("slug"); class Product extends Model { } @@ -13,11 +12,12 @@ const ProductModel = () => { allowNull: false, primaryKey: true }, - slug: { - type: DataTypes.STRING(255), - allowNull: false + eancode: { + type: DataTypes.STRING(13), + allowNull: false, + unique: "eancode" }, - title: { + name: { type: DataTypes.STRING(255), allowNull: false }, @@ -25,17 +25,59 @@ const ProductModel = () => { type: DataTypes.TEXT, allowNull: true }, - body: { - type: DataTypes.TEXT, - allowNull: false + stock: { + type: DataTypes.INTEGER, + 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), allowNull: true, references: { model: 'user', 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", 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 () { return { id: this.id, - slug: this.slug, - title: this.title, + eancode: this.eancode, + name: this.name, 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; + + diff --git a/src/database/models/mysql/ProductCategoryXref.js b/src/database/models/mysql/ProductCategoryXref.js new file mode 100644 index 0000000..6051608 --- /dev/null +++ b/src/database/models/mysql/ProductCategoryXref.js @@ -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; diff --git a/src/database/models/mysql/ProductTagXref.js b/src/database/models/mysql/ProductTagXref.js new file mode 100644 index 0000000..dd94adb --- /dev/null +++ b/src/database/models/mysql/ProductTagXref.js @@ -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; diff --git a/src/database/models/mysql/Role.js b/src/database/models/mysql/Role.js new file mode 100644 index 0000000..54cbbec --- /dev/null +++ b/src/database/models/mysql/Role.js @@ -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; diff --git a/src/database/models/mysql/UserRoleXref.js b/src/database/models/mysql/UserRoleXref.js new file mode 100644 index 0000000..1078e5e --- /dev/null +++ b/src/database/models/mysql/UserRoleXref.js @@ -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; diff --git a/src/database/models/mysql/index.js b/src/database/models/mysql/index.js index 09b1eac..7bbea5a 100644 --- a/src/database/models/mysql/index.js +++ b/src/database/models/mysql/index.js @@ -1,8 +1,16 @@ 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.Favorite = require('./Favorite'); 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.TagList = require('./TagList'); -exports.User = require('./User'); \ No newline at end of file +exports.User = require('./User'); +exports.UserRoleXref = require('./UserRoleXref'); \ No newline at end of file