import { AttributeValue , Converter } from 'aws-sdk/clients/dynamodb' ; import { isSafeAttributeName } from './expression_util' ; import { invertMap } from './util' ; internal export type NameMapper = { map ( name , mappedName ? ) : string ; get ( ) : { [ mappedName ] : string ; } | undefined ; } ; internal export const createNameMapper = ( ) : NameMapper => { let currentIndex = 0 ; const attributeNameMap = new Map < string , string > ( ) ; return { /**
         * Generate an expression attribute name for
         * `name` (if necessary - values not requiring
         * escaping will be returned as-is)
         */ map ( name , mappedName ? ) { if ( ! mappedName && isSafeAttributeName ( name ) ) { return name ; } let nameMapping = attributeNameMap . get ( name ) ; if ( ! nameMapping ) { nameMapping = mappedName ?? `#attr${ currentIndex ++ }` ; attributeNameMap . set ( name , nameMapping ) ; } return nameMapping ; } , /**
         * Return the map of attribute names
         */ get ( ) { const result = invertMap ( attributeNameMap ) ; if ( Object . keys ( result ) . length === 0 ) return undefined ; return result ; } , } ; } ; internal export type ValueMapper = { map ( value ) : string ; get ( ) : { [ mappedName ] : AttributeValue ; } | undefined ; } ; internal export const createValueMapper = ( ) : ValueMapper => { let currentIndex = 0 ; const valueMap = new Map < string , AttributeValue > ( ) ; return { /**
         * Given `value`, marshall it to DynamoDB format, store
         * it internally, and return the `:value` reference that
         * can be used in expressions
         */ map ( value ) { const valueKey = `:value${ currentIndex ++ }` ; const convertedValue = Converter . input ( value ) ; valueMap . set ( valueKey , convertedValue ) ; return valueKey ; } , /**
         * Get the map for `ExpressionAttributeValues`
         */ get ( ) { if ( valueMap . size === 0 ) return undefined ; return Array . from ( valueMap ) . reduce ( ( obj , [ key , value ] ) => { obj [ key ] = value ; return obj ; } , { } as { [ key ] : AttributeValue ; } ) ; } , } ; } ;