Skip to content

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

feat(web): improved performance for the dashboard and search route

daniel31x13ba3c613c267a
diff --git a/apps/web/lib/api/controllers/dashboard/getDashboardData.ts b/apps/web/lib/api/controllers/dashboard/getDashboardData.ts
index 25abf37d..6fc27ae6 100644
--- a/apps/web/lib/api/controllers/dashboard/getDashboardData.ts
+++ b/apps/web/lib/api/controllers/dashboard/getDashboardData.ts
@@ -1,5 +1,6 @@
import { prisma } from "@linkwarden/prisma";
import { LinkRequestQuery, Order, Sort } from "@linkwarden/types/global";
+import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";
export default async function getDashboardData(
userId: number,
@@ -11,21 +12,14 @@ export default async function getDashboardData(
else if (query.sort === Sort.NameAZ) order = { name: "asc" };
else if (query.sort === Sort.NameZA) order = { name: "desc" };
+ const { accessibleCollectionIds } = await getAccessibleCollectionIds(userId);
+
const pinnedLinks = await prisma.link.findMany({
take: 10,
where: {
AND: [
{
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
+ collectionId: { in: accessibleCollectionIds },
},
{
pinnedBy: { some: { id: userId } },
@@ -46,16 +40,7 @@ export default async function getDashboardData(
const recentlyAddedLinks = await prisma.link.findMany({
take: 10,
where: {
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
+ collectionId: { in: accessibleCollectionIds },
},
include: {
tags: true,
diff --git a/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts b/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts
index 3df3223a..fcff90ad 100644
--- a/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts
+++ b/apps/web/lib/api/controllers/dashboard/getDashboardDataV2.ts
@@ -1,19 +1,19 @@
import { prisma } from "@linkwarden/prisma";
import { Order } from "@linkwarden/types/global";
+import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";
export default async function getDashboardData(userId: number) {
const order: Order = { id: "desc" };
+ const { accessibleCollectionIds, memberCollectionIds } =
+ await getAccessibleCollectionIds(userId);
+ const accessibleCollectionIdSet = new Set(accessibleCollectionIds);
const accessibleTagsWhere = {
OR: [
{ ownerId: userId },
{
links: {
some: {
- collection: {
- members: {
- some: { userId },
- },
- },
+ collectionId: { in: memberCollectionIds },
},
},
},
@@ -26,11 +26,7 @@ export default async function getDashboardData(userId: number) {
prisma.link.count({
where: {
AND: [
- {
- collection: {
- OR: [{ ownerId: userId }, { members: { some: { userId } } }],
- },
- },
+ { collectionId: { in: accessibleCollectionIds } },
{ pinnedBy: { some: { id: userId } } },
],
},
@@ -65,11 +61,7 @@ export default async function getDashboardData(userId: number) {
take: 16,
where: {
AND: [
- {
- collection: {
- OR: [{ ownerId: userId }, { members: { some: { userId } } }],
- },
- },
+ { collectionId: { in: accessibleCollectionIds } },
{ pinnedBy: { some: { id: userId } } },
],
},
@@ -90,9 +82,7 @@ export default async function getDashboardData(userId: number) {
? prisma.link.findMany({
take: 16,
where: {
- collection: {
- OR: [{ ownerId: userId }, { members: { some: { userId } } }],
- },
+ collectionId: { in: accessibleCollectionIds },
},
omit: { textContent: true },
include: {
@@ -112,31 +102,23 @@ export default async function getDashboardData(userId: number) {
.filter((id): id is number => id != null);
const collectionPromises = collectionIds.map((colId) =>
- prisma.link
- .findMany({
- where: {
- AND: [
- {
- collection: {
- id: colId,
- OR: [{ ownerId: userId }, { members: { some: { userId } } }],
- },
+ (accessibleCollectionIdSet.has(colId)
+ ? prisma.link.findMany({
+ where: { collectionId: colId },
+ take: 16,
+ 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,
- })
- .then((links) => ({ colId, links }))
+ orderBy: order,
+ })
+ : Promise.resolve([] as any[])
+ ).then((links) => ({ colId, links }))
);
const [pinnedLinks, recentlyAddedLinks, ...collectionsResult] =
diff --git a/apps/web/lib/api/controllers/links/getLinks.ts b/apps/web/lib/api/controllers/links/getLinks.ts
index 8ba18a83..16e08418 100644
--- a/apps/web/lib/api/controllers/links/getLinks.ts
+++ b/apps/web/lib/api/controllers/links/getLinks.ts
@@ -1,5 +1,6 @@
import { prisma } from "@linkwarden/prisma";
import { LinkRequestQuery, Order, Sort } from "@linkwarden/types/global";
+import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";
export default async function getLink(userId: number, query: LinkRequestQuery) {
if (process.env.DISABLE_DEPRECATED_ROUTES === "true")
@@ -12,6 +13,9 @@ export default async function getLink(userId: number, query: LinkRequestQuery) {
const POSTGRES_IS_ENABLED =
process.env.DATABASE_URL?.startsWith("postgresql");
+ const { accessibleCollectionIds, memberCollectionIds } =
+ await getAccessibleCollectionIds(userId);
+
let order: Order = { id: "desc" };
if (query.sort === Sort.DateNewestFirst) order = { id: "desc" };
else if (query.sort === Sort.DateOldestFirst) order = { id: "asc" };
@@ -54,11 +58,7 @@ export default async function getLink(userId: number, query: LinkRequestQuery) {
{
links: {
some: {
- collection: {
- members: {
- some: { userId },
- },
- },
+ collectionId: { in: memberCollectionIds },
},
},
},
@@ -97,16 +97,7 @@ export default async function getLink(userId: number, query: LinkRequestQuery) {
where: {
AND: [
{
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
+ collectionId: { in: accessibleCollectionIds },
},
...collectionCondition,
{
diff --git a/apps/web/lib/api/controllers/search/searchLinks.ts b/apps/web/lib/api/controllers/search/searchLinks.ts
index cfee689a..9aa5dc32 100644
--- a/apps/web/lib/api/controllers/search/searchLinks.ts
+++ b/apps/web/lib/api/controllers/search/searchLinks.ts
@@ -6,6 +6,7 @@ import {
buildMeiliQuery,
parseSearchTokens,
} from "../../searchQueryBuilder";
+import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";
interface SearchLinksParams {
query: LinkRequestQuery;
@@ -23,6 +24,17 @@ export default async function searchLinks({
const paginationTakeCount = Number(process.env.PAGINATION_TAKE_COUNT) || 50;
+ const accessibleCollectionIdsPromise = userId
+ ? getAccessibleCollectionIds(userId)
+ : null;
+ accessibleCollectionIdsPromise?.catch(() => {});
+
+ const resolveAccessCondition = async () => {
+ if (!accessibleCollectionIdsPromise) return [];
+ const { accessibleCollectionIds } = await accessibleCollectionIdsPromise;
+ return [{ collectionId: { in: accessibleCollectionIds } }];
+ };
+
let order: Order = { id: "desc" };
if (query.sort === Sort.DateNewestFirst) order = { id: "desc" };
else if (query.sort === Sort.DateOldestFirst) order = { id: "asc" };
@@ -92,26 +104,13 @@ export default async function searchLinks({
const meiliIds = meiliResp.hits.map((h: any) => h.id);
+ const accessCondition = await resolveAccessCondition();
+
const links = await prisma.link.findMany({
where: {
id: { in: meiliIds },
AND: [
- ...(userId
- ? [
- {
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
- },
- ]
- : []),
+ ...accessCondition,
...collectionCondition,
{
OR: [
@@ -189,28 +188,15 @@ export default async function searchLinks({
});
}
+ const accessCondition = await resolveAccessCondition();
+
const links = await prisma.link.findMany({
take: paginationTakeCount,
skip: query.cursor ? 1 : undefined,
cursor: query.cursor ? { id: query.cursor } : undefined,
where: {
AND: [
- ...(userId
- ? [
- {
- collection: {
- OR: [
- { ownerId: userId },
- {
- members: {
- some: { userId },
- },
- },
- ],
- },
- },
- ]
- : []),
+ ...accessCondition,
...collectionCondition,
{
OR: [
diff --git a/apps/web/lib/api/controllers/tags/tagId/getTagById.ts b/apps/web/lib/api/controllers/tags/tagId/getTagById.ts
index 0c501769..dad4c1f9 100644
--- a/apps/web/lib/api/controllers/tags/tagId/getTagById.ts
+++ b/apps/web/lib/api/controllers/tags/tagId/getTagById.ts
@@ -1,9 +1,12 @@
import { prisma } from "@linkwarden/prisma";
+import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";
export default async function getTagById(userId: number, tagId: number) {
if (!tagId)
return { response: "Please choose a valid name for the tag.", status: 401 };
+ const { memberCollectionIds } = await getAccessibleCollectionIds(userId);
+
const tag = await prisma.tag.findFirst({
where: {
id: tagId,
@@ -12,13 +15,7 @@ export default async function getTagById(userId: number, tagId: number) {
{
links: {
some: {
- collection: {
- members: {
- some: {
- userId,
- },
- },
- },
+ collectionId: { in: memberCollectionIds },
},
},
},
diff --git a/apps/web/lib/api/controllers/users/userId/getUserById.ts b/apps/web/lib/api/controllers/users/userId/getUserById.ts
index e1b6071c..6d97fb63 100644
--- a/apps/web/lib/api/controllers/users/userId/getUserById.ts
+++ b/apps/web/lib/api/controllers/users/userId/getUserById.ts
@@ -1,9 +1,14 @@
-import { MEILI_INDEX_VERSION } from "@linkwarden/lib/constants";
+// import { MEILI_INDEX_VERSION } from "@linkwarden/lib/constants";
import { prisma } from "@linkwarden/prisma";
import { GetUserByIdResponse } from "@linkwarden/types/global";
+// import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";
export default async function getUserById(userId: number) {
- const [user, firstUnIndexedLinks, oauthAccountCount] = await Promise.all([
+ const [
+ user,
+ // firstUnIndexedLinks,
+ oauthAccountCount,
+ ] = await Promise.all([
prisma.user.findUnique({
where: {
id: userId,
@@ -18,17 +23,17 @@ export default async function getUserById(userId: number) {
dashboardSections: true,
},
}),
- prisma.link.findFirst({
- where: {
- collection: {
- OR: [{ ownerId: userId }, { members: { some: { userId } } }],
- },
- OR: [
- { indexVersion: null },
- { NOT: { indexVersion: MEILI_INDEX_VERSION } },
- ],
- },
- }),
+ // getAccessibleCollectionIds(userId).then(({ accessibleCollectionIds }) =>
+ // prisma.link.findFirst({
+ // where: {
+ // collectionId: { in: accessibleCollectionIds },
+ // OR: [
+ // { indexVersion: null },
+ // { NOT: { indexVersion: MEILI_INDEX_VERSION } },
+ // ],
+ // },
+ // })
+ // ),
prisma.account.count({
where: {
userId,
@@ -62,7 +67,7 @@ export default async function getUserById(userId: number) {
},
hasPassword: !!password,
hasOAuthAccount: oauthAccountCount.provider > 0,
- hasUnIndexedLinks: !!firstUnIndexedLinks,
+ // hasUnIndexedLinks: !!firstUnIndexedLinks,
};
return { response: data, status: 200 };
diff --git a/apps/web/lib/api/getAccessibleCollectionIds.ts b/apps/web/lib/api/getAccessibleCollectionIds.ts
new file mode 100644
index 00000000..6d81939d
--- /dev/null
+++ b/apps/web/lib/api/getAccessibleCollectionIds.ts
@@ -0,0 +1,38 @@
+import { prisma } from "@linkwarden/prisma";
+
+/**
+ * Resolves the collections a user can access (owned + shared with them).
+ *
+ * Link queries should filter on `collectionId: { in: accessibleCollectionIds }`
+ * instead of the nested `collection: { OR: [{ ownerId }, { members: ... }] }`
+ * relation filter. Both produce the exact same result set, but the id list
+ * hits the existing `Link.collectionId` index instead of running a correlated
+ * subquery per link.
+ *
+ * ownedCollectionIds: Collections the user owns
+ * memberCollectionIds: Collections the user is a member of
+ * accessibleCollectionIds: Collections the user is either part of or a member of
+ */
+export default async function getAccessibleCollectionIds(userId: number) {
+ const [ownedCollections, memberships] = await Promise.all([
+ prisma.collection.findMany({
+ where: { ownerId: userId },
+ select: { id: true },
+ }),
+ prisma.usersAndCollections.findMany({
+ where: { userId },
+ select: { collectionId: true },
+ }),
+ ]);
+
+ const ownedCollectionIds = ownedCollections.map((c) => c.id);
+ const memberCollectionIds = memberships.map((m) => m.collectionId);
+
+ return {
+ ownedCollectionIds,
+ memberCollectionIds,
+ accessibleCollectionIds: Array.from(
+ new Set([...ownedCollectionIds, ...memberCollectionIds])
+ ),
+ };
+}
diff --git a/packages/types/global.ts b/packages/types/global.ts
index e8739f0a..6b25b11f 100644
--- a/packages/types/global.ts
+++ b/packages/types/global.ts
@@ -240,5 +240,5 @@ export type GetUserByIdResponse = Omit<User, "password"> &
dashboardSections: DashboardSection[];
hasPassword: boolean;
hasOAuthAccount: boolean;
- hasUnIndexedLinks: boolean;
+ // hasUnIndexedLinks: boolean;
};