The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import { combineTagIds } from "@/lib/api/tags/combine-tag-ids";
import { prisma } from "@/lib/prisma";
import { getLinksCountQuerySchema } from "@/lib/zod/schemas/links";
import * as z from "zod/v4";
interface GetLinksCountParams extends z.infer<typeof getLinksCountQuerySchema> {
workspaceId: string;
folderIds?: string[];
}
export async function getLinksCount({
groupBy,
search,
domain,
tagId,
tagIds,
tagNames,
userId,
showArchived,
withTags,
folderId,
tenantId,
workspaceId,
folderIds,
}: GetLinksCountParams) {
const combinedTagIds = combineTagIds({ tagId, tagIds });
const linksWhere = {
projectId: workspaceId,
AND: [
...(folderIds
? [
{
OR: [
{
folderId: {
in: folderIds,
},
},
{
folderId: null,
},
],
},
]
: groupBy !== "folderId"
? [
{
folderId: folderId || null,
},
]
: []),
...(search
? [
{
OR: [
{ shortLink: { contains: search } },
{ url: { contains: search } },
],
},
]
: []),
],
archived: showArchived ? undefined : false,
...(domain &&
groupBy !== "domain" && {
domain,
}),
...(userId &&
groupBy !== "userId" && {
userId,
}),
...(tenantId && { tenantId }),
};
if (groupBy === "tagId") {
return await prisma.linkTag.groupBy({
by: ["tagId"],
where: {
link: linksWhere,
},
_count: true,
orderBy: {
_count: {
tagId: "desc",
},
},
});
} else {
const where = {
...linksWhere,
...(withTags && {
tags: {
some: {},
},
}),
...(combinedTagIds && combinedTagIds.length > 0
? {
tags: { some: { tagId: { in: combinedTagIds } } },
}
: tagNames
? {
tags: {
some: {
tag: {
name: {
in: tagNames,
},
},
},
},
}
: {}),
};
if (
groupBy === "domain" ||
groupBy === "userId" ||
groupBy === "folderId"
) {
return await prisma.link.groupBy({
by: [groupBy],
where,
_count: true,
orderBy: {
_count: {
[groupBy]: "desc",
},
},
});
} else {
return await prisma.link.count({
where,
});
}
}
}