dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
91 lines (90 loc) • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAccessPatternTool = void 0;
const zod_1 = require("zod");
const constants_js_1 = require("../../../entity/actions/accessPattern/constants.js");
const index_js_1 = require("../../../entity/actions/accessPattern/index.js");
const capacity_js_1 = require("../../../options/capacity.js");
const select_js_1 = require("../../../options/select.js");
const index_js_2 = require("../../../schema/actions/zodSchemer/index.js");
const constants_js_2 = require("../../../table/actions/accessPattern/constants.js");
const index_js_3 = require("../../../table/actions/accessPattern/index.js");
const index_js_4 = require("../../../table/index.js");
const defaultQueryOptionsSchema = zod_1.z
.object({
capacity: zod_1.z.enum(capacity_js_1.capacityOptions),
exclusiveStartKey: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
limit: zod_1.z.number(),
maxPages: zod_1.z.number(),
reverse: zod_1.z.boolean(),
consistent: zod_1.z.boolean(),
entityAttrFilter: zod_1.z.boolean(),
showEntityAttr: zod_1.z.boolean(),
attributes: zod_1.z.array(zod_1.z.string()).min(1),
select: zod_1.z.enum(select_js_1.selectOptions),
tableName: zod_1.z.string()
})
.partial()
.default({});
const addAccessPatternTool = (server, accessPattern, { dbTableKey, dbAccessPatternKey }) => {
let table = undefined;
let entity = undefined;
let schema = undefined;
let meta = {};
if (accessPattern instanceof index_js_1.IAccessPattern) {
table = accessPattern.entity.table;
entity = accessPattern.entity;
meta = accessPattern[constants_js_1.$meta];
schema = accessPattern[constants_js_1.$schema];
}
if (accessPattern instanceof index_js_3.IAccessPattern) {
table = accessPattern.table;
const entities = accessPattern[index_js_4.$entities];
if (entities.length === 1) {
entity = entities[0];
}
meta = accessPattern[constants_js_2.$meta];
schema = accessPattern[constants_js_2.$schema];
}
if (table === undefined || schema === undefined) {
return;
}
const tableName = table.tableName !== undefined ? table.getName() : undefined;
const hasTableName = tableName !== undefined;
const queryOptionsSchema = hasTableName
? defaultQueryOptionsSchema
: defaultQueryOptionsSchema.removeDefault().required({ tableName: true });
const queryToolName = `ddb-tb_use-${dbAccessPatternKey}-access-pattern-on-${dbTableKey}-table`.substring(0, 64);
let queryToolDescription = `Query multiple ${entity !== undefined ? `'${entity.entityName}' ` : ''} Items from the ${tableName !== null && tableName !== void 0 ? tableName : dbTableKey} Table using the ${dbAccessPatternKey} AccessPattern.`;
const { title, description } = meta;
if (title !== undefined) {
queryToolDescription += `\n# ${title}`;
}
if (description !== undefined) {
queryToolDescription += `\n\n${description}.`;
}
server.tool(queryToolName, queryToolDescription, {
query: new index_js_2.ZodSchemer(schema).parser({ transform: false }),
options: queryOptionsSchema
}, { title: queryToolDescription, readOnlyHint: true, destructiveHint: false }, async ({ query, options }) => {
try {
let command = accessPattern.query(query);
if (Object.values(options).length > 0) {
command = command.options(options);
}
const Response = await command.send();
if (Response.Items === undefined) {
Response.Items = [];
}
return {
content: [{ type: 'text', text: JSON.stringify(Response) }]
};
}
catch (error) {
return {
content: [{ type: 'text', text: String(error), isError: true }]
};
}
});
};
exports.addAccessPatternTool = addAccessPatternTool;