|
1 | 1 | import * as DynamoDB from 'aws-sdk/clients/dynamodb' |
2 | | -import { forEach } from 'lodash' |
3 | 2 | import { Metadata } from '../../decorator/metadata/index' |
4 | 3 | import { Attributes } from '../../mapper/index' |
5 | | -import { SortedUpdateExpressions } from '../request/update/update.request' |
6 | 4 | import { addUpdateExpression } from './param-util' |
7 | | -import { Expression, UpdateExpressionDefinitionFunction } from './type/index' |
| 5 | +import { Expression, UpdateExpression, UpdateExpressionDefinitionFunction } from './type/index' |
| 6 | +import { UpdateActionKeyword } from './type/update-action-keyword.type' |
8 | 7 |
|
9 | 8 | export function prepareAndAddUpdateExpressions( |
10 | 9 | metadata: Metadata<any>, |
11 | 10 | params: DynamoDB.UpdateItemInput | DynamoDB.Update, |
12 | 11 | updateDefFns: UpdateExpressionDefinitionFunction[], |
13 | 12 | ) { |
14 | 13 | if (updateDefFns && updateDefFns.length) { |
15 | | - const sortedByActionKeyWord: SortedUpdateExpressions = updateDefFns |
| 14 | + const sortedByActionKeyWord: Map<UpdateActionKeyword, UpdateExpression[]> = updateDefFns |
16 | 15 | .map(updateDefFn => { |
17 | 16 | return updateDefFn(<any>params.ExpressionAttributeNames, metadata) |
18 | 17 | }) |
19 | | - .reduce( |
20 | | - (result, expr) => { |
21 | | - if (!result[expr.type]) { |
22 | | - result[expr.type] = [] |
| 18 | + .reduce((result, expr) => { |
| 19 | + const actionKeyword = expr.type |
| 20 | + if (!result.has(actionKeyword)) { |
| 21 | + result.set(actionKeyword, []) |
23 | 22 | } |
24 | 23 |
|
25 | | - result[expr.type].push(expr) |
| 24 | + result.get(actionKeyword).push(expr) |
26 | 25 | return result |
27 | 26 | }, |
28 | | - <SortedUpdateExpressions>{}, |
| 27 | + new Map(), |
29 | 28 | ) |
30 | 29 |
|
31 | 30 | const actionStatements: string[] = [] |
32 | 31 | let attributeValues: Attributes = {} |
33 | 32 | let attributeNames: Record<string, string> = {} |
34 | 33 |
|
35 | | - forEach(sortedByActionKeyWord, (value, key) => { |
| 34 | + for (const [actionKeyword, updateExpressions] of sortedByActionKeyWord) { |
36 | 35 | const statements: string[] = [] |
37 | | - if (value && value.length) { |
38 | | - value.forEach(updateExpression => { |
| 36 | + if (updateExpressions && updateExpressions.length) { |
| 37 | + updateExpressions.forEach(updateExpression => { |
39 | 38 | statements.push(updateExpression.statement) |
40 | 39 | attributeValues = { ...attributeValues, ...updateExpression.attributeValues } |
41 | 40 | attributeNames = { ...attributeNames, ...updateExpression.attributeNames } |
42 | 41 | }) |
43 | | - actionStatements.push(`${key} ${statements.join(', ')}`) |
| 42 | + actionStatements.push(`${actionKeyword} ${statements.join(', ')}`) |
44 | 43 | } |
45 | | - }) |
| 44 | + } |
46 | 45 |
|
47 | 46 | const expression: Expression = { |
48 | 47 | statement: actionStatements.join(' '), |
|
0 commit comments