Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

improve recordLink + bulkDeleteLinks

Steven Teyaf157ee8edcc
diff --git a/apps/web/app/api/cron/cleanup/route.ts b/apps/web/app/api/cron/cleanup/route.ts
index aa71130cea..d90c37791d 100644
--- a/apps/web/app/api/cron/cleanup/route.ts
+++ b/apps/web/app/api/cron/cleanup/route.ts
@@ -31,7 +31,11 @@ export async function GET(req: Request) {
},
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
take: 100,
}),
@@ -63,9 +67,7 @@ export async function GET(req: Request) {
});
// Post delete cleanup
- await bulkDeleteLinks({
- links,
- });
+ await bulkDeleteLinks(links);
}
// Delete the domains
diff --git a/apps/web/app/api/cron/domains/delete/route.ts b/apps/web/app/api/cron/domains/delete/route.ts
index f75c969080..a50720551c 100644
--- a/apps/web/app/api/cron/domains/delete/route.ts
+++ b/apps/web/app/api/cron/domains/delete/route.ts
@@ -39,7 +39,11 @@ export async function POST(req: Request) {
domain,
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
take: 100, // TODO: We can adjust this number based on the performance
});
@@ -53,19 +57,7 @@ export async function POST(req: Request) {
linkCache.deleteMany(links),
// Record link in the Tinybird
- recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.id),
- workspace_id: workspaceId,
- program_id: link.programId ?? "",
- created_at: link.createdAt,
- deleted: true,
- })),
- ),
+ recordLink(links),
// Remove image from R2 storage if it exists
links
diff --git a/apps/web/app/api/cron/domains/transfer/route.ts b/apps/web/app/api/cron/domains/transfer/route.ts
index 00db26fbdd..c1d012c460 100644
--- a/apps/web/app/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/api/cron/domains/transfer/route.ts
@@ -72,16 +72,7 @@ export async function POST(req: Request) {
}),
recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: [],
- program_id: link.programId ?? "",
- workspace_id: newWorkspaceId,
- created_at: link.createdAt,
- })),
+ links.map((link) => ({ ...link, projectId: newWorkspaceId })),
),
]);
diff --git a/apps/web/app/api/cron/domains/update/route.ts b/apps/web/app/api/cron/domains/update/route.ts
index 8fbad9d4ac..b4c038a396 100644
--- a/apps/web/app/api/cron/domains/update/route.ts
+++ b/apps/web/app/api/cron/domains/update/route.ts
@@ -41,7 +41,11 @@ export async function POST(req: Request) {
domain: newDomain,
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
skip: (page - 1) * pageSize,
take: pageSize,
@@ -59,18 +63,7 @@ export async function POST(req: Request) {
}),
// update links in Tinybird
- recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
- })),
- ),
+ recordLink(links),
]);
await queueDomainUpdate({
diff --git a/apps/web/app/api/cron/workspaces/delete/route.ts b/apps/web/app/api/cron/workspaces/delete/route.ts
index a0bc8413a3..5715773230 100644
--- a/apps/web/app/api/cron/workspaces/delete/route.ts
+++ b/apps/web/app/api/cron/workspaces/delete/route.ts
@@ -37,7 +37,11 @@ export async function POST(req: Request) {
projectId: workspace.id,
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
take: 100, // TODO: We can adjust this number based on the performance
});
@@ -52,9 +56,7 @@ export async function POST(req: Request) {
},
}),
- bulkDeleteLinks({
- links,
- }),
+ bulkDeleteLinks(links),
]);
console.log(res);
diff --git a/apps/web/app/api/links/[linkId]/transfer/route.ts b/apps/web/app/api/links/[linkId]/transfer/route.ts
index f9f44c6f91..d39c5a2534 100644
--- a/apps/web/app/api/links/[linkId]/transfer/route.ts
+++ b/apps/web/app/api/links/[linkId]/transfer/route.ts
@@ -80,17 +80,7 @@ export const POST = withWorkspace(
waitUntil(
Promise.all([
linkCache.set({ ...link, projectId: newWorkspaceId }),
-
- recordLink({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: [],
- program_id: link.programId ?? "",
- workspace_id: newWorkspaceId,
- created_at: link.createdAt,
- }),
+ recordLink({ ...link, projectId: newWorkspaceId }),
// increment new workspace usage
prisma.project.update({
where: {
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index db5a774082..27caa97c08 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -395,7 +395,11 @@ export const DELETE = withWorkspace(
],
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
});
@@ -406,11 +410,7 @@ export const DELETE = withWorkspace(
},
});
- waitUntil(
- bulkDeleteLinks({
- links,
- }),
- );
+ waitUntil(bulkDeleteLinks(links));
return NextResponse.json(
{
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
index 93a41e13e5..deb2fe6448 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
@@ -26,7 +26,11 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
key: "_root",
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
},
users: {
@@ -138,14 +142,8 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
// record root domain link for all domains from Tinybird
recordLink(
workspaceLinks.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
+ ...link,
+ url: "",
})),
),
log({
diff --git a/apps/web/app/api/tags/[id]/route.ts b/apps/web/app/api/tags/[id]/route.ts
index c4c944136e..69ac1f4808 100644
--- a/apps/web/app/api/tags/[id]/route.ts
+++ b/apps/web/app/api/tags/[id]/route.ts
@@ -3,6 +3,7 @@ import { withWorkspace } from "@/lib/auth";
import { recordLink } from "@/lib/tinybird";
import { TagSchema, updateTagBodySchema } from "@/lib/zod/schemas/tags";
import { prisma } from "@dub/prisma";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// PATCH /api/workspaces/[idOrSlug]/tags/[id] – update a tag for a workspace
@@ -69,13 +70,12 @@ export const DELETE = withWorkspace(
links: {
select: {
link: {
- select: {
- id: true,
- domain: true,
- key: true,
- url: true,
- programId: true,
- createdAt: true,
+ include: {
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
},
},
@@ -91,17 +91,13 @@ export const DELETE = withWorkspace(
}
// update links metadata in tinybird after deleting a tag
- await recordLink(
- response.links.map(({ link }) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: [],
- program_id: link.programId ?? "",
- workspace_id: workspace.id,
- created_at: link.createdAt,
- })),
+ waitUntil(
+ recordLink(
+ response.links.map(({ link }) => ({
+ ...link,
+ tags: link.tags.filter(({ tag }) => tag.id !== id),
+ })),
+ ),
);
return NextResponse.json({ id });
diff --git a/apps/web/lib/actions/partners/invite-partner.ts b/apps/web/lib/actions/partners/invite-partner.ts
index 0374010cc8..6938342312 100644
--- a/apps/web/lib/actions/partners/invite-partner.ts
+++ b/apps/web/lib/actions/partners/invite-partner.ts
@@ -39,6 +39,5 @@ export const invitePartnerAction = authActionClient
email,
program,
link,
- workspace,
});
});
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
index 8a294416a4..29c2747652 100644
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@ -1,14 +1,10 @@
import { storage } from "@/lib/storage";
-import { recordLink } from "@/lib/tinybird";
+import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
import { R2_URL } from "@dub/utils";
-import { Link } from "@prisma/client";
import { linkCache } from "./cache";
+import { ExpandedLink } from "./utils";
-export async function bulkDeleteLinks({
- links,
-}: {
- links: (Link & { tags: { tagId: string }[] })[];
-}) {
+export async function bulkDeleteLinks(links: ExpandedLink[]) {
if (links.length === 0) {
return;
}
@@ -18,16 +14,9 @@ export async function bulkDeleteLinks({
linkCache.deleteMany(links),
// Record the links deletion in Tinybird
- recordLink(
+ recordLinkTB(
links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map(({ tagId }) => tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
+ ...transformLinkTB(link),
deleted: true,
})),
),
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index a89b907437..79a38f2767 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -137,16 +137,7 @@ export async function createLink(link: ProcessedLinkProps) {
// cache link in Redis
linkCache.set(response),
// record link in Tinybird
- recordLink({
- link_id: response.id,
- domain: response.domain,
- key: response.key,
- url: response.url,
- tag_ids: response.tags.map(({ tag }) => tag.id),
- program_id: link.programId ?? "",
- workspace_id: response.projectId,
- created_at: response.createdAt,
- }),
+ recordLink(response),
// Upload image to R2 and update the link with the uploaded image URL when
// proxy is enabled and image is set and not stored in R2
...(proxy && image && !isStored(image)
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index 197ebef7a1..28b8876cad 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -1,5 +1,5 @@
import { storage } from "@/lib/storage";
-import { recordLink } from "@/lib/tinybird";
+import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
@@ -11,7 +11,11 @@ export async function deleteLink(linkId: string) {
id: linkId,
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
});
@@ -26,15 +30,8 @@ export async function deleteLink(linkId: string) {
linkCache.delete(link),
// Record link in the Tinybird
- recordLink({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
+ recordLinkTB({
+ ...transformLinkTB(link),
deleted: true,
}),
]),
diff --git a/apps/web/lib/api/links/propagate-bulk-link-changes.ts b/apps/web/lib/api/links/propagate-bulk-link-changes.ts
index 0a0d6f94da..281450b0d7 100644
--- a/apps/web/lib/api/links/propagate-bulk-link-changes.ts
+++ b/apps/web/lib/api/links/propagate-bulk-link-changes.ts
@@ -7,17 +7,6 @@ export async function propagateBulkLinkChanges(links: ExpandedLink[]) {
// update Redis cache
linkCache.mset(links),
// update Tinybird
- recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags?.map(({ tag }) => tag.id) ?? [],
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
- })),
- ),
+ recordLink(links),
]);
}
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index c14a7fdc52..6931679999 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -164,16 +164,7 @@ export async function updateLink({
linkCache.set(response),
// record link in Tinybird
- recordLink({
- link_id: response.id,
- domain: response.domain,
- key: response.key,
- url: response.url,
- tag_ids: response.tags.map(({ tag }) => tag.id),
- program_id: response.programId ?? "",
- workspace_id: response.projectId,
- created_at: response.createdAt,
- }),
+ recordLink(response),
// if key is changed: delete the old key in Redis
(changedDomain || changedKey) && linkCache.delete(oldLink),
diff --git a/apps/web/lib/api/partners/invite-partner.ts b/apps/web/lib/api/partners/invite-partner.ts
index 92c74921af..0d83bf6aa0 100644
--- a/apps/web/lib/api/partners/invite-partner.ts
+++ b/apps/web/lib/api/partners/invite-partner.ts
@@ -2,7 +2,7 @@ import { updateConfig } from "@/lib/edge-config";
import { recordLink } from "@/lib/tinybird";
import { ProgramProps } from "@/lib/types";
import { prisma } from "@dub/prisma";
-import { Link, Project } from "@dub/prisma/client";
+import { Link } from "@dub/prisma/client";
import { sendEmail } from "emails";
import PartnerInvite from "emails/partner-invite";
import { createId } from "../utils";
@@ -10,12 +10,10 @@ import { createId } from "../utils";
export const invitePartner = async ({
email,
program,
- workspace,
link,
}: {
email: string;
program: ProgramProps;
- workspace: Project;
link: Link;
}) => {
const [programEnrollment, programInvite] = await Promise.all([
@@ -84,15 +82,9 @@ export const invitePartner = async ({
// record link update in tinybird
recordLink({
- domain: link.domain,
- key: link.key,
- link_id: link.id,
- created_at: link.createdAt,
- url: link.url,
- tag_ids: tags.map((t) => t.id) || [],
- program_id: program.id,
- workspace_id: workspace.id,
- deleted: false,
+ ...link,
+ tags: tags.map((t) => ({ tag: t })),
+ programId: program.id,
}),
// TODO: Remove this once we open up partners.dub.co to everyone
diff --git a/apps/web/lib/tinybird/record-link.ts b/apps/web/lib/tinybird/record-link.ts
index 9fd4175ba8..97593cdfad 100644
--- a/apps/web/lib/tinybird/record-link.ts
+++ b/apps/web/lib/tinybird/record-link.ts
@@ -1,4 +1,5 @@
import z from "@/lib/zod";
+import { ExpandedLink } from "../api/links";
import { tb } from "./client";
export const dubLinksMetadataSchema = z.object({
@@ -7,6 +8,10 @@ export const dubLinksMetadataSchema = z.object({
key: z.string(),
url: z.string().default(""),
tag_ids: z.array(z.string()).default([]),
+ tenant_id: z
+ .string()
+ .nullable()
+ .transform((v) => (v === null ? "" : v)),
program_id: z.string().default(""),
workspace_id: z
.string()
@@ -28,8 +33,30 @@ export const dubLinksMetadataSchema = z.object({
.transform((v) => (v ? 1 : 0)),
});
-export const recordLink = tb.buildIngestEndpoint({
+export const recordLinkTB = tb.buildIngestEndpoint({
datasource: "dub_links_metadata",
event: dubLinksMetadataSchema,
wait: true,
});
+
+export const transformLinkTB = (link: ExpandedLink) => {
+ return {
+ link_id: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ tag_ids: link.tags?.map(({ tag }) => tag.id),
+ tenant_id: link.tenantId ?? "",
+ program_id: link.programId ?? "",
+ workspace_id: link.projectId,
+ created_at: link.createdAt,
+ };
+};
+
+export const recordLink = async (payload: ExpandedLink | ExpandedLink[]) => {
+ if (Array.isArray(payload)) {
+ return await recordLinkTB(payload.map(transformLinkTB));
+ } else {
+ return await recordLinkTB(transformLinkTB(payload));
+ }
+};
diff --git a/apps/web/scripts/bulk-delete-links.ts b/apps/web/scripts/bulk-delete-links.ts
index e7ab43ea17..8b77b5a146 100644
--- a/apps/web/scripts/bulk-delete-links.ts
+++ b/apps/web/scripts/bulk-delete-links.ts
@@ -1,4 +1,4 @@
-import { recordLink } from "@/lib/tinybird";
+import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
@@ -9,15 +9,12 @@ async function main() {
where: {
domain,
},
- select: {
- id: true,
- domain: true,
- key: true,
- url: true,
- projectId: true,
- programId: true,
- tags: true,
- createdAt: true,
+ include: {
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
// take: 10000,
});
@@ -31,16 +28,9 @@ async function main() {
},
}),
// redis.del(domain),
- recordLink(
+ recordLinkTB(
links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
+ ...transformLinkTB(link),
deleted: true,
})),
),
diff --git a/apps/web/scripts/sync-links-metadata.ts b/apps/web/scripts/sync-links-metadata.ts
index 5293263507..26d7cf052f 100644
--- a/apps/web/scripts/sync-links-metadata.ts
+++ b/apps/web/scripts/sync-links-metadata.ts
@@ -8,7 +8,11 @@ const limit = 20000;
async function main() {
const links = await prisma.link.findMany({
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
orderBy: [
{
@@ -46,18 +50,7 @@ async function main() {
// }));
// });
- const res = await recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
- })),
- );
+ const res = await recordLink(links);
console.log(res);
}
diff --git a/apps/web/scripts/sync-tag-analytics.ts b/apps/web/scripts/sync-tag-analytics.ts
index 0ae3663a95..d97e7c14ba 100644
--- a/apps/web/scripts/sync-tag-analytics.ts
+++ b/apps/web/scripts/sync-tag-analytics.ts
@@ -10,7 +10,11 @@ async function main() {
},
},
include: {
- tags: true,
+ tags: {
+ select: {
+ tag: true,
+ },
+ },
},
orderBy: {
createdAt: "asc",
@@ -19,18 +23,7 @@ async function main() {
take: 1000,
});
- const res = await recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
- })),
- );
+ const res = await recordLink(links);
console.log(res);
}