migrate_collection
Modify collection structure by adding, removing, or updating fields, including data transformations and access rules for PocketBase databases.
Instructions
Add, remove, or modify fields from a collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| createRule | No | Optional new rule for creating records | |
| dataTransforms | No | Field transformation mappings for converting old field values to new ones | |
| deleteRule | No | Optional new rule for deleting records | |
| fields | Yes | New collection fields configuration | |
| listRule | No | Optional new rule for listing records | |
| name | No | Optional new collection name if you want to rename the collection | |
| updateRule | No | Optional new rule for updating records | |
| viewRule | No | Optional new rule for viewing records |
Implementation Reference
- src/tools/handlers/migration.ts:16-62 (handler)The createMigrateCollectionHandler function returns the core async tool handler that updates the collection schema (fields, rules, name) and optionally transforms existing data fields.export function createMigrateCollectionHandler(pb: PocketBase): ToolHandler { return async (args: MigrateCollectionArgs) => { try { const { collection, fields, dataTransforms = {}, name, ...rules } = args; // Prepare update data const updateData: any = { fields, ...rules, }; if (name) updateData.name = name; // Update collection schema const updatedCollection = await pb.collections.update(collection, updateData); // If there are data transforms, apply them if (Object.keys(dataTransforms).length > 0) { const records = await pb.collection(collection).getFullList(); for (const record of records) { const updates: any = {}; let hasUpdates = false; for (const [oldField, newField] of Object.entries(dataTransforms)) { if (record[oldField] !== undefined) { updates[newField] = record[oldField]; hasUpdates = true; } } if (hasUpdates) { await pb.collection(collection).update(record.id, updates); } } } return createJsonResponse({ success: true, collection: updatedCollection, message: `Collection '${collection}' migrated successfully`, }); } catch (error: unknown) { throw handlePocketBaseError("migrate collection", error); } }; }
- src/tools/schemas/migration.ts:5-82 (schema)JSON schema defining the input parameters for the migrate_collection tool, including collection name, new fields configuration, data transforms, and optional rules.export const migrateCollectionSchema = { type: "object", properties: { collection: { type: "string", description: "Collection name", }, fields: { type: "array", description: "New collection fields configuration", items: { type: "object", properties: { name: { type: "string", description: "Field name", }, type: { type: "string", description: "Field type", enum: [ "text", "number", "bool", "email", "url", "date", "select", "relation", "file", "json", "editor", "autodate", ], }, required: { type: "boolean", description: "Whether the field is required", }, options: { type: "object", description: "Field-specific options", }, }, required: ["name", "type"], }, }, dataTransforms: { type: "object", description: "Field transformation mappings for converting old field values to new ones", }, name: { type: "string", description: "Optional new collection name if you want to rename the collection", }, listRule: { type: "string", description: "Optional new rule for listing records", }, viewRule: { type: "string", description: "Optional new rule for viewing records", }, createRule: { type: "string", description: "Optional new rule for creating records", }, updateRule: { type: "string", description: "Optional new rule for updating records", }, deleteRule: { type: "string", description: "Optional new rule for deleting records", }, }, required: ["collection", "fields"], };
- src/server.ts:179-184 (registration)Tool registration object in the MCP server configuration array, linking the name, description, schema, and handler factory.{ name: "migrate_collection", description: "Add, remove, or modify fields from a collection", inputSchema: migrateCollectionSchema, handler: createMigrateCollectionHandler(pb), },