generate_typescript_interfaces
Generate TypeScript interfaces from PocketBase collections to enable type-safe database interactions in your applications.
Instructions
Generate TypeScript interfaces from PocketBase collections
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collections | No | Collection names to generate interfaces for (empty for all) | |
| options | No | Generation options |
Implementation Reference
- src/tools/handlers/generation.ts:135-217 (handler)Handler factory function that creates the tool handler for generating TypeScript interfaces from PocketBase collections. The core logic fetches collections, maps fields to TS types, handles relations, and generates interface code./** * Generate TypeScript interfaces from PocketBase collections */ export function createGenerateTypescriptInterfacesHandler(pb: PocketBase): ToolHandler { return async (args: GenerateTypescriptInterfacesArgs = {}) => { try { const { collections: targetCollections = [], options = {} } = args; const includeRelations = options.includeRelations ?? true; // Get all collections or specific ones const allCollections = await pb.collections.getFullList(); const collectionsToProcess = targetCollections.length > 0 ? allCollections.filter(c => targetCollections.includes(c.name)) : allCollections; const interfaces: string[] = []; // Generate base record interface interfaces.push("// Base record interface"); interfaces.push("export interface BaseRecord {"); interfaces.push(" id: string;"); interfaces.push(" created: string;"); interfaces.push(" updated: string;"); interfaces.push("}"); interfaces.push(""); // Generate interfaces for each collection for (const collection of collectionsToProcess) { const interfaceName = toPascalCase(collection.name); const fields = collection.fields || []; interfaces.push(`// ${collection.name} collection`); interfaces.push(`export interface ${interfaceName} extends BaseRecord {`); for (const field of fields) { if (field.system) continue; // Skip system fields const fieldType = mapPocketBaseTypeToTypeScript(field.type, field); const optional = !field.required ? "?" : ""; if (includeRelations && field.type === "relation") { const relatedCollection = field.options?.collectionId; if (relatedCollection) { const relatedCollectionName = allCollections.find( c => c.id === relatedCollection )?.name; if (relatedCollectionName) { const relatedInterface = toPascalCase(relatedCollectionName); const isMultiple = field.options?.maxSelect !== 1; interfaces.push( ` ${field.name}${optional}: ${isMultiple ? `${relatedInterface}[]` : relatedInterface};` ); continue; } } } interfaces.push(` ${field.name}${optional}: ${fieldType};`); } interfaces.push("}"); interfaces.push(""); } // Add utility types interfaces.push("// Utility types"); interfaces.push("export type RecordId = string;"); interfaces.push("export type RecordTimestamp = string;"); interfaces.push(""); // Add collection names type const collectionNames = collectionsToProcess.map(c => `"${c.name}"`).join(" | "); interfaces.push(`export type CollectionName = ${collectionNames};`); const result = interfaces.join("\n"); return createTextResponse(result); } catch (error: unknown) { throw handlePocketBaseError("generate TypeScript interfaces", error); } }; }
- Input schema validation for the generate_typescript_interfaces tool, defining expected arguments like collections and options.export const generateTypescriptInterfacesSchema = { type: "object", properties: { collections: { type: "array", description: "Collection names to generate interfaces for (empty for all)", items: { type: "string", }, }, options: { type: "object", description: "Generation options", properties: { includeRelations: { type: "boolean", description: "Whether to include relation types", }, }, }, }, };
- src/server.ts:212-216 (registration)Tool registration in the main server, specifying name, description, input schema, and handler.name: "generate_typescript_interfaces", description: "Generate TypeScript interfaces from PocketBase collections", inputSchema: generateTypescriptInterfacesSchema, handler: createGenerateTypescriptInterfacesHandler(pb), },
- src/types/index.ts:192-197 (schema)TypeScript interface defining the argument shape for the tool handler, matching the schema.export interface GenerateTypescriptInterfacesArgs { collections?: string[]; options?: { includeRelations?: boolean; }; }