Files
Edu/services/content/src/config/neo4j.ts
SpecialX 921fe82771 feat(content): 修复服务并添加chapters/knowledge-points/questions模块
- database.ts 导出db常量替代getDb()函数

- env.ts JWT_SECRET/ES_URL/NEO4J_URL改optional加DEV_MODE

- neo4j.ts driver惰性创建+try/catch+connectionTimeout:3000

- health/lifecycle改用Drizzle原生查询

- textbooks.schema修复integer到int+导出NewTextbook类型

- 新建chapters/knowledge-points/questions三模块CRUD

- knowledge-points含Neo4j前置依赖图非阻塞查询

- content-init.sql创建4张表

端到端验证: textbooks/chapters/knowledge-points/questions全CRUD通过
2026-07-09 08:52:15 +08:00

39 lines
1.1 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 neo4j from "neo4j-driver";
import type { Driver, Session } from "neo4j-driver";
import { env } from "./env.js";
// Neo4j driver 创建为惰性初始化:未配置 NEO4J_URL / NEO4J_PASSWORD 时
// driver 保持 null服务仍可正常启动。所有依赖 Neo4j 的查询在
// driver 为 null 时返回空结果或抛出可控错误,不会阻塞主流程。
let driver: Driver | null = null;
try {
if (env.NEO4J_URL && env.NEO4J_PASSWORD) {
driver = neo4j.driver(
env.NEO4J_URL,
neo4j.auth.basic("neo4j", env.NEO4J_PASSWORD),
// 连接超时 3sNeo4j 不可用时快速失败,避免拖慢 HTTP 响应
{ connectionTimeout: 3000, maxConnectionLifetime: 60_000 },
);
}
} catch (err) {
console.warn(
"Neo4j driver init failed, running without Neo4j:",
err instanceof Error ? err.message : String(err),
);
driver = null;
}
export function getNeo4jSession(): Session | null {
if (!driver) {
return null;
}
return driver.session();
}
export async function closeNeo4j(): Promise<void> {
if (driver) {
await driver.close();
}
}