A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
refactor: optimze the lists stats query for manual lists
Mohamed Bassem44ea02845da7
diff --git a/packages/trpc/models/lists.ts b/packages/trpc/models/lists.tsindex baf13e1c..6dd479c7 100644--- a/packages/trpc/models/lists.ts+++ b/packages/trpc/models/lists.ts@@ -287,6 +287,39 @@ export abstract class List {return [...ownedLists, ...sharedLists];}+ static async getSizes(+ ctx: AuthedContext,+ lists: readonly (ManualList | SmartList)[],+ ): Promise<Map<string, number>> {+ const manualListIds = lists+ .filter((list) => list.type === "manual")+ .map((list) => list.id);+ const smartLists = lists.filter((list) => list.type === "smart");++ const [manualSizes, smartSizes] = await Promise.all([+ manualListIds.length > 0+ ? ctx.db+ .select({+ listId: bookmarksInLists.listId,+ size: count(),+ })+ .from(bookmarksInLists)+ .where(inArray(bookmarksInLists.listId, manualListIds))+ .groupBy(bookmarksInLists.listId)+ : Promise.resolve([]),+ Promise.all(+ smartLists.map(+ async (list) => [list.id, await list.getSize()] as const,+ ),+ ),+ ]);++ const sizes = new Map(lists.map((list) => [list.id, 0]));+ manualSizes.forEach(({ listId, size }) => sizes.set(listId, size));+ smartSizes.forEach(([listId, size]) => sizes.set(listId, size));+ return sizes;+ }+static async getAllOwned(ctx: AuthedContext,): Promise<(ManualList | SmartList)[]> {diff --git a/packages/trpc/routers/lists.ts b/packages/trpc/routers/lists.tsindex 8944c914..8f82931c 100644--- a/packages/trpc/routers/lists.ts+++ b/packages/trpc/routers/lists.ts@@ -196,8 +196,7 @@ export const listsAppRouter = router({).query(async ({ ctx }) => {const lists = await List.getAll(ctx);- const sizes = await Promise.all(lists.map((l) => l.getSize()));- return { stats: new Map(lists.map((l, i) => [l.id, sizes[i]])) };+ return { stats: await List.getSizes(ctx, lists) };}),// Rss endpoints