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

235
Views
Sequelize, lazy loading on associated models

I am using sequelize 6.17

I have a class Folders which has many childFolders and files.

I would like to lazy load the childFolders and files that are on the association.

const folderContent = await db.folders.findAll({
            where: {
               parentFolderId: 123
            },
            limit,
            offset,
            include: [
               {
                  model: db.folders,
                  as: "childFolders",
                  required: false
               },
               {
                  model: db.files,
                  as: "files",
                  required: false
               }
            ],
         });

This will not work and will return folderContent.files and folderContent.childFolders.

Would be possible to get x amount of childFolders and when there is not anymore childFolders to start getting the files? Can this be returned as an array of childFolders and files?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

By indicating include option you perform eager loading and you will get all-in-one. If you wish to get childFolders or files separately for a certain folder model instance you need to call special methods getChildFolders or getFiles (generated by Sequelize):

const folderContent = await db.folders.findAll({
            where: {
               parentFolderId: 123
            },
            limit,
            offset,
         });

for (const folder of folders) {
  const files = await folder.getFiles();
  const childFolders = await folder.getChildFolders();
}
over 4 years ago · Santiago Trujillo 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!