UNPKG

@docyrus/tanstack-db-generator

Version:

Code generator utilities for TanStack Query / Database integration with Docyrus API

87 lines (85 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateCollections = generateCollections; const fs_1 = require("fs"); const path_1 = require("path"); async function generateCollections(spec, dataSources, outputDir) { // Generate individual collection files for (const dataSource of dataSources) { const collectionContent = generateCollectionFile(dataSource); const fileName = `${dataSource.appName}-${dataSource.dataSourceName}.collection.ts`; (0, fs_1.writeFileSync)((0, path_1.join)(outputDir, fileName), collectionContent); } // Generate index file const indexContent = generateCollectionsIndex(dataSources); (0, fs_1.writeFileSync)((0, path_1.join)(outputDir, 'index.ts'), indexContent); } function generateCollectionFile(dataSource) { const { appName, dataSourceName, entityName, endpoints } = dataSource; const collectionName = `${appName}${toPascalCase(dataSourceName)}Collection`; const methods = []; if (endpoints.list) { methods.push(generateListMethod(endpoints.list, entityName)); } if (endpoints.get) { methods.push(generateGetMethod(endpoints.get, entityName)); } if (endpoints.create) { methods.push(generateCreateMethod(endpoints.create, entityName)); } if (endpoints.update) { methods.push(generateUpdateMethod(endpoints.update, entityName)); } if (endpoints.delete) { methods.push(generateDeleteMethod(endpoints.delete)); } if (endpoints.deleteMany) { methods.push(generateDeleteManyMethod(endpoints.deleteMany)); } return `// Generated collection for ${appName}/${dataSourceName} import { apiClient } from '../../lib/api-client'; import type { ${entityName} } from '../types'; export const ${collectionName} = { ${methods.join(',\n\n')} }; `; } function generateListMethod(endpoint, entityName) { return ` list: (params?: { limit?: number; offset?: number; columns?: Array<string>; filterQuery?: any }): Promise<Array<${entityName}>> => apiClient.get('${endpoint.path}', params)`; } function generateGetMethod(endpoint, entityName) { return ` get: (recordId: string, params?: { columns?: Array<string> }): Promise<${entityName}> => apiClient.get('${endpoint.path}'.replace('{recordId}', recordId), params)`; } function generateCreateMethod(endpoint, entityName) { return ` create: (data: { data: any }): Promise<${entityName}> => apiClient.post('${endpoint.path}', data)`; } function generateUpdateMethod(endpoint, entityName) { return ` update: (recordId: string, data: { data: any }): Promise<${entityName}> => apiClient.patch('${endpoint.path}'.replace('{recordId}', recordId), data)`; } function generateDeleteMethod(endpoint) { return ` delete: (recordId: string): Promise<void> => apiClient.delete('${endpoint.path}'.replace('{recordId}', recordId))`; } function generateDeleteManyMethod(endpoint) { return ` deleteMany: (data: { recordIds: Array<string> }): Promise<void> => apiClient.delete('${endpoint.path}', data)`; } function generateCollectionsIndex(dataSources) { const exports = dataSources.map(ds => { const collectionName = `${ds.appName}${toPascalCase(ds.dataSourceName)}Collection`; const fileName = `${ds.appName}-${ds.dataSourceName}.collection`; return `export { ${collectionName} } from './${fileName}';`; }); return `// Generated collections index ${exports.join('\n')} `; } function toPascalCase(str) { return str.split(/[_-]/) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(''); }