generate_pb_schema
Convert TypeScript interfaces or database diagrams into PocketBase schema definitions to streamline database setup and ensure structural consistency.
Instructions
Generate a PocketBase schema based on TypeScript interfaces or database diagram
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| options | No | Generation options | |
| sourceCode | Yes | TypeScript interface or database diagram to convert to PocketBase schema |
Implementation Reference
- src/tools/handlers/generation.ts:14-133 (handler)The main handler function createGeneratePbSchemaHandler that analyzes TypeScript code, generates PocketBase collections from interfaces, adds auth and timestamps as needed, and returns the schema as JSON.export function createGeneratePbSchemaHandler(pb: PocketBase): ToolHandler { return async (args: GeneratePbSchemaArgs) => { try { const { sourceCode, options = {} } = args; const includeAuth = options.includeAuthentication ?? true; const includeTimestamps = options.includeTimestamps ?? true; // Analyze TypeScript source code const interfaces = analyzeTypeScriptForSchema(sourceCode, options); if (interfaces.length === 0) { return createTextResponse("No TypeScript interfaces found in the provided source code."); } const collections = []; // Generate collections from interfaces for (const iface of interfaces) { const fields = []; // Add standard fields if requested if (includeTimestamps) { fields.push( { name: "created", type: "autodate", required: false, system: true, onCreate: true, onUpdate: false, }, { name: "updated", type: "autodate", required: false, system: true, onCreate: true, onUpdate: true, } ); } // Convert interface properties to PocketBase fields for (const prop of iface.properties) { const field: any = { name: prop.name, type: mapTypeScriptToPocketBase(prop.type), required: !prop.optional, }; // Add type-specific options if (field.type === "text" && prop.type.includes("email")) { field.type = "email"; } else if (field.type === "text" && prop.type.includes("url")) { field.type = "url"; } else if (field.type === "text" && prop.type.includes("Date")) { field.type = "date"; } fields.push(field); } const collection = { name: iface.name.toLowerCase(), type: "base" as const, fields, listRule: "", viewRule: "", createRule: "", updateRule: "", deleteRule: "", }; collections.push(collection); } // Add authentication collection if requested if (includeAuth) { const authCollection = { name: "users", type: "auth" as const, fields: [ { name: "name", type: "text", required: false, }, { name: "avatar", type: "file", required: false, options: { maxSelect: 1, maxSize: 5242880, mimeTypes: ["image/jpeg", "image/png", "image/svg+xml", "image/gif"], }, }, ], listRule: "id = @request.auth.id", viewRule: "id = @request.auth.id", createRule: "", updateRule: "id = @request.auth.id", deleteRule: "id = @request.auth.id", }; collections.unshift(authCollection); } const schema = { collections, generatedAt: new Date().toISOString(), source: "TypeScript interfaces", }; return createJsonResponse(schema); } catch (error: unknown) { throw handlePocketBaseError("generate PocketBase schema", error); } }; }
- src/tools/schemas/generation.ts:5-28 (schema)Input schema validation for the generate_pb_schema tool, defining sourceCode as required and options for authentication and timestamps.export const generatePbSchemaSchema = { type: "object", properties: { sourceCode: { type: "string", description: "TypeScript interface or database diagram to convert to PocketBase schema", }, options: { type: "object", description: "Generation options", properties: { includeAuthentication: { type: "boolean", description: "Whether to include authentication related collections", }, includeTimestamps: { type: "boolean", description: "Whether to include created/updated timestamps", }, }, }, }, required: ["sourceCode"], };
- src/server.ts:206-210 (registration)Tool registration in the MCP server, specifying name, description, input schema, and handler.name: "generate_pb_schema", description: "Generate a PocketBase schema based on TypeScript interfaces or database diagram", inputSchema: generatePbSchemaSchema, handler: createGeneratePbSchemaHandler(pb), },
- src/types/index.ts:184-190 (schema)TypeScript interface defining the arguments for the generate_pb_schema handler.export interface GeneratePbSchemaArgs { sourceCode: string; options?: { includeAuthentication?: boolean; includeTimestamps?: boolean; }; }
- Helper function to map TypeScript types to corresponding PocketBase field types.function mapTypeScriptToPocketBase(tsType: string): string { const type = tsType.toLowerCase(); if (type.includes("string")) return "text"; if (type.includes("number")) return "number"; if (type.includes("boolean")) return "bool"; if (type.includes("date")) return "date"; if (type.includes("email")) return "email"; if (type.includes("url")) return "url"; if (type.includes("[]")) return "json"; if (type.includes("object") || type.includes("{")) return "json"; return "text"; // Default fallback }