Skip to content

Commit

Permalink
Merge pull request #862 from tszhong0411/use-postgresql
Browse files Browse the repository at this point in the history
Use PostgreSQL
  • Loading branch information
tszhong0411 committed Sep 16, 2024
2 parents dc83066 + 5e870f7 commit 030f849
Show file tree
Hide file tree
Showing 28 changed files with 748 additions and 609 deletions.
7 changes: 6 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
"**/.*.{js,cjs,mjs,jsx,ts,cts,mts,tsx,md,mdx,json,yml,yaml,svg,webmanifest}",
".*/**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx,md,mdx,json,yml,yaml,svg,webmanifest}"
],
"ignorePaths": ["pnpm-lock.yaml", "CHANGELOG.md", ".pnpm-store/"],
"ignorePaths": [
"pnpm-lock.yaml",
"CHANGELOG.md",
".pnpm-store/",
"packages/db/src/migrations/**"
],
"import": [
"@cspell/dict-software-terms",
"@cspell/dict-npm",
Expand Down
3 changes: 0 additions & 3 deletions .cspell/libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ fumadocs
hiett
jiti
knip
libsql
lightningcss
mdast
nanostores
Expand All @@ -36,8 +35,6 @@ sonner
sqld
tinycolor2
tsup
turso
tursodatabase
unifiedjs
usehooks
vfile
Expand Down
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ GITHUB_CLIENT_SECRET=
# Database URL
# For views, likes, guestbook, comments, authentication.
# ---------------------------------------------------------------------------------------------------------
DATABASE_URL="http://127.0.0.1:8080"
DATABASE_AUTH_TOKEN="no-need-for-local-development"
DATABASE_URL="postgres://postgres:postgres@localhost:5432/honghongme"

# ---------------------------------------------------------------------------------------------------------
# Redis
Expand Down
3 changes: 1 addition & 2 deletions .github/actions/check-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ runs:
- name: Create environment variables file
shell: bash
run: |
echo "DATABASE_URL=http://127.0.0.1:8080" >> .env.local
echo "DATABASE_AUTH_TOKEN=no-need-for-local-development" >> .env.local
echo "DATABASE_URL=postgres://postgres:postgres@localhost:5432/honghongme" >> .env.local
echo "RESEND_API_KEY=re_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" >> .env.local
echo "UPSTASH_REDIS_REST_URL=http://127.0.0.1:8079" >> .env.local
echo "UPSTASH_REDIS_REST_TOKEN=honghongme" >> .env.local
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cp .env.example .env.local
pnpm install
```

5. Run libSQL using Docker (or your preferred method):
5. Run PostgreSQL using Docker (or your preferred method):

```bash
docker compose up
Expand Down
2 changes: 0 additions & 2 deletions apps/web/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export { GET, POST } from '@/lib/auth'

export const runtime = 'edge'
7 changes: 0 additions & 7 deletions apps/web/src/app/api/trpc/[trpc]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import type { NextRequest } from 'next/server'
import { appRouter } from '@/trpc/root'
import { createTRPCContext } from '@/trpc/trpc'

/*
* React email renderer does not work with React 19 + Next.js 15
* Use Node.js runtime temporarily
* @see https://github.com/resend/react-email/issues/1630
*/
// export const runtime = 'edge'

const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers
Expand Down
2 changes: 0 additions & 2 deletions apps/web/src/app/og/[id]/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ type OGRouteProps = {
}
}

export const runtime = 'edge'

export const GET = async (_: Request, props: OGRouteProps) => {
try {
const {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/content/pages/about.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ I love using [Next.js](https://nextjs.org/) to create a website. Then use [GitHu
## About this site

- Framework: [Next.js](https://nextjs.org/)
- Database: [Turso](https://turso.tech/)
- Database: [PostgreSQL](https://www.postgresql.org/)
- ORM: [Drizzle](https://orm.drizzle.team/)
- Authentication: [NextAuth.js](https://next-auth.js.org/)
- Deployment: [Vercel](https://vercel.com)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/content/projects/blog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://honghong.me
github: https://github.com/tszhong0411/honghong.me
techstack:
- label: Typescript
- label: Turso
- label: PostgreSQL
- label: Next.js
- label: Drizzle
- label: MDX
Expand Down
14 changes: 8 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ version: '3'

services:
db:
image: ghcr.io/tursodatabase/libsql-server:latest
container_name: honghongme-db
platform: linux/amd64
image: postgres:16-alpine
container_name: honghongme-postgres
ports:
- '8080:8080'
- 5432:5432
volumes:
- ./volumes/libsql:/var/lib/sqld

- ./volumes/postgresql:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=honghongme
redis:
image: redis
container_name: honghongme-redis
Expand Down
6 changes: 2 additions & 4 deletions packages/db/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { env } from '@tszhong0411/env'
import { type Config } from 'drizzle-kit'

export default {
dialect: 'sqlite',
driver: 'turso',
dialect: 'postgresql',
schema: './src/schema/index.ts',
dbCredentials: {
url: env.DATABASE_URL,
authToken: env.DATABASE_AUTH_TOKEN
url: env.DATABASE_URL
},
out: './src/migrations',
strict: true,
Expand Down
5 changes: 3 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
"type-check": "tsc --noEmit"
},
"dependencies": {
"@libsql/client": "^0.9.0",
"@paralleldrive/cuid2": "^2.2.2",
"@tszhong0411/env": "workspace:*",
"drizzle-orm": "^0.33.0"
"drizzle-orm": "^0.33.0",
"pg": "^8.12.0"
},
"devDependencies": {
"@tszhong0411/eslint-config": "workspace:*",
"@tszhong0411/tsconfig": "workspace:*",
"@types/node": "^22.5.0",
"@types/pg": "^8.11.10",
"drizzle-kit": "^0.24.1"
},
"lint-staged": {
Expand Down
11 changes: 6 additions & 5 deletions packages/db/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createClient } from '@libsql/client/web'
import { env } from '@tszhong0411/env'
import { drizzle } from 'drizzle-orm/libsql'
import { drizzle } from 'drizzle-orm/node-postgres'
import pg from 'pg'

import * as schema from './schema'

const client = createClient({
url: env.DATABASE_URL,
authToken: env.DATABASE_AUTH_TOKEN
const client = new pg.Client({
connectionString: env.DATABASE_URL
})

client.connect()

export const db = drizzle(client, { schema })
25 changes: 16 additions & 9 deletions packages/db/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { createClient } from '@libsql/client/web'
import { env } from '@tszhong0411/env'
import { drizzle } from 'drizzle-orm/libsql'
import { migrate } from 'drizzle-orm/libsql/migrator'
import { drizzle } from 'drizzle-orm/node-postgres'
import { migrate } from 'drizzle-orm/node-postgres/migrator'
import path from 'node:path'
import pg from 'pg'

import * as schema from './schema'

const client = new pg.Client({
connectionString: env.DATABASE_URL
})

client.connect()

const main = async () => {
try {
const db = drizzle(
createClient({
url: env.DATABASE_URL,
authToken: env.DATABASE_AUTH_TOKEN
})
)
const db = drizzle(client, {
schema
})

await migrate(db, {
migrationsFolder: path.join(process.cwd(), 'src/migrations')
})
console.log('🎉 Database migration successfully!')
} catch (error) {
console.error('❌ Database migration failed:\n', error)
} finally {
client.end()
}
}

Expand Down
126 changes: 126 additions & 0 deletions packages/db/src/migrations/0000_goofy_kree.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
DO $$ BEGIN
CREATE TYPE "public"."role" AS ENUM('user', 'admin');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "account" (
"user_id" text NOT NULL,
"type" text NOT NULL,
"provider" text NOT NULL,
"provider_account_id" text NOT NULL,
"refresh_token" text,
"access_token" text,
"expires_at" integer,
"token_type" text,
"scope" text,
"id_token" text,
"session_state" text,
CONSTRAINT "account_provider_provider_account_id_pk" PRIMARY KEY("provider","provider_account_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "session" (
"session_token" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"expires" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
"id" text PRIMARY KEY NOT NULL,
"name" text,
"email" text NOT NULL,
"email_verified" timestamp,
"image" text,
"role" "role" DEFAULT 'user' NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "user_email_unique" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "verification_token" (
"identifier" text NOT NULL,
"token" text NOT NULL,
"expires" timestamp NOT NULL,
CONSTRAINT "verification_token_identifier_token_pk" PRIMARY KEY("identifier","token")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "comment" (
"id" text PRIMARY KEY NOT NULL,
"body" text NOT NULL,
"user_id" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
"post_id" text NOT NULL,
"parent_id" text,
"is_deleted" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "rate" (
"user_id" text NOT NULL,
"comment_id" text NOT NULL,
"like" boolean NOT NULL,
CONSTRAINT "rate_user_id_comment_id_pk" PRIMARY KEY("user_id","comment_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "guestbook" (
"id" text PRIMARY KEY NOT NULL,
"body" text NOT NULL,
"user_id" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "likes_session" (
"id" text PRIMARY KEY NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"likes" integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "post" (
"created_at" timestamp DEFAULT now() NOT NULL,
"slug" text PRIMARY KEY NOT NULL,
"likes" integer DEFAULT 0 NOT NULL,
"views" integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "comment" ADD CONSTRAINT "comment_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "comment" ADD CONSTRAINT "comment_post_id_post_slug_fk" FOREIGN KEY ("post_id") REFERENCES "public"."post"("slug") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "rate" ADD CONSTRAINT "rate_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "rate" ADD CONSTRAINT "rate_comment_id_comment_id_fk" FOREIGN KEY ("comment_id") REFERENCES "public"."comment"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "guestbook" ADD CONSTRAINT "guestbook_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit 030f849

Please sign in to comment.