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 nested createMany issue

Steven Tey45d67503467f
diff --git a/apps/web/app/api/cron/import/rebrandly/utils.ts b/apps/web/app/api/cron/import/rebrandly/utils.ts
index 3552946ff0..9d057ce5f6 100644
--- a/apps/web/app/api/cron/import/rebrandly/utils.ts
+++ b/apps/web/app/api/cron/import/rebrandly/utils.ts
@@ -172,7 +172,17 @@ export const importLinksFromRebrandly = async ({
({ title, slashtag: key, destination, tags, createdAt, updatedAt }) => {
// if tagsToId is provided and tags array is not empty, get the tagIds
const tagIds =
- tagsToId && tags.length > 0 ? tags.map((tag) => tagsToId[tag]) : [];
+ tagsToId && tags.length > 0
+ ? tags.map(
+ (tag: {
+ id: string;
+ name: string;
+ color: string;
+ active: boolean;
+ clicks: number;
+ }) => tagsToId[tag.name],
+ )
+ : [];
return {
projectId,
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 66e72a4585..74789d8736 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -520,15 +520,16 @@ export async function bulkCreateLinks({
if (links.length === 0) return [];
let createdLinks: LinkProps[] = [];
+ let linkTags: { tagId: string; linkId: string }[] = [];
if (skipPrismaCreate) {
createdLinks = links;
} else {
await prisma.link.createMany({
data: links.map(({ tagId, tagIds, ...link }) => {
- const combinedTagIds = combineTagIds({ tagId, tagIds });
const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
getParamsFromURL(link.url);
+
return {
...link,
title: truncate(link.title, 120),
@@ -540,13 +541,8 @@ export async function bulkCreateLinks({
utm_content,
expiresAt: link.expiresAt ? new Date(link.expiresAt) : null,
geo: link.geo || undefined,
- ...(combinedTagIds.length > 0 && {
- tags: {
- createMany: {
- data: combinedTagIds.map((tagId) => ({ tagId })),
- },
- },
- }),
+ // note: we're creating linkTags separately cause you can't do
+ // nested createMany in Prisma: https://github.com/prisma/prisma/issues/5455
};
}),
skipDuplicates: true,
@@ -554,8 +550,8 @@ export async function bulkCreateLinks({
createdLinks = (await Promise.all(
links.map(async (link) => {
- const { key, domain } = link;
- return await prisma.link.findUnique({
+ const { key, domain, tagId, tagIds } = link;
+ const data = await prisma.link.findUnique({
where: {
domain_key: {
domain,
@@ -563,8 +559,18 @@ export async function bulkCreateLinks({
},
},
});
+ if (!data) return null;
+ // combine tagIds for creation later
+ const combinedTagIds = combineTagIds({ tagId, tagIds });
+ linkTags.push(
+ ...combinedTagIds.map((tagId) => ({ tagId, linkId: data.id })),
+ );
+ return {
+ ...data,
+ tagIds: combinedTagIds,
+ };
}),
- )) as LinkProps[];
+ )) as LinkWithTagIdsProps[];
}
const pipeline = redis.pipeline();
@@ -592,7 +598,15 @@ export async function bulkCreateLinks({
pipeline.hset(domain, links);
});
- await pipeline.exec();
+ await Promise.all([
+ pipeline.exec(),
+ // create link tags
+ linkTags.length > 0 &&
+ prisma.linkTag.createMany({
+ data: linkTags,
+ skipDuplicates: true,
+ }),
+ ]);
return createdLinks.map((link) => {
const shortLink = linkConstructor({
diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsx
index 9e7cc7dc07..4166fe500b 100644
--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx
@@ -2,7 +2,6 @@
import useDomains from "@/lib/swr/use-domains";
import useProject from "@/lib/swr/use-project";
-import { LinkProps } from "@/lib/types";
import { AlertCircleFill, Lock, Random, X } from "@/ui/shared/icons";
import {
BlurImage,
@@ -90,6 +89,7 @@ function AddEditLinkModal({
domain: primaryDomain,
key: "",
url: "",
+ tags: [],
},
);