Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda