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

393
Visualizações
Sequelize: findAndCountAll(), but with a specific row as the first entry

I've a table that I'm querying using findAndCountAll()

I'm basically using it for pagination/ordering using the order, limit and index properties. Something simple like this:

exports.getApplications = async(req, res, next) => {
    const findOne = req.query.indexId;
    const index = req.query.index || 0;
    const limit = req.query.limit || 10;
    const orderField = req.query.orderField || 'createdAt';
    const orderDirection = req.query.orderDirection || 'DESC';
    const order = [ orderField, orderDirection ];

    const applications = await Application.findAndCountAll({   
        order: [order],      
        limit: parseInt(limit, 10), 
        offset: parseInt(index)
     
        // ...
    }
}

But I'd also like to be able to specify a particular indexId (findOne), and have that single entry appear before the paginated/ordered list (*edit: The row may or may not be included in the paginated list that's returned)

Is that possible using findAndCountAll? Or would I have to run a separate query using findByPk(), reduce the limit by 1, then unshift the entry to the front of the results array?

Thanks,

Nick


*Edit - Implementation in case anyone is interested. Now just have to make the queries run in parallel :)

try {
    // Find a specific row to highlight if needed
    if(findOne) {
        topRow = await Application.findByPk(findOne, {
            include: [...],
        });
    }

    const applications = await Application.findAndCountAll({
        include: [...],
        order: [order],      
        limit: parseInt(limit, 10), 
        offset: parseInt(index)
    });

    // If there's a row to be highlighted
    if(topRow) {
        const visible = applications.rows.filter(application => application.id === topRow.id);
        const index = applications.rows.findIndex(application => application.id === topRow.id);

        // 1 too many results in the array
        if(applications.rows.length + 1 > limit) {
            // If the row appears in the array to be returned, remove it
            if(visible) {
                applications.rows.splice(index, 1);
            } else {
                // Remove the last element instead
                applications.rows.pop();
            }
        } else { 
            if(visible) applications.rows.splice(index, 1);
        }
        
        // Add the highlighted topRow to the array
        applications.rows.unshift(topRow) 
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try to do this, assuming your findOne value is safe to use:

order: [
  [sequelize.literal(`id = ${findOne}`), 'DESC'],
  [orderField, orderDirection]
]

This will return true for the row you want, false for others, so it will first sort it to the top, then continue the rest of the ordering.

about 4 years ago · Juan Pablo Isaza Relatório
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