语言
主题
定位中日出--:--日落--:--AUTO
返回主页

博客的第一阶段不引入数据库。文章就是根目录 article/ 下的 Markdown 文件,由 Astro Content Collections 管理。

为什么是文件系统

文件即数据库的代价最低:Git 负责版本历史,编辑器负责写作体验,构建负责验证。没有迁移、没有连接池、没有备份策略。

类型安全的 frontmatter

Content Collection 的 schema 在构建时校验每篇文章的元数据:

const blog = defineCollection({
  loader: glob({ pattern: '**/*.md', base: './article' }),
  schema: z.object({
    title: z.string(),
    description: z.string(),
    pubDate: z.coerce.date(),
    tags: z.array(z.string()).default([]),
  }),
});

字段写错类型会在 astro build 时直接失败,而不是在线上静默出错。

什么时候需要数据库

当出现构建期无法知道的数据——评论、阅读量、用户状态——才考虑引入。在那之前,文件系统是正确的默认值。