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 planetscale logs

Steven Teyda82f228e753
diff --git a/apps/web/app/api/auth/account-exists/route.ts b/apps/web/app/api/auth/account-exists/route.ts
index 9dee4df11f..6bdefa12c7 100644
--- a/apps/web/app/api/auth/account-exists/route.ts
+++ b/apps/web/app/api/auth/account-exists/route.ts
@@ -16,7 +16,7 @@ export async function POST(req: NextRequest) {
const { email } = (await req.json()) as { email: string };
- if (!conn) {
+ if (!process.env.DATABASE_URL) {
return new Response("Database connection not established", {
status: 500,
});
diff --git a/apps/web/app/app.dub.co/(auth)/login/action.ts b/apps/web/app/app.dub.co/(auth)/login/action.ts
index 4fe45d6d59..ff627fa1c4 100644
--- a/apps/web/app/app.dub.co/(auth)/login/action.ts
+++ b/apps/web/app/app.dub.co/(auth)/login/action.ts
@@ -15,7 +15,7 @@ export async function accountExists(data: FormData) {
error: "Missing email",
};
}
- if (!conn) {
+ if (!process.env.DATABASE_URL) {
return {
error: "Database connection not established",
};
diff --git a/apps/web/lib/middleware/admin.ts b/apps/web/lib/middleware/admin.ts
index ffa8187f04..5deac76ba8 100644
--- a/apps/web/lib/middleware/admin.ts
+++ b/apps/web/lib/middleware/admin.ts
@@ -19,7 +19,7 @@ export default async function AdminMiddleware(req: NextRequest) {
};
const response = await conn
- ?.execute("SELECT projectId FROM ProjectUsers WHERE userId = ?", [
+ .execute("SELECT projectId FROM ProjectUsers WHERE userId = ?", [
session?.user?.id,
])
.then((res) => res.rows[0] as { projectId: string } | undefined);
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index 6a293d4219..15ee78eeb2 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -1,30 +1,28 @@
import { connect } from "@planetscale/database";
export const pscale_config = {
- url: process.env.DATABASE_URL || "mysql://user:pass@host",
+ url: process.env.DATABASE_URL,
};
-export const conn = process.env.DATABASE_URL ? connect(pscale_config) : null;
+export const conn = connect(pscale_config);
// custom cached connection that sets next.revalidate to 900s
-export const cachedConn = process.env.DATABASE_URL
- ? connect({
- ...pscale_config,
- fetch: (url, init) => {
- // set next.revalidate
- return fetch(url, {
- ...init,
- cache: undefined,
- next: {
- revalidate: 900,
- },
- });
+export const cachedConn = connect({
+ ...pscale_config,
+ fetch: (url, init) => {
+ // set next.revalidate
+ return fetch(url, {
+ ...init,
+ cache: undefined,
+ next: {
+ revalidate: 900,
},
- })
- : null;
+ });
+ },
+});
export const getLinkViaEdge = async (domain: string, key: string) => {
- if (!conn) return null;
+ if (!process.env.DATABASE_URL) return null;
const { rows } =
(await conn.execute(
diff --git a/apps/web/lib/stats.ts b/apps/web/lib/stats.ts
index 9be8e21f95..963e15b1b8 100644
--- a/apps/web/lib/stats.ts
+++ b/apps/web/lib/stats.ts
@@ -99,7 +99,7 @@ export const getStats = async ({
// Note: we're using decodeURIComponent in this function because that's how we store it in MySQL and Tinybird
if (
- !conn ||
+ !process.env.DATABASE_URL ||
!process.env.TINYBIRD_API_KEY ||
!VALID_TINYBIRD_ENDPOINTS.includes(endpoint)
) {
@@ -109,8 +109,7 @@ export const getStats = async ({
// get all-time clicks count if:
// 1. endpoint is /clicks
// 2. interval is not defined
- // 3. there's a connection to MySQL
- if (endpoint === "clicks" && key && !interval && conn) {
+ if (endpoint === "clicks" && key && !interval) {
const response =
key === "_root"
? await conn.execute("SELECT clicks FROM Domain WHERE slug = ?", [
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird.ts
index 9af709c30c..ae40e11583 100644
--- a/apps/web/lib/tinybird.ts
+++ b/apps/web/lib/tinybird.ts
@@ -33,7 +33,9 @@ export async function recordClick({
const ip = ipAddress(req) || LOCALHOST_IP;
// deduplicate clicks from the same IP, domain and key – only record 1 click per hour
const { success } = await ratelimit(2, "1 h").limit(
- `recordClick:${ip}:${domain}:${key || "_root"}`,
+ `recordClick:${ip}:${domain.toLowerCase()}:${
+ key?.toLowerCase() || "_root"
+ }`,
);
if (!success) {
return null;
@@ -75,27 +77,24 @@ export async function recordClick({
}),
},
).then((res) => res.json()),
+
// increment the click count for the link if key is specified (not root click)
// also increment the usage count for the project, and then we have a cron that will reset it at the start of new billing cycle
- ...(conn
+ key
? [
- key
- ? [
- conn.execute(
- "UPDATE Link SET clicks = clicks + 1, lastClicked = NOW() WHERE domain = ? AND `key` = ?",
- [domain, key],
- ),
- conn.execute(
- "UPDATE Project p JOIN Domain d ON p.id = d.projectId SET p.usage = p.usage + 1 WHERE d.slug = ?",
- [domain],
- ),
- ]
- : conn.execute(
- "UPDATE Domain SET clicks = clicks + 1, lastClicked = NOW() WHERE slug = ?",
- [domain],
- ),
+ conn.execute(
+ "UPDATE Link SET clicks = clicks + 1, lastClicked = NOW() WHERE domain = ? AND `key` = ?",
+ [domain, key],
+ ),
+ conn.execute(
+ "UPDATE Project p JOIN Domain d ON p.id = d.projectId SET p.usage = p.usage + 1 WHERE d.slug = ?",
+ [domain],
+ ),
]
- : []),
+ : conn.execute(
+ "UPDATE Domain SET clicks = clicks + 1, lastClicked = NOW() WHERE slug = ?",
+ [domain],
+ ),
]);
}