feat: 迁移数据库至 SQLite 并新增项目文档

- 将 Postgres 数据库替换为 SQLite
- 并同步添加 README 文档以优化项目初始化流程
This commit is contained in:
2026-01-20 16:56:11 +08:00
parent 5513979ebc
commit b967deb4b1
14 changed files with 482 additions and 78 deletions

View File

@@ -1,22 +1,41 @@
/**
* 数据库中间件
*
* 为 ORPC 处理器提供数据库连接。使用单例模式管理连接,
* 避免每次请求都创建新连接。
*/
import { os } from '@orpc/server'
import { createDb } from '@/db'
import { createDb, type Db } from '@/db'
const IS_SERVERLESS = false // TODO: 这里需要优化
let globalDb: ReturnType<typeof createDb> | null = null
function getDb() {
if (IS_SERVERLESS) {
return createDb()
}
/** 全局数据库实例 (单例模式) */
let globalDb: Db | null = null
/**
* 获取数据库实例
*
* 首次调用时创建连接,后续调用返回同一实例。
* 这种模式适合长时间运行的服务器进程。
*/
function getDb(): Db {
if (!globalDb) {
globalDb = createDb()
}
return globalDb
}
/**
* 数据库提供者中间件
*
* 使用方式:
* ```ts
* export const list = os.todo.list
* .use(dbProvider)
* .handler(async ({ context }) => {
* // context.db 可用
* return context.db.query.todoTable.findMany()
* })
* ```
*/
export const dbProvider = os.middleware(async ({ context, next }) => {
const db = getDb()