pb-collections
它能做什么
这项技能帮你设置 PocketBase 应用的数据结构,包括集合、字段和关系。适合希望在 PocketBase 中获得清晰数据库设计的开发者。
适用场景
- ✓当你需要创建新集合,或为现有集合添加字段时。
- ✓当你需要用关系把两个集合连接起来时。
- ✓当你需要在基础集合、认证集合和视图集合之间做选择的时候。
技能链接
https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/pocketbase/pb-collections/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我的博客设计 PocketBase 集合,包括用户、帖子和评论,并正确地把它们关联起来。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会为每类数据选择合适的集合类型和字段类型,设置关系,并说明默认值以及删除关联记录时的级联行为。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PocketBase Collection & Schema Design
Collection Types
Base Collection
Standard data collection. System fields: id, created, updated.
Auth Collection
Extends base with authentication. Additional system fields: email, emailVisibility, verified, password, tokenKey.
Cannot delete system fields. Can disable email/password auth in collection options.
View Collection
Read-only, backed by a SQL SELECT query. No create/update/delete. Fields are auto-detected from the query. Useful for aggregations, joins, and computed views.
-- Example: view collection query
SELECT p.id, p.title, COUNT(c.id) as comments_count
FROM posts p LEFT JOIN comments c ON c.post = p.id
GROUP BY p.id
View collections support API rules (list/view only) and can be used in relations.
Field Types
| Type | Go type | Zero default | Notes |
|---|---|---|---|
text |
string |
"" |
min/max length, regex pattern |
editor |
string |
"" |
Rich text (sanitized HTML) |
number |
float64 |
0 |
min/max, noDecimal option |
bool |
bool |
false |
|
email |
string |
"" |
Auto-validated format |
url |
string |
"" |
Auto-validated format |
date |
string |
"" |
ISO 8601 (2024-01-01 00:00:00.000Z) |
select |
string/[]string |
""/[] |
values list, maxSelect |
file |
string/[]string |
""/[] |
maxSelect, maxSize, mimeTypes |
relation |
string/[]string |
""/[] |
collectionId, cascadeDelete, maxSelect |
json |
any |
null |
Only type that can be null! maxSize |
autodate |
string |
auto | onCreate/onUpdate modifiers |
password |
string |
"" |
Stored hashed, never returned in API |
Critical: all types default to their zero value, NOT null. Only json fields can be null.
Field Modifiers
Use in collection schema definitions:
required— field cannot be empty/zerounique— unique constraint (composite via unique indexes)presentable— included in relation displayhidden— excluded from API responses unless explicitly requested:autogenerate— fortextfields: auto-generate value (e.g., slug from other field)
Relation Patterns
One-to-many
posts.author -> users (maxSelect: 1)
Each post has one author. Query posts by author: author = "USER_ID".
Many-to-many
posts.tags -> tags (maxSelect: 0, meaning unlimited)
Multi-select relation. Filter: tags ?= "TAG_ID" (contains).
Back-relations
No explicit back-relation field needed. Use @collection.posts.author in API rules or expand from either side:
GET /api/collections/users/records/USER_ID?expand=posts_via_author
Cascade Delete
Set cascadeDelete: true on the relation field. When the referenced record is deleted, all records pointing to it are also deleted. Default is false (sets to empty string).
Self-referencing
A collection can reference itself:
categories.parent -> categories (maxSelect: 1)
Indexes
- Created in the collection settings (not field-level)
- Format:
CREATE [UNIQUE] INDEX idx_name ON collection (field1, field2) - Unique indexes enforce composite uniqueness
- Partial indexes:
CREATE INDEX ... WHERE condition - Indexes on relation fields improve join performance
Auth Collection Specifics
OAuth2
Enable per-provider in collection settings. Each provider needs client ID + secret. PocketBase handles the full OAuth2 flow.
OTP (One-Time Password)
Enable in auth collection settings. Sends code via email. Configure otp.enabled, otp.duration, otp.length.
MFA (Multi-Factor Authentication)
Enable in auth collection settings. Requires a second factor after primary auth. mfa.enabled, mfa.duration, mfa.rule (filter to determine which users need MFA).
Password Auth
Enabled by default. Can customize minPasswordLength. Can disable entirely if using only OAuth2/OTP.
Auth Options
authToken.duration— token lifetime (seconds)passwordAuth.enabled— toggle email/passwordpasswordAuth.identityFields— fields used for login (default:email; can addusername)oauth2.enabled— toggle OAuth2otp.enabled— toggle OTP
Best Practices
- Prefer
selectoverboolwhen there might be more than 2 states in the future - Use
relationnottextfor foreign keys — you get cascade, expand, and type safety jsonfields are schemaless —
常见问题
什么是视图集合?
它是基于 SQL 查询构建的只读集合,适合联接和汇总。
什么是认证集合?
它是在基础集合上增加登录字段(例如邮箱、密码和验证状态)的集合。
它能避免哪些错误?
这项技能的设计目的是防止错误的字段类型,并说明零值默认的行为。
详情
作者的原始描述
Collection and schema design for PocketBase. Use when creating collections, designing schemas, adding fields, setting up relations, or choosing between base/auth/view collection types. Prevents wrong field types, documents zero-default behavior, and covers relation cascading.
热度
- GitHub 星标
- 33k
- 排名
- #8871
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
owasp-security
33k教你的智能体掌握面向网页应用、API、移动端、容器和 AI 智能体的 OWASP 安全指南,以便发现常见弱点并提出修复建议。
agent-management
33k让你的智能体使用 AI Maestro 命令行工具创建、列出、休眠、唤醒、重命名和删除 AI 智能体,管理每个智能体的生命周期。
agent-messaging
33k让你的智能体使用智能体消息协议,在 AI 智能体之间发送、接收和回复带签名的消息,大部分在你自己的电脑上完成。
docs-search
33k让你的智能体搜索自动生成的代码文档,查找函数签名、类定义和注释,从而在修改代码之前核对真实的 API。