Skip to content

Commit 74bc7a2

Browse files
committed
chore: update Dockerfile to set placeholder DATABASE_URL for build time and copy Prisma schema; modify getAllCocktails and getCocktailFromDB functions to handle build time logic
1 parent 922da54 commit 74bc7a2

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

.dockerignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Dependencies
2+
node_modules
3+
npm-debug.log
4+
pnpm-debug.log
5+
yarn-error.log
6+
7+
# Next.js
8+
.next
9+
out
10+
11+
# Environment files
12+
.env
13+
.env.local
14+
.env.development.local
15+
.env.test.local
16+
.env.production.local
17+
18+
# IDE
19+
.vscode
20+
.idea
21+
*.swp
22+
*.swo
23+
*~
24+
25+
# OS
26+
.DS_Store
27+
Thumbs.db
28+
29+
# Git
30+
.git
31+
.gitignore
32+
33+
# Docker
34+
Dockerfile
35+
docker-compose.yml
36+
.dockerignore
37+
38+
# Documentation
39+
README.md
40+
*.md
41+
42+
# Tests
43+
coverage
44+
.nyc_output
45+
46+
# Misc
47+
.eslintcache
48+

Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ RUN pnpm install --frozen-lockfile --shamefully-hoist
1919
# 复制所有源代码
2020
COPY . .
2121

22+
# 设置占位符环境变量用于构建时的 Prisma 客户端生成
23+
# 这个 URL 只在构建时使用,运行时会被实际的 DATABASE_URL 替换
24+
ENV DATABASE_URL="postgresql://placeholder:placeholder@placeholder:5432/placeholder?schema=public"
25+
2226
# 生成 Prisma 客户端
2327
RUN npx prisma generate
2428

@@ -46,8 +50,10 @@ COPY --from=builder /app/next.config.mjs ./
4650
# 安装生产依赖
4751
RUN pnpm install --prod --frozen-lockfile --shamefully-hoist
4852

49-
# 复制 Prisma 生成的客户端(必须在安装依赖之后)
53+
# 复制 Prisma schema 和生成的客户端
54+
COPY --from=builder /app/prisma ./prisma
5055
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
56+
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma
5157

5258
# 设置环境变量
5359
ENV NODE_ENV=production

lib/cocktail-data.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,38 @@ function mapDBCocktailToAppCocktail(dbCocktail: any): Cocktail {
2929
}
3030

3131
export async function getAllCocktails(): Promise<Cocktail[]> {
32+
// Check if we're in build time (no DATABASE_URL means we're building)
33+
const isBuildTime = !process.env.DATABASE_URL ||
34+
process.env.DATABASE_URL.includes('placeholder');
35+
36+
// During build time, return empty array or popular cocktails
37+
if (isBuildTime) {
38+
return Object.values(popularCocktails);
39+
}
40+
3241
try {
3342
const cocktails = await prisma.cocktail.findMany({
3443
orderBy: { createdAt: "desc" },
3544
});
3645
return cocktails.map(mapDBCocktailToAppCocktail);
3746
} catch (error) {
3847
console.error("Error fetching cocktails from DB:", error);
39-
return [];
48+
return Object.values(popularCocktails);
4049
}
4150
}
4251

4352
export async function getCocktailFromDB(id: string): Promise<Cocktail | null> {
44-
try {
45-
// First check if it's a popular cocktail ID (hardcoded)
46-
// If it is, we might want to return the hardcoded one OR check if we seeded it.
47-
// For now, let's prioritize DB, then fallback to popularCocktails.
53+
// Check if we're in build time (no DATABASE_URL means we're building)
54+
const isBuildTime = !process.env.DATABASE_URL ||
55+
process.env.DATABASE_URL.includes('placeholder');
4856

57+
// During build time, only use popular cocktails
58+
if (isBuildTime) {
59+
return popularCocktails[id] || null;
60+
}
61+
62+
try {
63+
// At runtime, try to fetch from DB first
4964
const dbCocktail = await prisma.cocktail.findUnique({
5065
where: { id },
5166
});

0 commit comments

Comments
 (0)