Created
July 4, 2021 15:49
-
-
Save enzodanjour/db78c765f14214b81ccfe08e05442018 to your computer and use it in GitHub Desktop.
como adiciono a tabela tags_produtos o id do produto e dos ids da lista de tags, e evitando redundâncias de repetição?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| module.exports = { | |
| up: async (queryInterface, Sequelize) => { | |
| await queryInterface.createTable('tagsProdutos', { | |
| id:{ | |
| type: Sequelize.INTEGER, | |
| allowNull: false, | |
| autoIncrement: true, | |
| primaryKey: true, | |
| }, | |
| produtosId: { | |
| type:Sequelize.INTEGER, | |
| references:{ | |
| model: 'Produto', | |
| key:'id', | |
| }, | |
| onDelete: 'SET NULL' | |
| }, | |
| tagsId: { | |
| type:Sequelize.INTEGER, | |
| references:{ | |
| model: 'Tags', | |
| key:'id', | |
| }, | |
| onDelete: 'SET NULL' | |
| } | |
| }) | |
| /** | |
| * Add altering commands here. | |
| * | |
| * Example: | |
| * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); | |
| */ | |
| }, | |
| down: async (queryInterface, Sequelize) => { | |
| /** | |
| * Add reverting commands here. | |
| * | |
| * Example: | |
| * | |
| */ | |
| await queryInterface.dropTable('tagsProdutos'); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const { | |
| Model | |
| } = require('sequelize'); | |
| module.exports = (sequelize, DataTypes) => { | |
| class Produto extends Model { | |
| // relacionamento muitos para muitos criando outra tabela | |
| static associate(models) { | |
| Produto.belongsToMany(models.Tags, {through: 'TagsProdutos',as:"tags",foreignKey:"tag_id"}); | |
| } | |
| }; | |
| Produto.init({ | |
| nome: DataTypes.STRING, | |
| descricao: DataTypes.STRING, | |
| preço: DataTypes.INTEGER, | |
| tagsId: DataTypes.STRING | |
| }, { | |
| sequelize, | |
| modelName: 'Produto', | |
| }); | |
| return Produto; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| const router = express.Router() | |
| const productMid = require('../middleware/validate_products_middleware') | |
| const { Produto, Tags } = require('../db/models') | |
| router.post('/', productMid) | |
| router.put('/', productMid) | |
| router.get('/', async (req,resp)=>{ | |
| const products = await Produto.findAll() | |
| resp.json({ | |
| products:products | |
| }) | |
| }) | |
| router.get('/:id',async (req,resp)=>{ | |
| const product = await Produto.findByPk(req.params.id) | |
| resp.json({products:product}) | |
| }) | |
| router.post('/', async (req,resp)=>{ | |
| const product = await Produto.create({nome:req.body.nome,descrição:req.body.descrição,preço:req.body.preço}) | |
| const tag = await Tags.bulkCreate(req.body.tags)// adiciona a lista de tags | |
| const tagProduto = await sequelize. //como adiciono a tabela tags produtos o id do produto e dos ids da lista de tags? | |
| resp.json({ | |
| msg:"Produto"+ product.nome +"adicionado com sucesso!" | |
| }) | |
| }) | |
| module.exports = router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment