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

247
Visualizações
select next and previous records in Sequelize based on createdAt

What I want: fetch one record from the database based on its ID (UUID) and retrieve next and previous records too

Associations: posts N:M categories, posts 1:N comments, posts N:1 users, posts N:M tags, posts 1:N post_views

Current solution:

const post = await PostModel.findByPk(id, {
    include: [
      { model: CategoryModel, attributes: ['title'] },
      { model: TagModel },
      { model: CommentModel, include: { model: UserModel } },
      { model: PostViewModel },
      { model: UserModel, attributes: ['fullname', 'id', 'avatar'] }
    ]
  });
const nextPost = await PostModel.findOne({
  where: { createdAt: {
    [Op.gt]: post.createdAt
  }}
const prevPost = await PostModel.findOne({
  where: { createdAt: {
    [Op.lt]: post.createdAt
  }}

});

but I think this is not efficient and good.

over 4 years ago · Santiago Trujillo
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