Implement Cloudflare D1 support with Prisma, update scripts, and enhance documentation

This commit is contained in:
2025-06-01 05:22:44 +02:00
parent 0c18e8be57
commit 71c8aff125
11 changed files with 884 additions and 11 deletions

View File

@ -1,5 +1,6 @@
// Simple Prisma client setup
// Prisma client setup with support for Cloudflare D1
import { PrismaClient } from "@prisma/client";
import { PrismaD1 } from "@prisma/adapter-d1";
// Add prisma to the NodeJS global type
// This approach avoids NodeJS.Global which is not available
@ -9,12 +10,24 @@ declare const global: {
prisma: PrismaClient | undefined;
};
// Initialize Prisma Client
const prisma = global.prisma || new PrismaClient();
// Check if we're running in Cloudflare Workers environment
const isCloudflareWorker = typeof globalThis.DB !== 'undefined';
// Save in global if we're in development
if (process.env.NODE_ENV !== "production") {
global.prisma = prisma;
// Initialize Prisma Client
let prisma: PrismaClient;
if (isCloudflareWorker) {
// In Cloudflare Workers, use D1 adapter
const adapter = new PrismaD1(globalThis.DB);
prisma = new PrismaClient({ adapter });
} else {
// In Next.js/Node.js, use regular SQLite
prisma = global.prisma || new PrismaClient();
// Save in global if we're in development
if (process.env.NODE_ENV !== "production") {
global.prisma = prisma;
}
}
export { prisma };