import {ensureTableTemplate} from './ensure-table'; import {handleCommonErrors, objectToString} from './helpers'; export interface DeleteItemTplInput { readonly key: Record; readonly tableName: string; readonly typeName: string; } /** template */ export function deleteItemTpl({key, tableName, typeName}: DeleteItemTplInput) { const outputTypeName = `Delete${typeName}Output`; const primaryKeyType = `${typeName}PrimaryKey`; return ` export type ${outputTypeName} = ResultType; /** */ export async function delete${typeName}(input: ${primaryKeyType}): Promise<${outputTypeName}> { ${ensureTableTemplate(tableName)} try { const commandInput: DeleteCommandInput = { ConditionExpression: 'attribute_exists(#pk)', ExpressionAttributeNames: { "#pk": "pk", }, Key: ${objectToString(key)}, ReturnConsumedCapacity: 'INDEXES', ReturnItemCollectionMetrics: 'SIZE', ReturnValues: 'NONE', TableName: tableName, }; const {ConsumedCapacity: capacity, ItemCollectionMetrics: metrics} = await ddbDocClient.send(new DeleteCommand(commandInput)); assert(capacity, 'Expected ConsumedCapacity to be returned. This is a bug in codegen.'); return { capacity, item: undefined, metrics, } } catch (err) { if (err instanceof ConditionalCheckFailedException) { throw new NotFoundError('${typeName}', input); } ${handleCommonErrors()} } } `; }