根据内容处理嵌套对象(如果嵌套对象包含数据则继续映射)

问题描述 投票:0回答:0

我正在尝试从我的本地数据库中删除内容。我设法添加了它们,但我还需要删除它们(问题),我已经像下面这样构造了本地数据库。
Structure
我有4张桌子(西瓜DB):

1 - Main Table
2 - SubMain Table
3 - Folder Table
4 - File Table

我的应用程序如何工作:

- Main Table 1 (main_id)
  - SubMain Table 1(submain_id, HAS A REFERENCE TO THE PAST FOLDER: main_id)
    - File 1 (file_id, , HAS A REFERENCE TO THE PAST FOLDER: submain_id)
    - Folder 1 (folder_id, HAS A REFERENCE TO THE PAST FOLDER: submain_id) 
      - File 1 (file_id, , HAS A REFERENCE TO THE PAST FOLDER: folder_id)   
      - Folder 1 (folder_id, HAS A REFERENCE TO THE PAST FOLDER: folder_id)      
        - Folder ...
        - File ...
          ...  
- Main Table 2 (main_id)
  - SubMain Table 2 (submain_id, HAS A REFERENCE TO THE PAST FOLDER: main_id)
    - File 2 (file_id , HAS A REFERENCE TO THE PAST FOLDER: submain_id)
    - Folder 2 (folder_id, HAS A REFERENCE TO THE PAST FOLDER: submain_id)    
...   

如您所见,我有这些嵌套表格。问题是文件夹,因为看到我可以在每个文件夹中包含其他文件夹和文件,我需要保留

mapping
或沿着结构向下移动,直到没有其他文件夹为止。 (我唯一能找到我需要删除的内容的方法是从
past folder
,因为所有文件夹和文件都包含过去的文件夹Id,所以我可以根据它来识别我需要删除哪些元素。
这就是我的想法:

export async function removeFilesInDatabase(item: any) {
  const isProject = item?.direction?.typeA;
  if (isProject) {
    const response = await readAllFromLocalStorage<PastasModel>(
      'pastasDisciplina',
      Q.where('pastFolder', item._id)
    );
    await Promise.all(
      response.map(async (pastasDisciplina) => {
        const arquivos = await readAllFromLocalStorage<ProjetosArquivoModel>(
          'projetosArquivos',
          Q.where('pastFolder', pastasDisciplina._id)
        ); 

        const pastas = await readAllFromLocalStorage<ProjetosPastaModel>(
          'projetosPastas',
          Q.where('pastFolder', pastasDisciplina._id)
        );

        (Heres the issue, if I have pastas, I would need to execute both of this logics 
to check and if there are more pastas and arquivos again, and again until there are no more pastas) Something like:
       if (pastas.length > 0)
       {
         const pastas = await readAllFromLocalStorage<ProjetosPastaModel>(
           'projetosPastas',
           Q.where('pastFolder', pastasDisciplina._id)
         );
           const arquivos = await readAllFromLocalStorage<ProjetosArquivoModel>(
            'projetosArquivos',
             Q.where('pastFolder', pastasDisciplina._id)
            );
          (if pastas do again)
        }  
        // My logic to delete will be under here     
      })
    );
  }
}
javascript arrays typescript object multidimensional-array
© www.soinside.com 2019 - 2024. All rights reserved.