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.

use bulkCreateLinks in importers

Steven Teyce0154d8b368
diff --git a/apps/web/app/api/cron/import/bitly/utils.ts b/apps/web/app/api/cron/import/bitly/utils.ts
index b1e74816d1..9df5440f96 100644
--- a/apps/web/app/api/cron/import/bitly/utils.ts
+++ b/apps/web/app/api/cron/import/bitly/utils.ts
@@ -1,3 +1,4 @@
+import { bulkCreateLinks } from "@/lib/api/links";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
@@ -39,8 +40,6 @@ export const importLinksFromBitly = async ({
const { links, pagination } = data;
const nextSearchAfter = pagination.search_after;
- const pipeline = redis.pipeline();
-
// convert links to format that can be imported into database
const importedLinks = links.flatMap(
({
@@ -57,13 +56,6 @@ export const importLinksFromBitly = async ({
if (!domains.includes(domain)) {
return [];
}
- pipeline.set(
- `${domain}:${key}`,
- {
- url: encodeURIComponent(url),
- },
- { nx: true },
- );
const createdAt = new Date(created_at).toISOString();
const tagId = tagsToId ? tagsToId[tags[0]] : null;
const linkDetails = {
@@ -94,13 +86,6 @@ export const importLinksFromBitly = async ({
// original bitlink, but it should
const customDomain = new URL(customBitlink).hostname;
const customKey = new URL(customBitlink).pathname.slice(1);
- pipeline.set(
- `${customDomain}:${customKey}`,
- {
- url: encodeURIComponent(url),
- },
- { nx: true },
- );
return {
...linkDetails,
domain: customDomain,
@@ -111,14 +96,8 @@ export const importLinksFromBitly = async ({
},
);
- // import links into database
- await Promise.all([
- prisma.link.createMany({
- data: importedLinks,
- skipDuplicates: true,
- }),
- pipeline.exec(),
- ]);
+ // bulk create links
+ await bulkCreateLinks(importedLinks);
count += importedLinks.length;
diff --git a/apps/web/app/api/cron/import/rebrandly/utils.ts b/apps/web/app/api/cron/import/rebrandly/utils.ts
index 4a32835a68..adeb36631b 100644
--- a/apps/web/app/api/cron/import/rebrandly/utils.ts
+++ b/apps/web/app/api/cron/import/rebrandly/utils.ts
@@ -1,3 +1,4 @@
+import { bulkCreateLinks } from "@/lib/api/links";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
@@ -165,47 +166,31 @@ export const importLinksFromRebrandly = async ({
} else {
const newLastLinkId = links[links.length - 1].id;
- const pipeline = redis.pipeline();
-
// convert links to format that can be imported into database
const importedLinks = links
- .map(({ title, slashtag, destination, tags, createdAt, updatedAt }) => {
- pipeline.set(
- `${domain}:${slashtag}`,
- {
- url: encodeURIComponent(destination),
- },
- {
- nx: true,
- },
- );
-
- // if tagsToId is provided and tags array is not empty, get the tagId
- const tagId =
- tagsToId && tags.length > 0 ? tagsToId[tags[0].name] : null;
-
- return {
- projectId,
- userId,
- domain,
- key: slashtag,
- url: destination,
- title,
- createdAt,
- updatedAt,
- ...(tagId ? { tagId } : {}),
- };
- })
+ .map(
+ ({ title, slashtag: key, destination, tags, createdAt, updatedAt }) => {
+ // if tagsToId is provided and tags array is not empty, get the tagId
+ const tagId =
+ tagsToId && tags.length > 0 ? tagsToId[tags[0].name] : null;
+
+ return {
+ projectId,
+ userId,
+ domain,
+ key,
+ url: destination,
+ title,
+ createdAt,
+ updatedAt,
+ ...(tagId ? { tagId } : {}),
+ };
+ },
+ )
.filter(Boolean);
- // import links into database
- await Promise.all([
- prisma.link.createMany({
- data: importedLinks,
- skipDuplicates: true,
- }),
- pipeline.exec(),
- ]);
+ // bulk create links
+ await bulkCreateLinks(importedLinks);
count += importedLinks.length;
diff --git a/apps/web/app/api/cron/import/short/utils.ts b/apps/web/app/api/cron/import/short/utils.ts
index 638304face..b6d2d8b16a 100644
--- a/apps/web/app/api/cron/import/short/utils.ts
+++ b/apps/web/app/api/cron/import/short/utils.ts
@@ -1,3 +1,4 @@
+import { bulkCreateLinks } from "@/lib/api/links";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
@@ -35,8 +36,6 @@ export const importLinksFromShort = async ({
).then((res) => res.json());
const { links, nextPageToken } = data;
- const pipeline = redis.pipeline();
-
// convert links to format that can be imported into database
const importedLinks = links
.map(
@@ -53,17 +52,6 @@ export const importLinksFromShort = async ({
if (path.length === 0) {
return null;
}
- pipeline.set(
- `${domain}:${path}`,
- {
- url: encodeURIComponent(originalURL),
- ...(iphoneURL && { ios: encodeURIComponent(iphoneURL) }),
- ...(androidURL && { android: encodeURIComponent(androidURL) }),
- },
- {
- nx: true,
- },
- );
return {
projectId,
userId,
@@ -80,14 +68,8 @@ export const importLinksFromShort = async ({
)
.filter(Boolean);
- // import links into database
- await Promise.all([
- prisma.link.createMany({
- data: importedLinks,
- skipDuplicates: true,
- }),
- pipeline.exec(),
- ]);
+ // bulk create links
+ await bulkCreateLinks(importedLinks);
count += importedLinks.length;
diff --git a/apps/web/scripts/bulk-delete-links.ts b/apps/web/scripts/bulk-delete-links.ts
index c1ec4bc296..015957c7eb 100644
--- a/apps/web/scripts/bulk-delete-links.ts
+++ b/apps/web/scripts/bulk-delete-links.ts
@@ -5,24 +5,13 @@ import { redis } from "./utils";
const domain = "song.fyi";
async function main() {
- const links = await prisma.link.findMany({
- where: {
- domain,
- },
- });
-
- const pipeline = redis.pipeline();
- links.forEach(({ domain, key }) => {
- pipeline.del(`${domain}:${key}`);
- });
-
const response = await Promise.all([
- pipeline.exec(),
prisma.link.deleteMany({
where: {
domain,
},
}),
+ redis.del(domain),
]);
console.log(JSON.stringify(response, null, 2));