版本: v1.0 最后更新: 2026-04-03 受众: 开发者、数据库管理员 阅读时间: 15 分钟
PostWaver 使用 SQLite 作为数据库,通过 Prisma ORM 进行数据访问。本文档详细说明了数据库架构、表结构和关系。
packages/database/prisma/database.db┌─────────────┐ ┌──────────────────┐ ┌─────────────┐
│ Post │───────│ PublishRecord │───────│ Platform │
│ │ 1 N │ │ N 1 │ Mapping │
└─────────────┘ └──────────────────┘ └─────────────┘
│ │
│ N │ N
│ │
┌──────────────┐ ┌─────────────┐
│ Tag │ │ Config │
│ │ │ │
└──────────────┘ └─────────────┘
用途: 存储文章元数据
| 字段 | 类型 | 说明 | 约束 |
|---|---|---|---|
| id | String | 主键 | Primary Key |
| title | String | 标题 | Not Null |
| slug | String | URL 友好标识 | Unique, Not Null |
| content | Text | Markdown 内容 | Not Null |
| excerpt | String | 摘要 | |
| status | Enum | 状态 | DRAFT, PUBLISHED, ARCHIVED |
| publishedAt | DateTime | 发布时间 | |
| tags | String[] | 标签列表 | |
| categories | String[] | 分类列表 | |
| createdAt | DateTime | 创建时间 | Default: now() |
| updatedAt | DateTime | 更新时间 | Default: now() |
| cleanedAt | DateTime | 清理时间 | |
| hash | String | 内容哈希 | Unique |
索引:
slug (UNIQUE)statuspublishedAthash (UNIQUE)用途: 存储文章发布记录
| 字段 | 类型 | 说明 | 约束 |
|---|---|---|---|
| id | String | 主键 | Primary Key |
| postId | String | 文章 ID | Foreign Key → Post.id |
| platform | Enum | 平台 | j看好in, wechat, csdn, zhihu |
| platformPostId | String | 平台文章 ID | |
| platformUrl | String | 平台文章 URL | |
| status | Enum | 发布状态 | PENDING, PUBLISHED, FAILED |
| publishedAt | DateTime | 发布时间 | |
| createdAt | DateTime | 创建时间 | Default: now() |
| updatedAt | DateTime | 更新时间 | Default: now() |
索引:
postId + platform (UNIQUE)platformPostId (UNIQUE)用途: 存储平台 ID 映射关系
| 字段 | 类型 | 说明 | 约束 |
|---|---|---|---|
| id | String | 主键 | Primary Key |
| postId | String | 文章 ID | Foreign Key → Post.id |
| platform | Enum | 平台 | j看好in, wechat, csdn, zhihu |
| platformPostId | String | 平台文章 ID | |
| platformUrl | String | 平台文章 URL | |
| createdAt | DateTime | 创建时间 | Default: now() |
| updatedAt | DateTime | 更新时间 | Default: now() |
索引:
postId + platform (UNIQUE)用途: 存储标签信息
| 字段 | 类型 | 说明 | 约束 |
|---|---|---|---|
| id | String | 主键 | Primary Key |
| name | String | 标签名 | Unique, Not Null |
| count | Int | 使用次数 | Default: 0 |
| createdAt | DateTime | 创建时间 | Default: now() |
| updatedAt | DateTime | 更新时间 | Default: now() |
索引:
name (UNIQUE)count用途: 存储系统配置
| 字段 | 类型 | 说明 | 约束 |
|---|---|---|---|
| id | String | 主键 | Primary Key |
| key | String | 配置键 | Unique, Not Null |
| value | String | 配置值 | Not Null |
| type | Enum | 值类型 | STRING, NUMBER, BOOLEAN, JSON |
| createdAt | DateTime | 创建时间 | Default: now() |
| updatedAt | DateTime | 更新时间 | Default: now() |
索引:
key (UNIQUE)关系: 一对多
说明: 一篇文章可以发布到多个平台
示例:
const post = await prisma.post.findUnique({
where: { id: 'post-id' },
include: { publishRecords: true }
})
关系: 多对多(通过 Post.tags 字段)
说明: 一篇文章可以有多个标签
示例:
const posts = await prisma.post.findMany({
where: {
tags: {
hasSome: ['javascript', 'typescript']
}
}
})
关系: 一对多
说明: 一篇文章可以映射到多个平台
cd packages/database
pnpm prisma migrate dev --name add_new_field
pnpm prisma migrate deploy
pnpm prisma migrate reset
const post = await prisma.post.create({
data: {
title: 'Hello World',
slug: 'hello-world',
content: '# Hello World',
status: 'PUBLISHED',
tags: ['test', 'example']
}
})
const post = await prisma.post.findUnique({
where: { id: 'post-id' }
})
const posts = await prisma.post.findMany({
where: { status: 'PUBLISHED' },
orderBy: { publishedAt: 'desc' }
})
const post = await prisma.post.update({
where: { id: 'post-id' },
data: {
title: 'Updated Title',
updatedAt: new Date()
}
})
await prisma.post.delete({
where: { id: 'post-id' }
})
slug, hash, name (tag)postId + platformstatus, publishedAt, countselect 只选择需要的字段include 预加载关联数据where 精确过滤take 和 skip 分页pnpm db:studio
访问: http://localhost:5555
# 启用查询日志
DEBUG="prisma:query" pnpm start
最后更新: 2026-04-03 维护者: PostWaver Team 反馈: GitHub Issues