import { Table } from '@typedorm/common' ; import { isEmptyObject } from '../../helpers/is-empty-object' ; import { MERGE_STRATEGY } from './base-expression-input' ; import { Condition } from './condition' ; import { Filter } from './filter' ; import { KeyCondition } from './key-condition' ; import { Projection } from './projection' ; import { Update } from './update/update' ; export class ExpressionBuilder { andMergeConditionExpressions ( existingExp , newExp ) { if ( existingExp . ConditionExpression && ! newExp . ConditionExpression ) { return this . removeEmptyFieldsAndReturn ( existingExp ) ; } if ( newExp . ConditionExpression && ! existingExp . ConditionExpression ) { return this . removeEmptyFieldsAndReturn ( newExp ) ; } if ( ! newExp && ! existingExp ) { return { } ; } const mergedExp = { ConditionExpression : `(${ existingExp . ConditionExpression }) AND (${ newExp . ConditionExpression })` , ExpressionAttributeNames : { ... existingExp . ExpressionAttributeNames , ... newExp . ExpressionAttributeNames , } , ExpressionAttributeValues : { ... existingExp . ExpressionAttributeValues , ... newExp . ExpressionAttributeValues , } , } ; return this . removeEmptyFieldsAndReturn ( mergedExp ) ; } param table buildUniqueRecordConditionExpression ( table ) { const uniqueRecordCondition = table . usesCompositeKey ( ) ? new Condition ( ) . attributeNotExist ( table . partitionKey ) . merge ( new Condition ( ) . attributeNotExist ( table . sortKey ) , MERGE_STRATEGY . AND ) : new Condition ( ) . attributeNotExist ( table . partitionKey ) ; const expression = this . buildConditionExpression ( uniqueRecordCondition ) ; return this . removeEmptyFieldsAndReturn ( expression ) ; } buildConditionExpression ( condition ) { if ( ! condition . expression ) { return { } ; } const expression = { ConditionExpression : condition . expression . trim ( ) , ExpressionAttributeNames : condition . names , ExpressionAttributeValues : condition . values , } ; return this . removeEmptyFieldsAndReturn ( expression ) ; } buildProjectionExpression ( projection ) { if ( ! projection . expression ) { return { } ; } const expression = { ProjectionExpression : projection . expression . trim ( ) , ExpressionAttributeNames : projection . names , } ; return this . removeEmptyFieldsAndReturn ( expression ) ; } buildUpdateExpression ( update ) { if ( ! update . expression ) { return { } ; } const expression = { UpdateExpression : update . expression . trim ( ) , ExpressionAttributeNames : update . names , ExpressionAttributeValues : update . values , } ; return this . removeEmptyFieldsAndReturn ( expression ) ; } buildKeyConditionExpression ( condition ) { if ( ! condition . expression ) { return { } ; } const expression = { KeyConditionExpression : condition . expression . trim ( ) , ExpressionAttributeNames : condition . names , ExpressionAttributeValues : condition . values , } ; return this . removeEmptyFieldsAndReturn ( expression ) ; } buildFilterExpression ( filter ) { if ( ! filter . expression ) { return { } ; } const expression = { FilterExpression : filter . expression . trim ( ) , ExpressionAttributeNames : filter . names , ExpressionAttributeValues : filter . values , } ; return this . removeEmptyFieldsAndReturn ( expression ) ; } private removeEmptyFieldsAndReturn ( expression ) { if ( isEmptyObject ( expression . ExpressionAttributeNames ) ) { delete expression . ExpressionAttributeNames ; } if ( isEmptyObject ( expression . ExpressionAttributeValues ) ) { delete expression . ExpressionAttributeValues ; } return expression ; } }