create_collection_migration
Generate a migration file to define and create a new PocketBase collection, including its schema, fields, and rules, for structured database management.
Instructions
Create a migration file specifically for creating a new PocketBase collection.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionDefinition | Yes | The full schema definition for the new collection (including name, id, fields, rules, etc.). | |
| description | No | Optional description override for the filename. |
Implementation Reference
- src/tools/migration-tools.ts:221-232 (handler)The handler function for the 'create_collection_migration' tool. Validates the input arguments and calls the underlying createCollectionMigration helper to generate the migration file.async function handleCreateCollectionMigration(args: CreateCollectionMigrationArgs): Promise<ToolResult> { if (!args.collectionDefinition) { throw invalidParamsError("Missing required argument: collectionDefinition"); } if (!args.collectionDefinition.name || !args.collectionDefinition.id) { throw invalidParamsError("collectionDefinition must include 'name' and 'id' properties."); } const filePath = await createCollectionMigration(args.collectionDefinition, args.description); return { content: [{ type: 'text', text: `Created collection migration file: ${filePath}` }], }; }
- src/tools/migration-tools.ts:78-94 (schema)The tool registration object containing the schema (inputSchema) for validating inputs to the 'create_collection_migration' tool.{ name: 'create_collection_migration', description: 'Create a migration file specifically for creating a new PocketBase collection.', inputSchema: { type: 'object', properties: { description: { type: 'string', description: 'Optional description override for the filename.' }, collectionDefinition: { type: 'object', description: 'The full schema definition for the new collection (including name, id, fields, rules, etc.).', additionalProperties: true, // Allow any properties for the schema required: ['name', 'id'] // Enforce required schema properties }, }, required: ['collectionDefinition'], }, },
- src/migrations/index.ts:49-76 (helper)The core helper function that generates and writes the migration file for creating a new collection based on the provided definition.export async function createCollectionMigration(collectionDefinition: Record<string, any>, description?: string): Promise<string> { const timestamp = helpers.generateTimestamp(); const collectionName = collectionDefinition.name; if (!collectionName || typeof collectionName !== 'string') { throw new Error("Collection definition must have a 'name' property."); } const collectionId = collectionDefinition.id; if (!collectionId || typeof collectionId !== 'string') { throw new Error("Collection definition must have an 'id' property."); } const desc = description || `create_${collectionName}_collection`; const sanitizedDescription = desc .toLowerCase() .replace(/[^a-z0-9_]+/g, '_') .replace(/^_+|_+$/g, ''); const filename = `${timestamp}_${sanitizedDescription}.js`; // Generate specific up/down queries const upQuery = helpers.generateCreateCollectionQuery(collectionDefinition); const downQuery = helpers.generateDeleteCollectionQuery(collectionId); // Use ID for down query const content = helpers.generateMigrationTemplate(upQuery, downQuery); return helpers.createMigrationFile(filename, content); }
- src/tools/index.ts:51-52 (registration)The dispatch logic in the main tool handler that routes 'create_collection_migration' calls to the migration tool handler.} else if (name === 'create_migration' || name === 'create_collection_migration' || name === 'add_field_migration' || name === 'list_migrations') { return handleMigrationToolCall(name, toolArgs, pb);
- src/tools/index.ts:15-25 (registration)The main tool registration function that includes migration tools (via listMigrationTools()), making 'create_collection_migration' available.export function registerTools(): { tools: ToolInfo[] } { // Use ToolInfo[] const tools: ToolInfo[] = [ // Use ToolInfo[] ...listRecordTools(), ...listCollectionTools(), ...listFileTools(), ...listMigrationTools(), // Uncommented ...listLogTools(), // Add log tools ...listCronTools(), // Add cron tools ]; return { tools }; }