AstroResearch/docs/database.md

2.4 KiB
Raw Blame History

AstroResearch Database Schema / 数据库设计

AstroResearch 使用轻量级、零配置的 SQLite 数据库作为持久化存储。数据库文件默认保存在项目根目录下的 astro_research.db,由 Rust 中的 sqlx 驱动管理并自动执行迁移。


1. 实体关系图 (Entity-Relationship Diagram)

erDiagram
    PAPERS {
        text bibcode PK
        text title
        text authors "JSON Array"
        text year
        text pub "Journal/Publisher"
        text keywords "JSON Array"
        text abstract
        text doi
        text arxiv_id
        integer citation_count
        integer reference_count
        text pdf_path
        text html_path
        text markdown_path
        text translation_path
        datetime created_at
    }

    NOTES {
        integer id PK
        text bibcode FK
        integer paragraph_index
        text note_text
        text highlight_color
        text selected_text
        datetime created_at
    }

    CITATIONS_REFERENCES {
        text source_bibcode PK
        text target_bibcode PK
    }

    PAPERS ||--o{ NOTES : "has"
    PAPERS ||--o{ CITATIONS_REFERENCES : "cites / cited_by"

2. 数据表结构详述 (Table Schema Details)

2.1 papers 表 (文献元数据)

存储文献的核心元数据和本地物理存储路径。

  • 索引
    • idx_papers_doi -> 基于 doi
    • idx_papers_arxiv_id -> 基于 arxiv_id

2.2 citations_references 表 (引文与参考文献拓扑)

多对多关联表,存储文献之间的引用网络(即拓扑星系图的基础数据)。

  • 复合主键(source_bibcode, target_bibcode)
  • 索引
    • idx_citations_ref_source -> 优化以 source_bibcode 查询参考文献
    • idx_citations_ref_target -> 优化以 target_bibcode 查询被引文献

2.3 notes 表 (高亮与阅读笔记)

存储学者在阅读器中对特定段落创建的高亮和笔记。

  • 外键bibcode 级联删除 (ON DELETE CASCADE)。
  • 索引
    • idx_notes_bibcode -> 优化单篇文献的笔记列表查询。

3. 数据库迁移说明

迁移脚本存放在 migrations/ 下,服务启动时(src/main.rs)会自动调用 sqlx::migrate!().run(&pool).await 自动部署:

  1. 20260608000000_init.sql:初始化 paperscitations_references 结构。
  2. 20260608000001_notes.sql:添加 notes 笔记高亮表,并为关联建立级联删除。