create-collection

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PocketBase 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.js
):
javascript
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.js
):
javascript
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.js
bash
node temp-collection.js

4. Verify Migration Was Created

4. 验证迁移文件已生成

bash
ls -la pocketbase/pb_migrations/
You should see a new
.js
file with the timestamp and collection name.
bash
ls -la pocketbase/pb_migrations/
你应该会看到一个带有时间戳和集合名称的新
.js
文件。

5. Delete the Temp Script

5. 删除临时脚本

bash
rm temp-collection.js
bash
rm temp-collection.js

Field Types

字段类型

Common field types for
pb.collections.create()
:
  • text
    - String field
  • bool
    - Boolean
  • number
    - Numeric
  • email
    - Email validation
  • url
    - URL validation
  • date
    - Date/datetime
  • select
    - Enum (add
    values: ['a', 'b']
    )
  • relation
    - Foreign key (add
    collectionId: 'xxx'
    )
  • file
    - File upload
  • json
    - JSON data
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:
  • null
    - Superusers only
  • ''
    (empty string) - Anyone
  • '@request.auth.id != ""'
    - Authenticated users
  • 'user_id = @request.auth.id'
    - Owner only
访问规则使用PocketBase过滤语法:
  • null
    - 仅超级管理员可访问
  • ''
    (空字符串)- 所有人可访问
  • '@request.auth.id != ""'
    - 仅已认证用户可访问
  • 'user_id = @request.auth.id'
    - 仅集合所有者可访问