import { Converter , PutItemInput } from 'aws-sdk/clients/dynamodb' ; import { Context } from '../context' ; import { toWrapped , getCollection } from '../base/util' ; import { DocumentWithId } from '../base/common' ; import { createNameMapper , createValueMapper } from '../base/mappers' ; import debugDynamo from '../debug/debugDynamo' ; import { CompositeCondition } from '../base/conditions' ; import { parseCompositeCondition } from '../base/conditions_parser' ; param context param collectionName param value param options param options . condition returns throws export async function replace < DocumentType extends DocumentWithId > ( context , collectionName , value , options = { } ) { const collection = getCollection ( context , collectionName ) ; const wrapped = toWrapped < DocumentType > ( collection , value ) ; let conditionExpression ; const nameMapper = createNameMapper ( ) ; const valueMapper = createValueMapper ( ) ; if ( options . condition ) { conditionExpression = parseCompositeCondition ( options . condition , { nameMapper , valueMapper , parsePath : [ ] , } ) ; } const request = { TableName : collection . layout . tableName , Item : Converter . marshall ( wrapped ) , ReturnValues : 'NONE' , ConditionExpression : conditionExpression , ExpressionAttributeNames : nameMapper . get ( ) , ExpressionAttributeValues : valueMapper . get ( ) , } ; debugDynamo ( 'PutItem' , request ) ; await context . ddb . putItem ( request ) . promise ( ) ; return wrapped . value ; }