dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
54 lines (53 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addPutEntityItemTool = void 0;
const zod_1 = require("zod");
const index_js_1 = require("../../../../entity/actions/put/index.js");
const options_js_1 = require("../../../../entity/actions/put/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 defaultPutOptionsSchema = 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.putItemCommandReturnValuesOptions),
tableName: zod_1.z.string()
})
.partial()
.default({});
const addPutEntityItemTool = (server, entity, options) => {
const { entityName, table } = entity;
const { dbTableKey } = options;
const tableName = table.tableName !== undefined ? table.getName() : undefined;
const hasTableName = tableName !== undefined;
const putOptionsSchema = hasTableName
? defaultPutOptionsSchema
: defaultPutOptionsSchema.removeDefault().required({ tableName: true });
const putToolName = `ddb-tb_put-${entityName}-item-in-${dbTableKey}-table`.substring(0, 64);
let putToolDescription = `Put a '${entityName}' Item in the ${tableName !== null && tableName !== void 0 ? tableName : dbTableKey} Table.`;
const { title, description } = entity.meta;
if (title !== undefined) {
putToolDescription += `\n# ${title}`;
}
if (description !== undefined) {
putToolDescription += `\n\n${description}.`;
}
server.tool(putToolName, putToolDescription, {
item: new index_js_2.ZodSchemer(entity.schema).parser({ transform: false }),
options: putOptionsSchema
}, { title: putToolDescription, readOnlyHint: false, destructiveHint: false }, async ({ item, options }) => {
try {
const Response = await new index_js_1.PutItemCommand(entity, item, options).send();
return {
content: [{ type: 'text', text: JSON.stringify(Response) }]
};
}
catch (error) {
return {
content: [{ type: 'text', text: String(error), isError: true }]
};
}
});
};
exports.addPutEntityItemTool = addPutEntityItemTool;