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 Short.io & Rebrandly importers

Steven Tey854f04f2342b
diff --git a/apps/web/app/api/cron/import/rebrandly/utils.ts b/apps/web/app/api/cron/import/rebrandly/utils.ts
index f1903dd49f..322a1630d4 100644
--- a/apps/web/app/api/cron/import/rebrandly/utils.ts
+++ b/apps/web/app/api/cron/import/rebrandly/utils.ts
@@ -199,8 +199,26 @@ export const importLinksFromRebrandly = async ({
)
.filter(Boolean);
+ // check if links are already in the database
+ const alreadyCreatedLinks = await prisma.link.findMany({
+ where: {
+ domain,
+ key: {
+ in: importedLinks.map((link) => link.key),
+ },
+ },
+ select: {
+ key: true,
+ },
+ });
+
+ // filter out links that are already in the database
+ const linksToCreate = importedLinks.filter(
+ (link) => !alreadyCreatedLinks.some((l) => l.key === link.key),
+ );
+
// bulk create links
- await bulkCreateLinks({ links: importedLinks });
+ await bulkCreateLinks({ links: linksToCreate });
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 4fd30b685b..b3f6b08c11 100644
--- a/apps/web/app/api/cron/import/short/utils.ts
+++ b/apps/web/app/api/cron/import/short/utils.ts
@@ -27,7 +27,7 @@ export const importLinksFromShort = async ({
shortApiKey: string;
}) => {
const data = await fetch(
- `https://api.short.io/api/links?domain_id=${domainId}&limit=150${
+ `https://api.short.io/api/links?domain_id=${domainId}&limit=50${
pageToken ? `&pageToken=${pageToken}` : ""
}`,
{
@@ -37,6 +37,7 @@ export const importLinksFromShort = async ({
},
},
).then((res) => res.json());
+
const { links, nextPageToken } = data;
let tagsToCreate = new Set<string>();
@@ -79,6 +80,24 @@ export const importLinksFromShort = async ({
)
.filter(Boolean);
+ // check if links are already in the database
+ const alreadyCreatedLinks = await prisma.link.findMany({
+ where: {
+ domain,
+ key: {
+ in: importedLinks.map((link) => link.key),
+ },
+ },
+ select: {
+ key: true,
+ },
+ });
+
+ // filter out links that are already in the database
+ const linksToCreate = importedLinks.filter(
+ (link) => !alreadyCreatedLinks.some((l) => l.key === link.key),
+ );
+
// import tags into database
if (importTags && tagsToCreate.size > 0) {
const existingTags = await prisma.tag.findMany({
@@ -114,7 +133,7 @@ export const importLinksFromShort = async ({
// bulk create links
await bulkCreateLinks({
- links: importedLinks.map(({ tags, ...rest }) => {
+ links: linksToCreate.map(({ tags, ...rest }) => {
return {
...rest,
...(importTags && {
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index 6397787c05..f90b004e27 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -12,9 +12,9 @@ export async function bulkCreateLinks({
}) {
if (links.length === 0) return [];
- // create links via $transaction (because Prisma doesn't support nested createMany)
+ // create links via Promise.all (because Prisma doesn't support nested createMany)
// ref: https://github.com/prisma/prisma/issues/8131#issuecomment-997667070
- const createdLinks = await prisma.$transaction(
+ const createdLinks = await Promise.all(
links.map(({ tagId, tagIds, tagNames, ...link }) => {
const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =
getParamsFromURL(link.url);
diff --git a/apps/web/scripts/bulk-delete-links.ts b/apps/web/scripts/bulk-delete-links.ts
index 909b5c4ddb..9ca615947e 100644
--- a/apps/web/scripts/bulk-delete-links.ts
+++ b/apps/web/scripts/bulk-delete-links.ts
@@ -3,13 +3,13 @@ import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import "dotenv-flow/config";
-const filters = {
- domain: "song.fyi",
-};
+const domain = "song.fyi";
async function main() {
const links = await prisma.link.findMany({
- where: filters,
+ where: {
+ domain,
+ },
select: {
id: true,
domain: true,
@@ -20,11 +20,11 @@ async function main() {
},
});
const response = await Promise.all([
- // prisma.link.deleteMany({
- // where: filters,
- // }),
+ prisma.link.deleteMany({
+ where: { domain },
+ }),
+ redis.del(domain),
...links.flatMap((link) => [
- redis.hdel(link.domain.toLowerCase(), link.key.toLowerCase()),
recordLink({
link,
deleted: true,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 44a2781278..4e79fb6420 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -51,13 +51,13 @@ importers:
version: link:../../packages/utils
'@next-auth/prisma-adapter':
specifier: ^1.0.5
- version: 1.0.5(@prisma/[email protected])([email protected])
+ version: 1.0.5(@prisma/[email protected])([email protected])
'@planetscale/database':
specifier: ^1.16.0
version: 1.16.0
'@prisma/client':
specifier: ^5.9.1
- version: 5.10.2([email protected])
+ version: 5.13.0([email protected])
'@react-email/components':
specifier: ^0.0.14
version: 0.0.14(@types/[email protected])([email protected])
@@ -210,7 +210,7 @@ importers:
version: 4.0.2
prisma:
specifier: ^5.9.1
- version: 5.10.2
+ version: 5.13.0
react:
specifier: ^18.2.0
version: 18.2.0
@@ -4125,13 +4125,13 @@ packages:
sparse-bitfield: 3.0.3
dev: false
resolution: {integrity: sha512-VqMS11IxPXrPGXw6Oul6jcyS/n8GLOWzRMrPr3EMdtD6eOalM6zz05j08PcNiis8QzkfuYnCv49OvufTuaEwYQ==}
peerDependencies:
'@prisma/client': '>=2.26.0 || >=3'
next-auth: ^4
dependencies:
- '@prisma/client': 5.10.2([email protected])
+ '@prisma/client': 5.13.0([email protected])
dev: false
@@ -4475,8 +4475,8 @@ packages:
engines: {node: '>=16'}
dev: false
- resolution: {integrity: sha512-ef49hzB2yJZCvM5gFHMxSFL9KYrIP9udpT5rYo0CsHD4P9IKj473MbhU1gjKKftiwWBTIyrt9jukprzZXazyag==}
+ resolution: {integrity: sha512-uYdfpPncbZ/syJyiYBwGZS8Gt1PTNoErNYMuqHDa2r30rNSFtgTA/LXsSk55R7pdRTMi5pHkeP9B14K6nHmwkg==}
engines: {node: '>=16.13'}
requiresBuild: true
peerDependencies:
@@ -4485,39 +4485,39 @@ packages:
prisma:
optional: true
dependencies:
- prisma: 5.10.2
+ prisma: 5.13.0
dev: false
- /@prisma/[email protected]:
- resolution: {integrity: sha512-bkBOmH9dpEBbMKFJj8V+Zp8IZHIBjy3fSyhLhxj4FmKGb/UBSt9doyfA6k1UeUREsMJft7xgPYBbHSOYBr8XCA==}
+ /@prisma/[email protected]:
+ resolution: {integrity: sha512-699iqlEvzyCj9ETrXhs8o8wQc/eVW+FigSsHpiskSFydhjVuwTJEfj/nIYqTaWFYuxiWQRfm3r01meuW97SZaQ==}
dev: false
- /@prisma/engines-version@5.10.0-34.5a9203d0590c951969e85a7d07215503f4672eb9:
- resolution: {integrity: sha512-uCy/++3Jx/O3ufM+qv2H1L4tOemTNqcP/gyEVOlZqTpBvYJUe0tWtW0y3o2Ueq04mll4aM5X3f6ugQftOSLdFQ==}
+ /@prisma/engines-version@5.13.0-23.b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b:
+ resolution: {integrity: sha512-AyUuhahTINGn8auyqYdmxsN+qn0mw3eg+uhkp8zwknXYIqoT3bChG4RqNY/nfDkPvzWAPBa9mrDyBeOnWSgO6A==}
dev: false
- /@prisma/[email protected]:
- resolution: {integrity: sha512-HkSJvix6PW8YqEEt3zHfCYYJY69CXsNdhU+wna+4Y7EZ+AwzeupMnUThmvaDA7uqswiHkgm5/SZ6/4CStjaGmw==}
+ /@prisma/[email protected]:
+ resolution: {integrity: sha512-hIFLm4H1boj6CBZx55P4xKby9jgDTeDG0Jj3iXtwaaHmlD5JmiDkZhh8+DYWkTGchu+rRF36AVROLnk0oaqhHw==}
requiresBuild: true
dependencies:
- '@prisma/debug': 5.10.2
- '@prisma/engines-version': 5.10.0-34.5a9203d0590c951969e85a7d07215503f4672eb9
- '@prisma/fetch-engine': 5.10.2
- '@prisma/get-platform': 5.10.2
+ '@prisma/debug': 5.13.0
+ '@prisma/engines-version': 5.13.0-23.b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
+ '@prisma/fetch-engine': 5.13.0
+ '@prisma/get-platform': 5.13.0
dev: false
- /@prisma/[email protected]:
- resolution: {integrity: sha512-dSmXcqSt6DpTmMaLQ9K8ZKzVAMH3qwGCmYEZr/uVnzVhxRJ1EbT/w2MMwIdBNq1zT69Rvh0h75WMIi0mrIw7Hg==}
+ /@prisma/[email protected]:
+ resolution: {integrity: sha512-Yh4W+t6YKyqgcSEB3odBXt7QyVSm0OQlBSldQF2SNXtmOgMX8D7PF/fvH6E6qBCpjB/yeJLy/FfwfFijoHI6sA==}
dependencies:
- '@prisma/debug': 5.10.2
- '@prisma/engines-version': 5.10.0-34.5a9203d0590c951969e85a7d07215503f4672eb9
- '@prisma/get-platform': 5.10.2
+ '@prisma/debug': 5.13.0
+ '@prisma/engines-version': 5.13.0-23.b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
+ '@prisma/get-platform': 5.13.0
dev: false
- /@prisma/[email protected]:
- resolution: {integrity: sha512-nqXP6vHiY2PIsebBAuDeWiUYg8h8mfjBckHh6Jezuwej0QJNnjDiOq30uesmg+JXxGk99nqyG3B7wpcOODzXvg==}
+ /@prisma/[email protected]:
+ resolution: {integrity: sha512-B/WrQwYTzwr7qCLifQzYOmQhZcFmIFhR81xC45gweInSUn2hTEbfKUPd2keAog+y5WI5xLAFNJ3wkXplvSVkSw==}
dependencies:
- '@prisma/debug': 5.10.2
+ '@prisma/debug': 5.13.0
dev: false
/@protobufjs/[email protected]:
@@ -13954,13 +13954,13 @@ packages:
react: 18.2.0
dev: false
- resolution: {integrity: sha512-hqb/JMz9/kymRE25pMWCxkdyhbnIWrq+h7S6WysJpdnCvhstbJSNP/S6mScEcqiB8Qv2F+0R3yG+osRaWqZacQ==}
+ resolution: {integrity: sha512-kGtcJaElNRAdAGsCNykFSZ7dBKpL14Cbs+VaQ8cECxQlRPDjBlMHNFYeYt0SKovAVy2Y65JXQwB3A5+zIQwnTg==}
engines: {node: '>=16.13'}
hasBin: true
requiresBuild: true
dependencies:
- '@prisma/engines': 5.10.2
+ '@prisma/engines': 5.13.0
dev: false