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

@@ -8,7 +8,7 @@
- **运行时**: Bun
- **语言**: TypeScript (strict mode, ESNext)
- **样式**: Tailwind CSS v4
- **数据库**: PostgreSQL + Drizzle ORM
- **数据库**: SQLite (Bun 内置) + Drizzle ORM
- **状态管理**: TanStack Query
- **路由**: TanStack Router (文件路由)
- **RPC**: ORPC (类型安全 RPC契约优先)
@@ -44,9 +44,10 @@ biome format --write . # 仅格式化代码
### 数据库
```bash
bun db:init # 初始化 SQLite 数据库 (创建表)
bun db:generate # 从 schema 生成迁移文件
bun db:migrate # 执行数据库迁移
bun db:push # 直接推送 schema 变更 (仅开发环境)
bun db:studio # 打开 Drizzle Studio 数据库管理界面
```
### 测试
@@ -145,20 +146,25 @@ export const Route = createFileRoute('/')({
-`src/db/schema/*.ts` 定义 schema
-`src/db/schema/index.ts` 导出
- 使用 `drizzle-orm/pg-core`PostgreSQL 类型
- 主键使用 `uuidv7()` (需要 PostgreSQL 扩展)
- 使用 `drizzle-orm/sqlite-core` 的 SQLite 类型
- 主键使用 `crypto.randomUUID()` 生成 UUID
- 始终包含 `createdAt``updatedAt` 时间戳
示例:
```typescript
import { pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
export const myTable = pgTable('my_table', {
id: uuid().primaryKey().default(sql`uuidv7()`),
name: text().notNull(),
createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true }).notNull().defaultNow().$onUpdateFn(() => new Date()),
export const myTable = sqliteTable('my_table', {
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
name: text('name').notNull(),
createdAt: integer('created_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`),
updatedAt: integer('updated_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`)
.$onUpdateFn(() => new Date()),
})
```
@@ -273,5 +279,5 @@ const mutation = useMutation(orpc.myFeature.create.mutationOptions())
---
**最后更新**: 2026-01-18
**最后更新**: 2026-01-20
**项目版本**: 基于 package.json 依赖版本