The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
replace prismaEdge
Kiran Kd48f90faae28
diff --git a/apps/web/app/api/analytics/edge/route.ts b/apps/web/app/api/analytics/edge/route.tsindex 6b72e9f424..9f51efd749 100644--- a/apps/web/app/api/analytics/edge/route.ts+++ b/apps/web/app/api/analytics/edge/route.ts@@ -5,16 +5,22 @@ import {exceededLimitError,handleAndReturnErrorResponse,} from "@/lib/api/errors";-import { prismaEdge } from "@/lib/prisma/edge";+import { conn } from "@/lib/planetscale/connection";import { PlanProps } from "@/lib/types";import { ratelimit } from "@/lib/upstash";import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";import { DUB_DEMO_LINKS, DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";+import { Link, Project } from "@prisma/client";import { ipAddress } from "@vercel/functions";import { NextResponse, type NextRequest } from "next/server";export const runtime = "edge";+type LinkWithWorkspace = Pick<Link, "id" | "projectId"> &+ Pick<Project, "plan" | "usage" | "usageLimit"> & {+ sharedDashboardId: string;+ };+export const GET = async (req: NextRequest) => {try {const searchParams = getSearchParams(req.url);@@ -29,7 +35,7 @@ export const GET = async (req: NextRequest) => {});}- let link;+ let link: Pick<Link, "id" | "projectId"> | null = null;const demoLink = DUB_DEMO_LINKS.find((l) => l.domain === domain && l.key === key,@@ -52,57 +58,70 @@ export const GET = async (req: NextRequest) => {});}}+link = {id: demoLink.id,projectId: DUB_WORKSPACE_ID,};- } else {- link = await prismaEdge.link.findUnique({- where: {- domain_key: { domain, key },- },- select: {- id: true,- sharedDashboard: true,- projectId: true,- project: {- select: {- plan: true,- usage: true,- usageLimit: true,- },- },- },- });+ }++ // Find the link by domain and key+ else {+ // TODO:+ // Move this to '/lib/planetscale'+ const { rows } = await conn.execute<LinkWithWorkspace>(+ `SELECT Link.id, Link.projectId, Project.plan, Project.usage, Project.usageLimit, SharedDashboard.id AS sharedDashboardId+ FROM Link+ LEFT JOIN Project ON Link.projectId = Project.id+ LEFT JOIN SharedDashboard ON Link.id = SharedDashboard.linkId+ WHERE Link.domain = ? AND Link.key = ?+ LIMIT 1`,+ [domain, key],+ );++ if (!rows || rows.length === 0) {+ throw new DubApiError({+ code: "not_found",+ message: "Link not found",+ });+ }++ const linkFound = rows[0];// if the link is explicitly private (publicStats === false)- if (!link?.sharedDashboard) {+ if (!linkFound.sharedDashboardId) {throw new DubApiError({code: "forbidden",message: "This link does not have a public analytics dashboard",});}- const workspace = link.project;+ const { plan, usage, usageLimit, projectId } = linkFound;validDateRangeForPlan({- plan: workspace?.plan || "free",+ plan: plan || "free",interval,start,end,throwError: true,});- if (workspace && workspace.usage > workspace.usageLimit) {+ // Check if the project has exceeded its usage limit if projectId is defined+ if (projectId && usage > usageLimit) {throw new DubApiError({code: "forbidden",message: exceededLimitError({- plan: workspace.plan as PlanProps,- limit: workspace.usageLimit,+ plan: plan as PlanProps,+ limit: usageLimit,type: "clicks",}),});}++ link = {+ id: linkFound.id,+ projectId: linkFound.projectId,+ };}const response = await getAnalytics({