name: flexible-database-design version: 1.0.0 description: | Guide agents and users to design and implement a "flexible database" on SQLite that can handle semi-structured, multi-source data. Typical scenarios: personal knowledge base, PDF/report archive, policy tracking, fragmented notes, custom form/questionnaire fields, multi-source data aggregation, event logging. When the user says things like "I want to build a knowledge base", "archive PDF reports", "search within report content", "collect policies or scattered information", this skill provides the end-to-end workflow. It includes: design principles, a three-layer model, agent workflows, schema templates, and reference Python scripts. metadata: openclaw: emoji: 🧱 requires: bins: ["python3"] install: - id: python kind: system bins: ["python3"]
一套可複用的「軟 Schema」設計方法:主幹硬、尾巴軟,三層演進。使用者安裝後,Agent 可據此指導其真正構建出靈活資料庫。
| 層級 | 作用 | 典型做法 |
|---|---|---|
| 原始層 | 不丟資訊、可追溯 | 整條記錄原樣存,加雜湊去重、來源、版本號 |
| 軟欄位層 | 靈活查詢 | JSON 存結構化結果;鍵值對錶按 key 查、聚合 |
| 業務檢視層 | 高頻查詢、報表 | 物化表/檢視,按需建索引 |
向用戶確認以下資訊,並記錄到對話中:
| 問題 | 用途 |
|---|---|
| 你的資料主要來自哪裡?(微信/網頁/API/手動輸入/多種) | 定 source_type 列舉 |
| 每條記錄大概有哪些「永遠會有」的資訊?(如:時間、來源、型別) | 定主幹欄位 |
| 有哪些「可能經常變、不同來源不一樣」的資訊? | 確認用 JSON/鍵值對 |
| 是否有按編號/文號等唯一標識查詢的需求? | 決定是否在檢視中加入對應欄位,並考慮快捷查詢 |
| 需要全文搜尋嗎? | 決定是否建 FTS |
| 內容語言?(中文為主 / 英文為主 / 混合) | 決定是否採用中文分詞、FTS 策略 |
| 內容形態?(純文本 / PDF / Excel / 網頁 / 混合) | 決定歸檔前是否需要提取(如 pypdf) |
| 預期資料量?(百 / 千 / 萬 / 十萬級) | 決定 LIKE 回退是否可用、是否考慮外部搜尋引擎 |
根據使用者描述,選擇最接近的場景並適配:
| 場景 | 主幹欄位建議 | 軟欄位典型 key |
|---|---|---|
| 個人知識庫 / 碎片收集 | id, created_at, source, content_type, raw_content | title, tags, url, project, deadline |
| 政策資訊收集 | id, created_at, source, source_type | title, release_date, issuing_org, policy_type, url, policy_no, industry, subsidy_amount |
| 財務報表收集 | id, created_at, source, source_type | company, report_type, report_date, revenue, net_income, total_assets, roa, roe |
| 表單/問卷 | id, created_at, form_id, respondent_id, raw_response | 各題目 id 或題目名 |
| PDF/報告知識庫 | id, created_at, source, content_type, raw_content | report_title, report_type, period_start, period_end, file_path |
| 多源異構(如群訊息聚合) | id, created_at, source, sender, raw_content | data_type, items[], trend, 各業務欄位 |
source_type 的 CHECK 列舉;messages_fts 相關注釋;data/ 目錄,指定 db 路徑(如 data/flexible.db)。| 內容語言 | 推薦方案 | 說明 |
|---|---|---|
| 英文為主 | FTS5 (unicode61) | 預設即可 |
| 中文為主 | FTS + LIKE 回退 | 長短語易漏檢,需實現 recall() |
| 中文為主(資料量 < 5000) | 同上 + 短詞拆分 | 如「煤炭期貨價格」→ 拆為「煤炭」「期貨」「價格」分別查,取並集 |
| 中文為主(資料量 > 5000) | 考慮 Meilisearch / jieba+FTS | SQLite FTS 中文能力有限 |
實現要點:查詢層實現 recall(keyword):先 FTS,無結果則 LIKE;中文可加短詞拆分。LIKE '%x%' 無法用索引,資料量大時需評估效能。掃描件 PDF 無法提取正文,歸檔時需跳過或標記。
scripts/:核心 flexible_db.py、archive_item.py、query_items.py;可選 manage_item.py、import_batch.py、quick_validate.py、extractors/。flexible_db.py 中的 db_path 指向使用者的 db 路徑。archive_item.py --llm-extract 或配置 FLEXIBLE_EXTRACTOR=extractors.dummy:extract;可自定義 extractors/ 下的實現。抽取器欄位應與場景匹配:政策類建議 title、release_date、issuing_org、policy_type、url、policy_no;知識庫類建議 title、tags、url、project;財報類建議 company、report_type、report_date、revenue、net_income(金額統一存「元」);PDF/報告類建議 report_title、report_type、period_start、period_end。pip install pypdf),掃描件無法提取時跳過。可選:將 PDF 複製到專案 data/reports/ 統一管理,file_path 存相對路徑;抽取器可從檔名解析 period、source、report_type。python 或 python3 按環境選擇):# 建表(首次執行指令碼時會自動執行 schema)
python3 scripts/archive_item.py -c "測試第一條" -s "manual"
# 查詢
python3 scripts/query_items.py --list
python3 scripts/query_items.py --field "tags" --value "工作"
python3 scripts/query_items.py --stats
更多技能請訪問小蔥技能站7w4.net。
recall())| 使用者意圖 | 推薦主幹 | 推薦軟欄位 | 備註 |
|---|---|---|---|
| 個人知識庫 | id, created_at, source, content_type | title, tags, url, project | 碎片收集同此 |
| PDF/報告知識庫 | id, created_at, source, content_type, file_path | report_title, report_type, period_start, period_end | 需提取正文;中文檢索見「全文檢索策略」 |
| 政策資訊收集 | id, created_at, source, source_type | title, release_date, issuing_org, policy_type, url, policy_no, industry, subsidy_amount | 政府網站/新聞來源,文號常作查詢鍵 |
| 財務報表收集 | id, created_at, source, source_type | company, report_type, report_date, revenue, net_income, total_assets, roa, roe | 金額統一存「元」;report_type→category |
| 表單/問卷 | id, created_at, form_id, respondent_id | 題目 id → 答案 | 可加 form_version |
| 多源訊息聚合 | id, created_at, source, sender | data_type, items[], trend | 參考 agri-market-info |
| 埋點/事件 | id, created_at, event_name, user_id | properties JSON | 可加 event_version |
LIKE '%x%' 用於萬級資料 → 全表掃描,應評估或改用外部搜尋引擎flexible-database-design/
├── SKILL.md # 本檔案
├── README.md
├── references/
│ ├── schema_template.sql # 通用建表模板
│ ├── view_examples.sql # 業務檢視示例
│ └── fulltext_chinese.md # 可選:中文檢索實現示例(短詞拆分、recall 邏輯)
└── scripts/
├── flexible_db.py # 資料庫核心邏輯
├── archive_item.py # 歸檔 CLI(支援 --llm-extract、--backup)
├── query_items.py # 查詢 CLI(支援 --export)
├── manage_item.py # 管理 CLI(軟刪除、恢復、更新)
├── import_batch.py # 批次匯入
├── quick_validate.py # 快速驗證
└── extractors/ # 抽取器(可替換為 LLM 實現)
allowed-tools: - Bash - FileRead - FileWrite - Shell
作者 | Mars Yang 日期 | 2025-03-09
這是一個質量較高的資料庫設計 Skill,提供了清晰的設計思路和可直接使用的程式碼工具。文件寫得詳細,場景案例豐富,普通人也能理解。不過使用起來需要一定技術基礎,完整的落地流程還比較複雜,部分功能的細節處理還有最佳化空間。總體來說,對於需要構建靈活資料庫的使用者很有價值,但建議有一定程式設計經驗的人使用效果更好。