update
Modify specific records in a database collection using defined criteria and update data, ensuring accurate and targeted changes through a standardized interface.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | 集合名称 | |
| data | Yes | 要更新的数据 (object) | |
| where | Yes | 查询条件 (JQL格式) |
Implementation Reference
- database.js:285-298 (handler)The handler function registered for the 'update' MCP tool. It destructures the input parameters (collection, where, data), calls the core updateDatabase function, and formats the response as MCP content or error.async function handleUpdateTool(params, dbUrl) { const { collection, where, data } = params; try { const result = await updateDatabase(collection, where, data, dbUrl); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], }; } catch (error) { return { content: [{ type: 'text', text: error.message }], isError: true, }; }
- database.js:136-172 (helper)Core helper function that performs the actual HTTP POST request to the uniCloud database service with 'update' action, handling timeout and error cases.async function updateDatabase(collection, where, data, dbUrl = DEFAULT_DB_URL) { try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT); // 使用传入的dbUrl或默认URL const targetUrl = dbUrl || DEFAULT_DB_URL; // console.log('更新操作使用URL:', targetUrl); const response = await fetch(targetUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ operation: { collection, action: 'update', where, data, }, }), signal: controller.signal, }); clearTimeout(timeoutId); const result = await response.json(); if (result.code !== 0) { throw new Error(result.msg || '更新失败'); } return result.data; } catch (error) { if (error.name === 'AbortError') { throw new Error(`更新超时: 请求超过${REQUEST_TIMEOUT}毫秒`); } throw new Error(`更新错误: ${error.message}`); }
- database.js:403-410 (schema)Tool definition including input schema using Zod for validation: collection (string), where (record), data (record). This is passed to the MCP server registration.updateTool: { name: 'update', params: { collection: z.string().describe('集合名称'), where: z.record(z.any()).describe('查询条件 (JQL格式)'), data: z.record(z.any()).describe('要更新的数据 (object)'), }, handler: (params) => handleUpdateTool(params, dbServiceUrl),
- index.js:46-50 (registration)Registration of the 'update' tool on the MCP server using the name, params schema, and handler from database.js getToolDefinitions.server.tool( tools.updateTool.name, tools.updateTool.params, tools.updateTool.handler );