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.

fix bulk creation

Steven Tey547f70b2b50f
diff --git a/apps/web/app/api/callback/stripe/route.ts b/apps/web/app/api/callback/stripe/route.ts
index 7c44516b99..4ce82484eb 100644
--- a/apps/web/app/api/callback/stripe/route.ts
+++ b/apps/web/app/api/callback/stripe/route.ts
@@ -221,13 +221,13 @@ export const POST = async (req: Request) => {
const projectUsers = project.users.map(
({ user }) => user.email as string,
);
- const projectDomains = project.domains.map((domain) => domain.slug);
const pipeline = redis.pipeline();
// remove root domain redirect for all domains
- projectDomains.forEach((domain) => {
- pipeline.hset(domain, {
+ project.domains.forEach((domain) => {
+ pipeline.hset(domain.slug, {
_root: {
+ id: domain.id,
projectId: project.id,
},
});
diff --git a/apps/web/app/api/cron/links/event/route.ts b/apps/web/app/api/cron/links/event/route.ts
index b65a1ef92a..d585a245f2 100644
--- a/apps/web/app/api/cron/links/event/route.ts
+++ b/apps/web/app/api/cron/links/event/route.ts
@@ -67,7 +67,7 @@ export async function POST(req: Request) {
// increment links usage and send alert if needed
if (type === "create" && link.projectId) {
- const [project, _redisLink] = await Promise.all([
+ const [project, _] = await Promise.all([
prisma.project.update({
where: {
id: link.projectId,
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 9372b37da7..faa712eff5 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -462,48 +462,27 @@ export async function addLink(link: LinkProps) {
export async function bulkCreateLinks(links: LinkProps[]) {
if (links.length === 0) return [];
- // split links into domains
- const linksByDomain: Record<string, Record<string, RedisLinkProps>> = {};
- links.forEach(async (link) => {
- const { domain, key } = link;
-
- if (!linksByDomain[domain]) {
- linksByDomain[domain] = {};
- }
- // this technically will be a synchronous function since isIframeable won't be run for bulk link creation
- linksByDomain[domain][key.toLowerCase()] = await formatRedisLink(link);
- });
-
- const pipeline = redis.pipeline();
-
- Object.entries(linksByDomain).forEach(([domain, links]) => {
- pipeline.hset(domain, links);
- });
-
- await Promise.all([
- prisma.link.createMany({
- data: links.map((link) => {
- const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
- getParamsFromURL(link.url);
- return {
- ...link,
- title: truncate(link.title, 120),
- description: truncate(link.description, 240),
- utm_source,
- utm_medium,
- utm_campaign,
- utm_term,
- utm_content,
- expiresAt: link.expiresAt ? new Date(link.expiresAt) : null,
- geo: link.geo || undefined,
- };
- }),
- skipDuplicates: true,
+ await prisma.link.createMany({
+ data: links.map((link) => {
+ const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
+ getParamsFromURL(link.url);
+ return {
+ ...link,
+ title: truncate(link.title, 120),
+ description: truncate(link.description, 240),
+ utm_source,
+ utm_medium,
+ utm_campaign,
+ utm_term,
+ utm_content,
+ expiresAt: link.expiresAt ? new Date(link.expiresAt) : null,
+ geo: link.geo || undefined,
+ };
}),
- pipeline.exec(),
- ]);
+ skipDuplicates: true,
+ });
- const createdLinks = await Promise.all(
+ const createdLinks = (await Promise.all(
links.map(async (link) => {
const { key, domain } = link;
return await prisma.link.findUnique({
@@ -515,8 +494,33 @@ export async function bulkCreateLinks(links: LinkProps[]) {
},
});
}),
+ )) as LinkProps[];
+
+ const pipeline = redis.pipeline();
+
+ // split links into domains
+ const linksByDomain: Record<string, Record<string, RedisLinkProps>> = {};
+
+ // this technically will be a synchronous function since isIframeable won't be run for bulk link creation
+ await Promise.all(
+ createdLinks.map(async (link) => {
+ const { domain, key } = link;
+
+ if (!linksByDomain[domain]) {
+ linksByDomain[domain] = {};
+ }
+ const formattedLink = await formatRedisLink(link);
+
+ linksByDomain[domain][key.toLowerCase()] = formattedLink;
+ }),
);
+ Object.entries(linksByDomain).forEach(([domain, links]) => {
+ pipeline.hset(domain, links);
+ });
+
+ await pipeline.exec();
+
return createdLinks.map((link) => ({
...link,
shortLink: linkConstructor({