Skip to content

Performance concern about getAllParentsForFolder using while loop #5

Description

@EricTsai83

I have a question regarding the implementation of getAllParentsForFolder in queries.ts. Currently, it uses a while loop to retrieve all parent folders. I'm wondering if this approach could impact performance, especially when dealing with deeply nested structures.

  getAllParentsForFolder: async function (folderId: number) {
    const parents = [];
    let currentId: number | null = folderId;
    while (currentId !== null) {
      const folder = await db
        .selectDistinct()
        .from(foldersSchema)
        .where(eq(foldersSchema.id, currentId));

      if (!folder[0]) {
        throw new Error("Parent folder not found");
      }
      parents.unshift(folder[0]);
      currentId = folder[0]?.parent;
    }
    return parents;
  },

Is there a more efficient way to achieve the same result?
Is SQL CTE better for this type of query by using sql operator? Ex.QUERIES-WITH-RECURSIVE
I’d appreciate any insights or recommendations. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions