Technology Used | Resource URL |
---|---|
JavaScript | https://developer.mozilla.org/en-US/docs/Web/JavaScript |
Node.js | https://developer.mozilla.org/en-US/docs/Glossary/Node.js |
MySQL | https://dev.mysql.com/doc/ |
Express.js | https://developer.mozilla.org/en-US/docs/Glossary/Express.js |
This application allows users to make API requests to an express.js server to read, write, and update ecommerce data in a MySQL database.
View the walkthrough video here.
The below code uses sequelize models and methods to correctly define the relationships between the Product, Category, Tag, and ProductTag MySQL tables.
// import models
const Product = require('./Product');
const Category = require('./Category');
const Tag = require('./Tag');
const ProductTag = require('./ProductTag');
// Products belongsTo Category
Product.belongsTo(Category, {
foreignKey: 'category_id'
});
// Categories have many Products
Category.hasMany(Product, {
foreignKey: 'category_id'
});
// Products belongToMany Tags (through ProductTag)
Product.belongsToMany(Tag, {
through: ProductTag,
foreignKey: 'product_id'
});
// Tags belongToMany Products (through ProductTag)
Tag.belongsToMany(Product, {
through: ProductTag,
foreignKey: 'tag_id'
});
module.exports = {
Product,
Category,
Tag,
ProductTag,
};