create-collection
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePocketBase Collection Management
PocketBase 集合管理
Use this skill whenever you need to modify the database schema (create/update/delete collections).
当你需要修改数据库架构(创建/更新/删除集合)时,均可使用本技能。
Why This Approach
为什么选择这种方式
PocketBase automatically generates migration files when collections are created/modified via the SDK. This ensures:
- Correct internal IDs
- Proper migration format
- Automatic rollback functions
- No manual SQL errors
当通过SDK创建或修改集合时,PocketBase会自动生成迁移文件。这可以确保:
- 正确的内部ID
- 规范的迁移格式
- 自动回滚功能
- 避免手动编写SQL出错
Instructions
操作步骤
1. Get the PocketBase URL
1. 获取PocketBase URL
Find the container IP:
bash
docker network inspect shipyard-pocketbase-template_default --format '{{range .Containers}}{{.IPv4Address}}{{end}}'查找容器IP:
bash
docker network inspect shipyard-pocketbase-template_default --format '{{range .Containers}}{{.IPv4Address}}{{end}}'2. Write a Temporary Script
2. 编写临时脚本
Create a temp JS file (e.g., ):
temp-collection.jsjavascript
import PocketBase from 'pocketbase'
const pb = new PocketBase('http://<CONTAINER_IP>:8090')
await pb.collection('_superusers').authWithPassword('admin@test.local', 'testtest123')
// CREATE a collection
await pb.collections.create({
name: 'my_collection',
type: 'base', // or 'auth' for user collections
fields: [
{ name: 'title', type: 'text', required: true },
{ name: 'completed', type: 'bool' },
{ name: 'user_id', type: 'text', required: true },
],
listRule: '@request.auth.id != ""',
viewRule: '@request.auth.id != ""',
createRule: '@request.auth.id != ""',
updateRule: 'user_id = @request.auth.id',
deleteRule: 'user_id = @request.auth.id',
})
// UPDATE a collection
// const collection = await pb.collections.getOne('my_collection')
// await pb.collections.update(collection.id, { name: 'new_name' })
// DELETE a collection
// await pb.collections.delete('my_collection')
console.log('Done!')创建一个临时JS文件(例如):
temp-collection.jsjavascript
import PocketBase from 'pocketbase'
const pb = new PocketBase('http://<CONTAINER_IP>:8090')
await pb.collection('_superusers').authWithPassword('admin@test.local', 'testtest123')
// 创建集合
await pb.collections.create({
name: 'my_collection',
type: 'base', // 若为用户集合则使用'auth'
fields: [
{ name: 'title', type: 'text', required: true },
{ name: 'completed', type: 'bool' },
{ name: 'user_id', type: 'text', required: true },
],
listRule: '@request.auth.id != ""',
viewRule: '@request.auth.id != ""',
createRule: '@request.auth.id != ""',
updateRule: 'user_id = @request.auth.id',
deleteRule: 'user_id = @request.auth.id',
})
// 更新集合
// const collection = await pb.collections.getOne('my_collection')
// await pb.collections.update(collection.id, { name: 'new_name' })
// 删除集合
// await pb.collections.delete('my_collection')
console.log('Done!')3. Run the Script
3. 运行脚本
bash
node temp-collection.jsbash
node temp-collection.js4. Verify Migration Was Created
4. 验证迁移文件已生成
bash
ls -la pocketbase/pb_migrations/You should see a new file with the timestamp and collection name.
.jsbash
ls -la pocketbase/pb_migrations/你应该会看到一个带有时间戳和集合名称的新文件。
.js5. Delete the Temp Script
5. 删除临时脚本
bash
rm temp-collection.jsbash
rm temp-collection.jsField Types
字段类型
Common field types for :
pb.collections.create()- - String field
text - - Boolean
bool - - Numeric
number - - Email validation
email - - URL validation
url - - Date/datetime
date - - Enum (add
select)values: ['a', 'b'] - - Foreign key (add
relation)collectionId: 'xxx' - - File upload
file - - JSON data
json
pb.collections.create()- - 字符串字段
text - - 布尔值
bool - - 数值
number - - 邮箱验证
email - - URL验证
url - - 日期/日期时间
date - - 枚举类型(需添加
select)values: ['a', 'b'] - - 外键(需添加
relation)collectionId: 'xxx' - - 文件上传
file - - JSON数据
json
Rules
访问规则
Access rules use PocketBase filter syntax:
- - Superusers only
null - (empty string) - Anyone
'' - - Authenticated users
'@request.auth.id != ""' - - Owner only
'user_id = @request.auth.id'
访问规则使用PocketBase过滤语法:
- - 仅超级管理员可访问
null - (空字符串)- 所有人可访问
'' - - 仅已认证用户可访问
'@request.auth.id != ""' - - 仅集合所有者可访问
'user_id = @request.auth.id'