module import * as DynamoDB from 'aws-sdk/clients/dynamodb' ; import { Metadata } from '../../decorator/metadata/metadata' ; import { Attributes } from '../../mapper/type/attribute.type' ; import { addUpdateExpression } from './param-util' ; import { Expression } from './type/expression.type' ; import { UpdateActionKeyword } from './type/update-action-keyword.type' ; import { UpdateExpressionDefinitionFunction } from './type/update-expression-definition-function' ; import { UpdateExpression } from './type/update-expression.type' ; hidden export function prepareAndAddUpdateExpressions ( metadata , params , updateDefFns ) { if ( updateDefFns && updateDefFns . length ) { const sortedByActionKeyWord = updateDefFns . map ( ( updateDefFn ) => { return updateDefFn ( < any > params . ExpressionAttributeNames , metadata ) ; } ) . reduce ( ( result , expr ) => { const actionKeyword = expr . type ; if ( ! result . has ( actionKeyword ) ) { result . set ( actionKeyword , [ ] ) ; } result . get ( actionKeyword ) . push ( expr ) ; return result ; } , new Map ( ) ) ; const actionStatements = [ ] ; let attributeValues = { } ; let attributeNames = { } ; for ( const [ actionKeyword , updateExpressions ] of sortedByActionKeyWord ) { const statements = [ ] ; if ( updateExpressions && updateExpressions . length ) { updateExpressions . forEach ( ( updateExpression ) => { statements . push ( updateExpression . statement ) ; attributeValues = { ... attributeValues , ... updateExpression . attributeValues } ; attributeNames = { ... attributeNames , ... updateExpression . attributeNames } ; } ) ; actionStatements . push ( `${ actionKeyword } ${ statements . join ( ', ' ) }` ) ; } } const expression = { statement : actionStatements . join ( ' ' ) , attributeValues , attributeNames , } ; addUpdateExpression ( expression , params ) ; } else { throw new Error ( 'at least one update operation must be defined' ) ; } }