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

342
Views
Converting a SQL query to SEQUELIZE

I am trying to wrap my head around creating this sequelize query.

SELECT 
    dayofweek(datum) AS DOW, 
    concat(DAY(datum), '-', MONTH(datum)) AS datum, 
    ochtend, 
    middag, 
    avond 
FROM dbt.menus
    ORDER BY datum ASC LIMIT 5 OFFSET (variable in sequelize code);

I have been able to create 2 separate queries where the 'order by filter' works and 'aliases' work, but not combined.

This is what i currenty have in sequelize

const amount = await Menu.count();
const listOfMenu = await Menu.findAll({ 
    order: [['datum', 'ASC']],
    offset: amount - 5,
    limit: 5,
    attributes: {
        include: [
            [
              Menu.sequelize.fn
              (
                "DATE_FORMAT", 
                Menu.sequelize.col("datum"), 
                "%d-%m "
              ),
              "datum",
            ],
          ]
     }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You just need to use fn twice: in order and in attributes options:

const datumExpression = Menu.sequelize.fn
              (
                "DATE_FORMAT", 
                Menu.sequelize.col("datum"), 
                "%d-%m "
              )
const listOfMenu = await Menu.findAll({ 
    order: [[datumExpression, 'ASC']],
    offset: amount - 5,
    limit: 5,
    attributes: {
        include: [
            [
              datumExpression,
              "datum",
            ],
          ]
     }
});

Also you can combine findAll and count queries into one findAndCountAll and you will get the result with two props: rows and count:

const { rows: listOfMenu, count: amount } = await Menu.findAndCountAll({ 
    order: [[datumExpression, 'ASC']],
    offset: amount - 5,
    limit: 5,
    attributes: {
        include: [
            [
              datumExpression,
              "datum",
            ],
          ]
     }

});

about 4 years ago · Juan Pablo Isaza Report
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!