26 lines
872 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// SQLite 默认 DELETE journal 全库写锁;WAL 持久化到库文件,busy_timeout 需每个连接设置。
// 启动期数据库尚未就绪时按递增间隔重试,避免 worker 首查就抛 PrismaClientKnownRequestError。
export const ensureDatabaseReady = async (maxAttempts = 10) => {
let lastError: unknown
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
await prisma.$queryRawUnsafe('PRAGMA journal_mode=WAL;')
await prisma.$queryRawUnsafe('PRAGMA busy_timeout=5000;')
await prisma.$queryRaw`SELECT 1`
return
} catch (error) {
lastError = error
if (attempt < maxAttempts) {
await new Promise((resolve) => setTimeout(resolve, 300 * attempt))
}
}
}
throw lastError
}
export default prisma