UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

57 lines (56 loc) 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addDeleteEntityItemTool = void 0; const zod_1 = require("zod"); const index_js_1 = require("../../../../entity/actions/delete/index.js"); const options_js_1 = require("../../../../entity/actions/delete/options.js"); const capacity_js_1 = require("../../../../options/capacity.js"); const metrics_js_1 = require("../../../../options/metrics.js"); const index_js_2 = require("../../../../schema/actions/zodSchemer/index.js"); const defaultDeleteOptionsSchema = zod_1.z .object({ capacity: zod_1.z.enum(capacity_js_1.capacityOptions), metrics: zod_1.z.enum(metrics_js_1.metricsOptions), returnValues: zod_1.z.enum(options_js_1.deleteItemCommandReturnValuesOptions), tableName: zod_1.z.string() }) .partial() .default({}); const addDeleteEntityItemTool = (server, entity, options) => { const { entityName, table } = entity; const { dbTableKey } = options; const tableName = table.tableName !== undefined ? table.getName() : undefined; const hasTableName = tableName !== undefined; const deleteOptionsSchema = hasTableName ? defaultDeleteOptionsSchema : defaultDeleteOptionsSchema.removeDefault().required({ tableName: true }); const deleteToolName = `ddb-tb_delete-${entityName}-item-from-${dbTableKey}-table`.substring(0, 64); let deleteToolDescription = `Delete a '${entityName}' Item from the ${tableName !== null && tableName !== void 0 ? tableName : dbTableKey} Table.`; const { title, description } = entity.meta; if (title !== undefined) { deleteToolDescription += `\n# ${title}`; } if (description !== undefined) { deleteToolDescription += `\n\n${description}.`; } server.tool(deleteToolName, deleteToolDescription, { key: new index_js_2.ZodSchemer(entity.schema).parser({ mode: 'key', transform: false }), options: deleteOptionsSchema }, { title: deleteToolDescription, readOnlyHint: false, destructiveHint: true }, async ({ key, options }) => { try { const Response = await new index_js_1.DeleteItemCommand(entity, key, options).send(); return { content: [{ type: 'text', text: JSON.stringify(Response) }] }; } catch (error) { return { content: [{ type: 'text', text: String(error), isError: true }] }; } }); }; exports.addDeleteEntityItemTool = addDeleteEntityItemTool;