Skip to content

Commit 777be4e

Browse files
committed
feat: add Prisma setup with PostgreSQL database and user model
1 parent e8cb131 commit 777be4e

File tree

11 files changed

+1002
-4
lines changed

11 files changed

+1002
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ yarn-error.log*
3939
# typescript
4040
*.tsbuildinfo
4141
next-env.d.ts
42+
43+
/app/generated/prisma

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Build
2+
FROM node:20-bookworm AS builder
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
RUN npm ci
7+
8+
COPY . .
9+
RUN npx prisma generate
10+
RUN npm run build
11+
12+
FROM node:20-bookworm AS runner
13+
WORKDIR /app
14+
ENV NODE_ENV=production
15+
16+
COPY --from=builder /app/package*.json ./
17+
COPY --from=builder /app/node_modules ./node_modules
18+
COPY --from=builder /app/.next ./.next
19+
COPY --from=builder /app/public ./public
20+
COPY --from=builder /app/prisma ./prisma
21+
22+
EXPOSE 3000
23+
CMD ["npm", "start"]

app/api/users/route.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { prisma } from "@/lib/prisma";
2+
3+
export async function GET() {
4+
const users = await prisma.user.findMany();
5+
return Response.json(users);
6+
}

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
db:
3+
image: postgres:16
4+
container_name: sander-tf-postgres
5+
restart: unless-stopped
6+
ports:
7+
- "5432:5432"
8+
environment:
9+
POSTGRES_USER: postgres
10+
POSTGRES_PASSWORD: postgres
11+
POSTGRES_DB: sander-tf
12+
volumes:
13+
- pgdata:/var/lib/postgresql/data
14+
volumes:
15+
pgdata:

lib/prisma.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PrismaClient } from "@prisma/client";
2+
3+
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
4+
5+
export const prisma =
6+
globalForPrisma.prisma ??
7+
new PrismaClient({
8+
log: ["error", "warn"],
9+
});
10+
11+
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

0 commit comments

Comments
 (0)