Vishal LoharV
Convex Community2y ago
2 replies
Vishal Lohar

Can we optimize this query?

export const getTrendingFilterTags = query({
  args: { platformType: v.string() },
  handler: async ({ db }, args) => {
    const dictionaryCategories = await getManyFrom(
      db,
      "dictionaryCategories",
      "by_platform_type",
      args.platformType,
      "platformType",
    );
    const categoryIds = new Set(
      dictionaryCategories.map((category) => category._id),
    );
    const dictionarySubCategories = await filter(
      db.query("dictionarySubCategories"),
      (subCategory) => categoryIds.has(subCategory.dictionaryCategoriesId),
    ).collect();
    const subCategoryIds = new Set(
      dictionarySubCategories.map((subCategory) => subCategory._id),
    );
    const dictionaryEntries = await filter(
      db.query("dictionaryEntries"),
      (entry) => subCategoryIds.has(entry.dictionarySubCategoriesId),
    ).collect();
    const entryIds = new Set(dictionaryEntries.map((entry) => entry._id));

    const bareTrendingTags = await filter(
      db.query("trendingFilterTags"),
      (tag) => entryIds.has(tag.dictionaryEntry),
    ).collect();

    const trendingTags = await asyncMap(bareTrendingTags, async (tag) => {
      const dictionaryEntry = dictionaryEntries.find(
        (entry) => entry._id === tag.dictionaryEntry,
      );
      const dictionarySubCategory = dictionarySubCategories.find(
        (subCategory) =>
          subCategory._id === dictionaryEntry?.dictionarySubCategoriesId,
      );
      const dictionaryCategory = dictionaryCategories.find(
        (category) =>
          category._id === dictionarySubCategory?.dictionaryCategoriesId,
      );
      return //Something

    return trendingTags;
  },
});
Was this page helpful?