import { Converter , DeleteItemInput } from 'aws-sdk/clients/dynamodb' ; import { Context } from '../context' ; import { unwrap , assemblePrimaryKeyValue , getRootCollection , } from '../base/util' ; import { DocumentWithId , WrappedDocument } from '../base/common' ; import debugDynamo from '../debug/debugDynamo' ; import { CompositeCondition } from '../base/conditions' ; import { createNameMapper , createValueMapper } from '../base/mappers' ; import { parseCompositeCondition } from '../base/conditions_parser' ; param context param collectionName param id param options param options . condition returns throws export async function deleteById < DocumentType extends DocumentWithId > ( context , collectionName , id , options = { } ) { const collection = getRootCollection ( context , collectionName ) ; const nameMapper = createNameMapper ( ) ; const valueMapper = createValueMapper ( ) ; let conditionExpression ; if ( options . condition ) { conditionExpression = parseCompositeCondition ( options . condition , { nameMapper , valueMapper , parsePath : [ ] , } ) ; } const request = { TableName : collection . layout . tableName , Key : Converter . marshall ( { [ collection . layout . primaryKey . partitionKey ] : assemblePrimaryKeyValue ( collectionName , id , collection . layout . indexKeySeparator ) , [ collection . layout . primaryKey . sortKey ] : assemblePrimaryKeyValue ( collectionName , id , collection . layout . indexKeySeparator ) , } ) , ReturnValues : 'ALL_OLD' , ConditionExpression : conditionExpression , ExpressionAttributeNames : nameMapper . get ( ) , ExpressionAttributeValues : valueMapper . get ( ) , } ; debugDynamo ( 'DeleteItem' , request ) ; const result = await context . ddb . deleteItem ( request ) . promise ( ) ; if ( result . Attributes ) { const wrapped = Converter . unmarshall ( result . Attributes ) ; return unwrap ( wrapped as WrappedDocument < DocumentType > ) ; } return undefined ; }