Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
Sequelize - Count Based on Include Condition

I'm using the sequelize count function as follows:

Definition:

const countAllOrdersWhere = async (conditions) =>
    Order.count({
        where: conditions,
    })
        .then((count) => ({ count, error: null }))
        .catch((error) => ({ count: null, error }));

Usage:

  const { count, error: countError } = await countAllOrdersWhere({
                    [Op.or]: [
                        { userId: userIds, status },
                        { status, venueId },
                    ],
                });

This works great, however I now need to add a condition based on an associated model:

  1. Each order has many orderItems associated with it (there's an orderId on each row in the orderItem table.)
  2. Each orderItem has one item associated with it - (there's an itemid on each row in the orderItem table.)

I have an array of itemIds and I would like to only count the orders which are associated with an orderItem which has an itemId which is in my list.

When querying the orders, I enforce this clause in a findAll function by doing

include: [
 {
 model: OrderItem,   
 where: Sequelize.literal(
      itemIds && itemIds.length > 0
      ? `"orderItems"."itemId" IN (${itemIds})`
      : 'true'
 )}]

however, I'm not sure how to do this in a count function

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda