Skip to content

Commit

Permalink
feat: refactor planetscale to neon
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed Mar 10, 2024
1 parent 278ce97 commit 24dc662
Show file tree
Hide file tree
Showing 36 changed files with 676 additions and 1,640 deletions.
23 changes: 9 additions & 14 deletions app/(main)/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sql } from 'drizzle-orm'
import { count, isNotNull } from 'drizzle-orm'
import Link from 'next/link'
import React from 'react'

Expand All @@ -8,6 +8,7 @@ import { Container } from '~/components/ui/Container'
import { kvKeys } from '~/config/kv'
import { navigationItems } from '~/config/nav'
import { db } from '~/db'
import { subscribers } from '~/db/schema'
import { env } from '~/env.mjs'
import { prettifyNumber } from '~/lib/math'
import { redis } from '~/lib/redis'
Expand Down Expand Up @@ -98,26 +99,20 @@ async function LastVisitorInfo() {
}

export async function Footer() {
const {
rows: [count],
} = await db.execute(
sql`SELECT
(SELECT COUNT(*) FROM subscribers WHERE subscribed_at IS NOT NULL) as subscribers`
)
const [subs] = await db
.select({
subCount: count(),
})
.from(subscribers)
.where(isNotNull(subscribers.subscribedAt))

return (
<footer className="mt-32">
<Container.Outer>
<div className="border-t border-zinc-100 pb-16 pt-10 dark:border-zinc-700/40">
<Container.Inner>
<div className="mx-auto mb-8 max-w-md">
<Newsletter
subCount={
typeof count !== 'undefined' && 'subscribers' in count
? count.subscribers
: undefined
}
/>
<Newsletter subCount={`${subs?.subCount ?? '0'}`} />
</div>
<div className="flex flex-col items-center justify-between gap-6 sm:flex-row">
<p className="text-sm text-zinc-500/80 dark:text-zinc-400/80">
Expand Down
6 changes: 5 additions & 1 deletion app/admin/comments/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import { clientFetch } from '~/sanity/lib/client'
export default async function AdminCommentsPage() {
const {
rows: [commentsCount],
} = await db.execute<{ today_count: number }>(
} = await db.execute<{
today_count: number
this_week_count: number
this_month_count: number
}>(
sql`SELECT
(SELECT COUNT(*) FROM comments WHERE DATE(created_at) = CURDATE()) as today_count,
(SELECT COUNT(*) FROM comments WHERE YEARWEEK(created_at, 1) = YEARWEEK(CURDATE(), 1)) as this_week_count,
Expand Down
2 changes: 1 addition & 1 deletion app/admin/newsletters/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { newsletters } from '~/db/schema'
export default async function AdminNewslettersPage() {
const {
rows: [count],
} = await db.execute<{ today_count: number }>(
} = await db.execute<{ total: number }>(
sql`SELECT
(SELECT COUNT(*) FROM newsletters) as total`
)
Expand Down
6 changes: 5 additions & 1 deletion app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { db } from '~/db'
export default async function AdminPage() {
const {
rows: [count],
} = await db.execute<{ today_count: number }>(
} = await db.execute<{
comments: number
subscribers: number
guestbook: number
}>(
sql`SELECT
(SELECT COUNT(*) FROM comments) as comments,
(SELECT COUNT(*) FROM subscribers WHERE subscribed_at IS NOT NULL) as subscribers,
Expand Down
2 changes: 1 addition & 1 deletion app/admin/subscribers/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { subscribers } from '~/db/schema'
export default async function AdminSubscribersPage() {
const {
rows: [count],
} = await db.execute<{ today_count: number }>(
} = await db.execute<{ total: number }>(
sql`SELECT
(SELECT COUNT(*) FROM subscribers WHERE subscribed_at IS NOT NULL) as total`
)
Expand Down
11 changes: 8 additions & 3 deletions app/api/comments/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export async function POST(req: NextRequest, { params }: Params) {
lastName: user.lastName,
imageUrl: user.imageUrl,
},
parentId: parentId ? (parentId as bigint) : null,
parentId: parentId ? (parentId as number) : null,
}

if (parentId && env.NODE_ENV === 'production') {
Expand Down Expand Up @@ -160,11 +160,16 @@ export async function POST(req: NextRequest, { params }: Params) {
}
}

const { insertId } = await db.insert(comments).values(commentData)
const [newComment] = await db
.insert(comments)
.values(commentData)
.returning({
newId: comments.id,
})

return NextResponse.json({
...commentData,
id: CommentHashids.encode(insertId),
id: CommentHashids.encode(newComment!.newId),
createdAt: new Date(),
parentId: hashedParentId,
} satisfies CommentDto)
Expand Down
9 changes: 7 additions & 2 deletions app/api/guestbook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ export async function POST(req: NextRequest) {
})
}

const { insertId } = await db.insert(guestbook).values(guestbookData)
const [newGuestbook] = await db
.insert(guestbook)
.values(guestbookData)
.returning({
newId: guestbook.id,
})

return NextResponse.json(
{
...guestbookData,
id: GuestbookHashids.encode(insertId),
id: GuestbookHashids.encode(newGuestbook!.newId),
createdAt: new Date(),
} satisfies GuestbookDto,
{
Expand Down
13 changes: 4 additions & 9 deletions db/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { connect } from '@planetscale/database'
import { drizzle } from 'drizzle-orm/planetscale-serverless'
import { Pool } from '@neondatabase/serverless'
import { drizzle } from 'drizzle-orm/neon-serverless'

import { env } from '~/env.mjs'

// create the connection
const connection = connect({
host: env.DATABASE_HOST,
username: env.DATABASE_USERNAME,
password: env.DATABASE_PASSWORD,
})

export const db = drizzle(connection)
const pool = new Pool({ connectionString: env.DATABASE_URL })
export const db = drizzle(pool)
39 changes: 39 additions & 0 deletions db/migrations/0000_shallow_iron_fist.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CREATE TABLE IF NOT EXISTS "comments" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" varchar(200) NOT NULL,
"user_info" json,
"post_id" varchar(100) NOT NULL,
"parent_id" integer,
"body" json,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "guestbook" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" varchar(200) NOT NULL,
"user_info" json,
"message" text NOT NULL,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "newsletters" (
"id" serial PRIMARY KEY NOT NULL,
"subject" varchar(200),
"body" text,
"sent_at" timestamp,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "subscribers" (
"id" serial PRIMARY KEY NOT NULL,
"email" varchar(120),
"token" varchar(50),
"subscribed_at" timestamp,
"unsubscribed_at" timestamp,
"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "post_idx" ON "comments" ("post_id");
5 changes: 0 additions & 5 deletions db/migrations/0000_sweet_king_cobra.sql

This file was deleted.

2 changes: 0 additions & 2 deletions db/migrations/0001_worried_crystal.sql

This file was deleted.

10 changes: 0 additions & 10 deletions db/migrations/0002_square_jasper_sitwell.sql

This file was deleted.

1 change: 0 additions & 1 deletion db/migrations/0003_talented_jane_foster.sql

This file was deleted.

2 changes: 0 additions & 2 deletions db/migrations/0004_cold_snowbird.sql

This file was deleted.

3 changes: 0 additions & 3 deletions db/migrations/0005_dapper_red_ghost.sql

This file was deleted.

1 change: 0 additions & 1 deletion db/migrations/0006_messy_starjammers.sql

This file was deleted.

7 changes: 0 additions & 7 deletions db/migrations/0007_robust_sersi.sql

This file was deleted.

7 changes: 0 additions & 7 deletions db/migrations/0008_violet_the_enforcers.sql

This file was deleted.

Loading

0 comments on commit 24dc662

Please sign in to comment.