fix(prisma): redirect SQLite file target to writeable /tmp path in serverless contexts

This commit is contained in:
FalahMobile
2026-07-08 19:28:55 +08:00
parent 8861e9143d
commit e2f8d7be66
2 changed files with 39 additions and 4 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ export default defineConfig({
workers: process.env.CI ? 1 : undefined, workers: process.env.CI ? 1 : undefined,
reporter: 'html', reporter: 'html',
use: { use: {
baseURL: 'https://6a4e32b8782b7f28bdd55a45--mobile2-staging-falah.netlify.app', baseURL: 'https://6a4e33d683bf9228e13b4f35--mobile2-staging-falah.netlify.app',
trace: 'on-first-retry', trace: 'on-first-retry',
video: 'retain-on-failure', video: 'retain-on-failure',
}, },
+38 -3
View File
@@ -1,7 +1,42 @@
import { PrismaClient } from '@prisma/client' import { PrismaClient } from '@prisma/client'
import fs from 'fs'
import path from 'path'
let prisma: PrismaClient
if (process.env.LAMBDA_TASK_ROOT || process.env.NETLIFY) {
const targetDbPath = '/tmp/dev.db'
if (!fs.existsSync(targetDbPath)) {
let sourceDbPath = path.join(process.cwd(), 'prisma', 'dev.db')
if (!fs.existsSync(sourceDbPath)) {
sourceDbPath = path.join(process.cwd(), 'dev.db')
}
try {
if (fs.existsSync(sourceDbPath)) {
fs.copyFileSync(sourceDbPath, targetDbPath)
fs.chmodSync(targetDbPath, 0o666)
console.log(`Copied database to writeable path: ${targetDbPath}`)
} else {
console.error(`Database template not found at: ${sourceDbPath}`)
}
} catch (e) {
console.error('Error copying SQLite database to /tmp:', e)
}
}
prisma = new PrismaClient({
datasources: {
db: {
url: `file:${targetDbPath}`
}
}
})
} else {
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient } const globalForPrisma = globalThis as unknown as { prisma: PrismaClient }
prisma = globalForPrisma.prisma ?? new PrismaClient()
export const prisma = globalForPrisma.prisma ?? new PrismaClient()
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
}
export { prisma }