Files
novalon-website/src/lib/db.ts
T
zhangxiang a366bd1400 fix(qa): 第五轮系统性质量保障缺陷修复 + 单元/集成测试
安全/鉴权:登出清服务端 httpOnly 令牌、登录 IP 限流前置 bcrypt、停用账号
令牌在 6 个内联 role handler 失效、admin/items 存在性 oracle 认证前置、
同意偏好 sanitize fail-closed、api-crypto 按 HTTP 方法判体。

CMS:workflow 状态更新+审计收进交互式 $transaction(失败即回滚)、编辑器
richtext 走 textarea 防换行损毁、array/object 子字段按作用域读写防污染、
about/contact/erp-upgrade 补 ISR revalidate 与路由映射。

UI/a11y:AnimatedCounter 入视口前显起点、CLS 累计上报、MetricCard 动画入
effect 并卸载取消 rAF、后台状态四态映射/搜索防抖+序号守卫/媒体单 input/
吞错改横幅/表单 label-aria 关联。

新增对应判别性单测与 pagination/rate-limit/client-ip/sanitize/validate-content-data
等模块及 tests-integration 真库集成层。
2026-09-28 10:48:07 +08:00

48 lines
2.4 KiB
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 '@/generated/prisma/client';
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}
/**
* SQLite 并发默认值调优(验收 B-5)。
*
* 实测(@prisma/client 6.19.3 + 生成客户端 src/generated/prisma,对临时库文件跑 `PRAGMA` 读回):
* - journal_mode 默认 'delete'(rollback journal)⇒ 同一时刻只允许一个写者,
* 并发写会立刻 SQLITE_BUSY;WAL 允许「一读多写」并显著缩小写锁窗口。
* - busy_timeout 默认已是 5000(并非 0),此处仍显式设置以固定预期、不依赖引擎默认值。
* - datasource URL 上的查询参数(`?journal_mode=WAL&busy_timeout=…`)在本版本被**静默忽略**:
* `file:<db>?journal_mode=WAL` 与 `file:<db>?totally_bogus=1` 都读回 journal_mode='delete',
* 故不能靠改 DATABASE_URL 解决(该结论也意味着「改 .env/schema.prisma」并非正确方向)。
* - `$on('beforeConnect')` 在本生成客户端不可用:`$on` 只接受 LogLevel
* (src/generated/prisma/internal/class.ts:128 + prismaNamespace.ts:1515),
* `beforeConnect` 仅存在于 binary 运行时(node_modules/@prisma/client/runtime/binary.js)。
* - `PRAGMA journal_mode=WAL` 对**库文件**持久生效(新客户端实例不再执行 PRAGMA 仍读回 'wal'),
* 因此进程内执行一次即可覆盖后续连接;busy_timeout 是连接级设置,但 SQLite 的默认值已足够。
*
* 失败只记录不上抛:数据库不可用时真正的业务查询会给出更准确的错误,
* 不能让一个 PRAGMA 失败把整个模块导入变成 rejection。
*/
let sqliteTuning: Promise<void> | null = null;
export function applySqliteConcurrencyDefaults(): Promise<void> {
if (!sqliteTuning) {
sqliteTuning = (async () => {
await prisma.$queryRaw`PRAGMA journal_mode=WAL`;
await prisma.$queryRaw`PRAGMA busy_timeout=5000`;
})().catch((error: unknown) => {
console.error('Failed to apply SQLite WAL/busy_timeout pragmas:', error);
});
}
return sqliteTuning;
}
// 模块首次被导入即尽力开启 WAL(等价于「连接时设置」,因为 Prisma 会在此处隐式建立连接)。
void applySqliteConcurrencyDefaults();