Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

improved performance

daniel31x13e08684328ace
diff --git a/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts b/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts
index 32b1456d..1e897855 100644
--- a/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts
+++ b/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts
@@ -2,183 +2,140 @@ import { prisma } from "@linkwarden/prisma";
import { Order } from "@linkwarden/types";
export default async function getDashboardData(userId: number) {
- let order: Order = { id: "desc" };
+ const order: Order = { id: "desc" };
- const dashboardSections = await prisma.dashboardSection.findMany({
- where: {
- userId,
- },
- });
+ const [dashboardSections, numberOfPinnedLinks] = await Promise.all([
+ prisma.dashboardSection.findMany({ where: { userId } }),
+ prisma.link.count({
+ where: {
+ AND: [
+ {
+ collection: {
+ OR: [{ ownerId: userId }, { members: { some: { userId } } }],
+ },
+ },
+ { pinnedBy: { some: { id: userId } } },
+ ],
+ },
+ }),
+ ]);
const viewPinned = dashboardSections.some(
(section) => section.type === "PINNED_LINKS"
);
-
const viewRecent = dashboardSections.some(
(section) => section.type === "RECENT_LINKS"
);
-
const collectionSections = dashboardSections.filter(
(section) => section.type === "COLLECTION"
);
- const numberOfPinnedLinks = await prisma.link.count({
- where: {
- AND: [
- {
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
- },
- {
- pinnedBy: { some: { id: userId } },
- },
- ],
- },
- });
-
if (!viewRecent && !viewPinned && collectionSections.length === 0) {
return {
- data: {
- links: [],
- numberOfPinnedLinks,
- },
+ data: { links: [], numberOfPinnedLinks },
message: "Dashboard data fetched successfully.",
statusCode: 200,
success: true,
};
}
- let pinnedLinks: any[] = [];
-
- if (viewPinned) {
- pinnedLinks = await prisma.link.findMany({
- take: 16,
- where: {
- AND: [
- {
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
+ // Prepare promises for pinned and recent links
+ const pinnedLinksPromise = viewPinned
+ ? prisma.link.findMany({
+ take: 16,
+ where: {
+ AND: [
+ {
+ collection: {
+ OR: [{ ownerId: userId }, { members: { some: { userId } } }],
+ },
},
+ { pinnedBy: { some: { id: userId } } },
+ ],
+ },
+ omit: { textContent: true },
+ include: {
+ tags: true,
+ collection: true,
+ pinnedBy: {
+ where: { id: userId },
+ select: { id: true },
},
- {
- pinnedBy: { some: { id: userId } },
+ },
+ orderBy: order,
+ })
+ : Promise.resolve([] as any[]);
+
+ const recentLinksPromise = viewRecent
+ ? prisma.link.findMany({
+ take: 16,
+ where: {
+ collection: {
+ OR: [{ ownerId: userId }, { members: { some: { userId } } }],
},
- ],
- },
- omit: {
- textContent: true,
- },
- include: {
- tags: true,
- collection: true,
- pinnedBy: {
- where: { id: userId },
- select: { id: true },
},
- },
- orderBy: order || { id: "desc" },
- });
- }
-
- let recentlyAddedLinks: any[] = [];
-
- if (viewRecent) {
- recentlyAddedLinks = await prisma.link.findMany({
- take: 16,
- where: {
- collection: {
- OR: [
- { ownerId: userId },
+ omit: { textContent: true },
+ include: {
+ tags: true,
+ collection: true,
+ pinnedBy: {
+ where: { id: userId },
+ select: { id: true },
+ },
+ },
+ orderBy: order,
+ })
+ : Promise.resolve([] as any[]);
+
+ const collectionIds = collectionSections
+ .map((section) => section.collectionId)
+ .filter((id): id is number => id != null);
+
+ const collectionPromises = collectionIds.map((colId) =>
+ prisma.link
+ .findMany({
+ where: {
+ AND: [
{
- members: {
- some: { userId },
+ collection: {
+ id: colId,
+ OR: [{ ownerId: userId }, { members: { some: { userId } } }],
},
},
],
},
- },
- omit: {
- textContent: true,
- },
- include: {
- tags: true,
- collection: true,
- pinnedBy: {
- where: { id: userId },
- select: { id: true },
+ take: 16,
+ omit: { textContent: true },
+ include: {
+ tags: true,
+ collection: true,
+ pinnedBy: {
+ where: { id: userId },
+ select: { id: true },
+ },
},
- },
- orderBy: order || { id: "desc" },
- });
- }
-
- let collectionLinks: any = {};
+ orderBy: order,
+ })
+ .then((links) => ({ colId, links }))
+ );
- if (collectionSections.length > 0) {
- const collectionIds = collectionSections
- .filter((section) => section.collectionId !== null)
- .map((section) => section.collectionId!);
+ const [pinnedLinks, recentlyAddedLinks, ...collectionsResult] =
+ await Promise.all([
+ pinnedLinksPromise,
+ recentLinksPromise,
+ ...collectionPromises,
+ ]);
- if (collectionIds.length > 0) {
- for (const collectionId of collectionIds) {
- const links = await prisma.link.findMany({
- where: {
- AND: [
- {
- collection: {
- id: collectionId,
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
- },
- ],
- },
- take: 16,
- omit: {
- textContent: true,
- },
- include: {
- tags: true,
- collection: true,
- pinnedBy: {
- where: { id: userId },
- select: { id: true },
- },
- },
- orderBy: order || { id: "desc" },
- });
- collectionLinks[collectionId] = links;
- }
- }
- }
+ const collectionLinks: Record<number, any[]> = {};
+ collectionsResult.forEach(({ colId, links }) => {
+ collectionLinks[colId] = links;
+ });
- const links = [...recentlyAddedLinks, ...pinnedLinks].sort(
+ const merged = [...recentlyAddedLinks, ...pinnedLinks].sort(
(a, b) => new Date(b.id).getTime() - new Date(a.id).getTime()
);
-
- const uniqueLinks = links.filter(
- (link, index, self) => index === self.findIndex((t) => t.id === link.id)
+ const uniqueLinks = merged.filter(
+ (link, idx, arr) => idx === arr.findIndex((l) => l.id === link.id)
);
return {
diff --git a/packages/prisma/migrations/20250627164227_add_index_to_link_model/migration.sql b/packages/prisma/migrations/20250627164227_add_index_to_link_model/migration.sql
new file mode 100644
index 00000000..6f4be487
--- /dev/null
+++ b/packages/prisma/migrations/20250627164227_add_index_to_link_model/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX "Link_collectionId_idx" ON "Link"("collectionId");
diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma
index 1cd005a5..fa3903f1 100644
--- a/packages/prisma/schema.prisma
+++ b/packages/prisma/schema.prisma
@@ -188,6 +188,8 @@ model Link {
importDate DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
+
+ @@index([collectionId])
}
model Tag {