# 数据库模型文档 ## ArticleModel ArticleModel 是一个功能完整的文章管理模型,提供了丰富的CRUD操作和查询方法。 ### 主要特性 - ✅ 完整的CRUD操作 - ✅ 文章状态管理(草稿、已发布、已归档) - ✅ 自动生成slug、摘要和阅读时间 - ✅ 标签和分类管理 - ✅ SEO优化支持 - ✅ 浏览量统计 - ✅ 相关文章推荐 - ✅ 全文搜索功能 ### 数据库字段 | 字段名 | 类型 | 说明 | |--------|------|------| | id | integer | 主键,自增 | | title | string | 文章标题(必填) | | content | text | 文章内容(必填) | | author | string | 作者 | | category | string | 分类 | | tags | string | 标签(逗号分隔) | | keywords | string | SEO关键词 | | description | string | 文章描述 | | status | string | 状态:draft/published/archived | | published_at | timestamp | 发布时间 | | view_count | integer | 浏览量 | | featured_image | string | 特色图片 | | excerpt | text | 文章摘要 | | reading_time | integer | 阅读时间(分钟) | | meta_title | string | SEO标题 | | meta_description | text | SEO描述 | | slug | string | URL友好的标识符 | | created_at | timestamp | 创建时间 | | updated_at | timestamp | 更新时间 | ### 基本用法 ```javascript import { ArticleModel } from '../models/ArticleModel.js' // 创建文章 const article = await ArticleModel.create({ title: "我的第一篇文章", content: "这是文章内容...", author: "张三", category: "技术", tags: "JavaScript, Node.js, 教程" }) // 查找所有已发布的文章 const publishedArticles = await ArticleModel.findPublished() // 根据ID查找文章 const article = await ArticleModel.findById(1) // 更新文章 await ArticleModel.update(1, { title: "更新后的标题", content: "更新后的内容" }) // 发布文章 await ArticleModel.publish(1) // 删除文章 await ArticleModel.delete(1) ``` ### 查询方法 #### 基础查询 - `findAll()` - 查找所有文章 - `findById(id)` - 根据ID查找文章 - `findBySlug(slug)` - 根据slug查找文章 - `findPublished()` - 查找所有已发布的文章 - `findDrafts()` - 查找所有草稿文章 #### 分类查询 - `findByAuthor(author)` - 根据作者查找文章 - `findByCategory(category)` - 根据分类查找文章 - `findByTags(tags)` - 根据标签查找文章 #### 搜索功能 - `searchByKeyword(keyword)` - 关键词搜索(标题、内容、关键词、描述、摘要) #### 统计功能 - `getArticleCount()` - 获取文章总数 - `getPublishedArticleCount()` - 获取已发布文章数量 - `getArticleCountByCategory()` - 按分类统计文章数量 - `getArticleCountByStatus()` - 按状态统计文章数量 #### 推荐功能 - `getRecentArticles(limit)` - 获取最新文章 - `getPopularArticles(limit)` - 获取热门文章 - `getFeaturedArticles(limit)` - 获取特色文章 - `getRelatedArticles(articleId, limit)` - 获取相关文章 #### 高级查询 - `findByDateRange(startDate, endDate)` - 按日期范围查找文章 - `incrementViewCount(id)` - 增加浏览量 ### 状态管理 文章支持三种状态: - `draft` - 草稿状态 - `published` - 已发布状态 - `archived` - 已归档状态 ```javascript // 发布文章 await ArticleModel.publish(articleId) // 取消发布 await ArticleModel.unpublish(articleId) ``` ### 自动功能 #### 自动生成slug 如果未提供slug,系统会自动根据标题生成: ```javascript // 标题: "我的第一篇文章" // 自动生成slug: "我的第一篇文章" ``` #### 自动计算阅读时间 基于内容长度自动计算阅读时间(假设每分钟200个单词) #### 自动生成摘要 如果未提供摘要,系统会自动从内容中提取前150个字符 ### 标签管理 标签支持逗号分隔的格式,系统会自动处理: ```javascript // 输入: "JavaScript, Node.js, 教程" // 存储: "JavaScript, Node.js, 教程" // 查询: 支持模糊匹配 ``` ### SEO优化 支持完整的SEO字段: - `meta_title` - 页面标题 - `meta_description` - 页面描述 - `keywords` - 关键词 - `slug` - URL友好的标识符 ### 错误处理 所有方法都包含适当的错误处理: ```javascript try { const article = await ArticleModel.create({ title: "", // 空标题会抛出错误 content: "内容" }) } catch (error) { console.error("创建文章失败:", error.message) } ``` ### 性能优化 - 所有查询都包含适当的索引 - 支持分页查询 - 缓存友好的查询结构 ### 迁移和种子 项目包含完整的数据库迁移和种子文件: - `20250830014825_create_articles_table.mjs` - 创建articles表 - `20250830020000_add_article_fields.mjs` - 添加额外字段 - `20250830020000_articles_seed.mjs` - 示例数据 ### 运行迁移和种子 ```bash # 运行迁移 npx knex migrate:latest # 运行种子 npx knex seed:run ```