Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Database refactoring #1138

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ dist/
.next/
/out/

# Prisma
prisma/

# production
/build

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Dub.co is the open-source link management infrastructure for modern marketing te
- [Tailwind](https://tailwindcss.com/) – CSS
- [Upstash](https://upstash.com/) – redis
- [Tinybird](https://tinybird.com/) – analytics
- [PlanetScale](https://planetscale.com/) – database
- [PlanetScale](https://planetscale.com/) or [Neon](https://neon.tech/home) – database
- [NextAuth.js](https://next-auth.js.org/) – auth
- [BoxyHQ](https://boxyhq.com/enterprise-sso) – SSO/SAML
- [Turborepo](https://turbo.build/repo) – monorepo
Expand Down
3 changes: 2 additions & 1 deletion apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ QSTASH_TOKEN=
QSTASH_CURRENT_SIGNING_KEY=
QSTASH_NEXT_SIGNING_KEY=

# MySQL Database via Planetscale
# PostgreSQL or MySQL Database
# Get your MySQL Database URL here: https://planetscale.com/docs/tutorials/connect-nodejs-app
# Or PostgreSQL here: https://neon.tech/home
DATABASE_URL="mysql://root:@localhost:3306/planetscale"
# Set local development documentation for connecting to a local database: https://dub.co/docs/local-development#step-4-set-up-planetscale-mysql-database
PLANETSCALE_DATABASE_URL="http://root:unused@localhost:3900"
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/[domain]/stats/[key]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import Analytics from "@/ui/analytics";
import { constructMetadata } from "@dub/utils";
import { notFound } from "next/navigation";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/analytics/edge/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
exceededLimitError,
handleAndReturnErrorResponse,
} from "@/lib/api/errors";
import { getLinkViaEdge, getWorkspaceViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge, getWorkspaceViaEdge } from "@/lib/db/edge";
import { ratelimit } from "@/lib/upstash";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { DUB_DEMO_LINKS, DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/analytics/public-stats/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { withWorkspace } from "@/lib/auth";
import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import { prisma } from "@/lib/prisma";
import z from "@/lib/zod";
import { domainKeySchema } from "@/lib/zod/schemas/links";
Expand Down
16 changes: 12 additions & 4 deletions apps/web/app/api/auth/account-exists/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isWhitelistedEmail } from "@/lib/edge-config";
import { DATABASE_URL, conn } from "@/lib/planetscale";
import { DATABASE_URL, getEdgeClient } from "@/lib/db";
import { ratelimit } from "@/lib/upstash";
import { ipAddress } from "@vercel/edge";
import { NextRequest, NextResponse } from "next/server";
Expand All @@ -25,9 +25,17 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ accountExists: true, hasPassword: true });
}

const user = await conn
.execute("SELECT email, passwordHash FROM User WHERE email = ?", [email])
.then((res) => res.rows[0]);
const client = getEdgeClient();

const user = await client.user.findUnique({
where: {
email: email
},
select: {
email: true,
passwordHash: true,
}
});

if (user) {
return NextResponse.json({
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/links/random/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { ratelimitOrThrow } from "@/lib/api/utils";
import { getRandomKey } from "@/lib/planetscale";
import { getRandomKey } from "@/lib/db/edge";
import { domainKeySchema } from "@/lib/zod/schemas/links";
import { getSearchParams } from "@dub/utils";
import { NextRequest, NextResponse } from "next/server";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/links/verify/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { keyChecks, processKey } from "@/lib/api/links/utils";
import { getWorkspaceViaEdge } from "@/lib/planetscale";
import { getWorkspaceViaEdge } from "@/lib/db/edge";
import { domainKeySchema } from "@/lib/zod/schemas/links";
import { workspaceIdSchema } from "@/lib/zod/schemas/workspaces";
import { getSearchParams } from "@dub/utils";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/og/analytics/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import {
GOOGLE_FAVICON_URL,
getApexDomain,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/workspaces/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DubApiError } from "@/lib/api/errors";
import { withSession } from "@/lib/auth";
import { checkIfUserExists } from "@/lib/planetscale";
import { checkIfUserExists } from "@/lib/db/edge";
import { prisma } from "@/lib/prisma";
import {
WorkspaceSchema,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/expired/[domain]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import { Background, Footer, Nav, NavMobile } from "@dub/ui";
import { constructMetadata } from "@dub/utils";
import { TimerOff } from "lucide-react";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/inspect/[domain]/[key]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import {
Background,
Footer,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/password/[domain]/[key]/action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use server";

import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import { linkConstructor } from "@dub/utils";

export async function verifyPassword(_prevState: any, data: FormData) {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/proxy/[domain]/[key]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLinkViaEdge } from "@/lib/planetscale";
import { getLinkViaEdge } from "@/lib/db/edge";
import { BlurImage } from "@dub/ui";
import {
GOOGLE_FAVICON_URL,
Expand Down Expand Up @@ -61,7 +61,7 @@ export default async function ProxyPage({
<main className="flex h-screen w-screen items-center justify-center">
<div className="mx-5 w-full max-w-lg overflow-hidden rounded-lg border border-gray-200 sm:mx-0">
<img
src={data.image}
src={data.image || ""}
alt={unescape(data.title || "")}
className="w-full object-cover"
/>
Expand Down
7 changes: 7 additions & 0 deletions apps/web/db/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Database

## Add Migration
pnpm dlx prisma migrate dev --name <Message> --schema <Schema>

Example:
`pnpm dlx prisma migrate dev --name init --schema .\db\postgresql\schema\`
File renamed without changes.
Loading