{"meta":{"title":"同步文档以适应代码更改","intro":"副驾驶聊天 可帮助使代码文档保持最新状态。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat 指南"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook/document-code","title":"文档编码"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook/document-code/sync-documentation","title":"同步文档"}],"documentType":"article"},"body":"# 同步文档以适应代码更改\n\n副驾驶聊天 可帮助使代码文档保持最新状态。\n\n随着对代码进行更改，使文档保持最新可能很困难。 然而，良好的文档对于维护代码库和确保开发人员能够有效地使用代码至关重要。 副驾驶聊天 可帮助更新现有代码文档。\n\n## 示例方案\n\n假设有一个 TypeScript 函数，该函数按类别名称检索产品，但文档已过期。\n\n```typescript id=fetch-products-by-category\n/**\n * Retrieves all products belonging to a specific category.\n * * @param categoryId - The unique identifier of the product category\n * @returns Promise that resolves to an array of Product objects\n * * @example\n * const products = await getByCategoryName(5);\n * // Returns: [{id: 1, name: \"Product 1\", categoryId: 5}, ...]\n * * @throws {PrismaClientKnownRequestError} If database query fails\n * @see {@link https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany}\n */\nasync getByCategoryName(categoryId: number, page = 1, pageSize = 10): Promise<Products[]> {\n  return await prisma.products.findMany({\n    where: { categoryId },\n    skip: (page - 1) * pageSize,\n    take: pageSize,\n    include: {\n      reviews: {\n        orderBy: { date: 'desc' },\n        take: 5,\n      },\n    },\n  });\n}\n```\n\n## 示例提示\n\n可以让 副驾驶聊天 根据当前代码更新文档。\n\n```copilot copy prompt ref=fetch-products-by-category\nUpdate the existing documentation for the getByCategoryName function to reflect the current implementation.\n```\n\n## 示例响应\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\nCopilot 说明它将为 `getByCategoryName` 函数创建更新的文档。 然后，它提供更新的文档，其中现在包括 `page` 和 `pageSize` 参数，以及可能引发的 `PrismaClientKnownRequestError` 错误。\n\n例如：\n\n```typescript\n/**\n * Retrieves all products belonging to a specific category with pagination and recent reviews.\n * * @param categoryId - The unique identifier of the product category\n * @param page - The page number for pagination (default: 1)\n * @param pageSize - Number of products per page (default: 10)\n * @returns Promise that resolves to an array of Product objects with their 5 most recent reviews\n * * @example\n * const products = await getByCategoryName(5, 2, 20);\n * // Returns: [{id: 1, name: \"Product 1\", categoryId: 5, reviews: [...5 recent reviews]}, ...]\n * * @throws {PrismaClientKnownRequestError} If database query fails\n * @see {@link https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany}\n */\n```\n\n## 其他阅读材料\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}