Hello folks, this is probably a well known problem, but I am having a hard time figuring it out from the docs and forums.
So, my application, when a button on the front end is pressed, should flag the current item as favorite or not. I have made it in Sequelize following the docs, and it led me to the following models:
users.model.js
const { DataTypes } = require("sequelize");
const db = require("../config/db.config");
const bcrypt = require("bcrypt");
const Users = db.define("User", {
username: {
type: DataTypes.STRING(255),
},
email: {
type: DataTypes.STRING(255),
},
password: {
type: DataTypes.STRING,
},
});
Users.beforeCreate("save", async function (next) {
const salt = await bcrypt.genSalt();
this.password = await bcrypt.hash(this.password, salt);
next();
});
module.exports = Users;
item.model.js
const { DataTypes } = require("sequelize");
const db = require("../config/db.config");
const Items = db.define("Item", {
title: {
type: DataTypes.STRING(255),
},
geolocation: {
type: DataTypes.GEOMETRY("POINT"),
},
});
module.exports = Items;
and their associations:
associations.Items.belongsToMany(associations.Users, {
through: "Favorites",
});
associations.Users.belongsToMany(associations.Items, {
through: "Favorites",
});
Now of course, as a result, the junction table has been automatically been created, however how can I access it to grab the current user, grab the item id and setting up a endpoint to create a favorite association in the junction table? As I mentioned, the Sequelize docs are confusing about it