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

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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