Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
How create (optimize) service on Backend (ExpressJS) with sequelize

Tried to create API for internet store using ExpressJS and sequelize. I have users, basket and devices. To connect basket and choosen by user devices used middle table BasketDevice.

Models:

export const Models = {

    User: sequelize.define('user',
        {
            id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
            email: { type: DataTypes.STRING, unique: true },
            password: { type: DataTypes.STRING },
            role: { type: DataTypes.STRING, defaultValue: 'USER' },
            isActivated: { type: DataTypes.BOOLEAN, defaultValue: false},
            activationLink: { type: DataTypes.STRING, unique: true }
        }
    ),
    Basket: sequelize.define('basket',
        {
            id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        }
    ),
    BasketDevice: sequelize.define('basketDevice',
        {
            id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },        
        }
    ),
    Device: sequelize.define('device',
        {
            id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
            name: { type: DataTypes.STRING, allowNull: false, unique: true },
            price: { type: DataTypes.INTEGER, allowNull: false },
            img: { type: DataTypes.STRING, allowNull: false },
            rating: { type: DataTypes.INTEGER, defaultValue: 0 },
        }
}

Assotiations between tables:

Models.User.hasOne(Models.Basket)
Models.Basket.belongsTo(Models.User)

Models.Basket.hasMany(Models.BasketDevice, {as: 'devices'})
Models.BasketDevice.belongsTo(Models.Basket)

Models.Device.hasMany(Models.BasketDevice)
Models.BasketDevice.belongsTo(Models.Device)

Tryed to give in response basket include basket device

Basket service:

class BasketService {
    async getBasket(token: string) {
        const userData = await TokenService.verifyRefreshToken(token) as any
        const basket = await Models.Basket.findOne({
             where: { userId: userData.id },
             include: [{model: Models.BasketDevice, as: 'devices'}]
            })

        return basket
    }
}

Don't know how create getBasket function that gives added to cart devices and counting of one device What is the opltimal way to give devices added in basket?

almost 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!