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

248
Views
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
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!