dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
60 lines (59 loc) • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addGetEntityItemTool = void 0;
const zod_1 = require("zod");
const index_js_1 = require("../../../../entity/actions/get/index.js");
const capacity_js_1 = require("../../../../options/capacity.js");
const index_js_2 = require("../../../../schema/actions/zodSchemer/index.js");
const defaultGetOptionsSchema = zod_1.z
.object({
capacity: zod_1.z.enum(capacity_js_1.capacityOptions),
consistent: zod_1.z.boolean(),
attributes: zod_1.z.array(zod_1.z.string()).min(1),
tableName: zod_1.z.string()
})
.partial()
.default({});
const addGetEntityItemTool = (server, entity, options) => {
const { entityName, table } = entity;
const { dbTableKey } = options;
const tableName = table.tableName !== undefined ? table.getName() : undefined;
const hasTableName = tableName !== undefined;
const getOptionsSchema = hasTableName
? defaultGetOptionsSchema
: defaultGetOptionsSchema.removeDefault().required({ tableName: true });
const getToolName = `ddb-tb_get-${entityName}-item-from-${dbTableKey}-table`;
let getToolDescription = `Get a '${entityName}' Item from the ${tableName !== null && tableName !== void 0 ? tableName : dbTableKey} Table.`;
const { title, description } = entity.meta;
if (title !== undefined) {
getToolDescription += `\n# ${title}`;
}
if (description !== undefined) {
getToolDescription += `\n\n${description}.`;
}
server.tool(getToolName, getToolDescription, {
key: new index_js_2.ZodSchemer(entity.schema).parser({
mode: 'key',
transform: false
}),
options: getOptionsSchema
}, { title: getToolDescription, readOnlyHint: true }, async ({ key, options }) => {
try {
const Response = await new index_js_1.GetItemCommand(entity, key, options).send();
if (Response.Item === undefined) {
return {
content: [{ type: 'text', text: 'Unable to find item', isError: true }]
};
}
return {
content: [{ type: 'text', text: JSON.stringify(Response) }]
};
}
catch (error) {
return {
content: [{ type: 'text', text: String(error), isError: true }]
};
}
});
};
exports.addGetEntityItemTool = addGetEntityItemTool;