Skip to content

Commit b5419e1

Browse files
refactor(typing): get rid of AttributeMap & AttributeValue
- Added generics for Attributes to have nicer support, maybe we should also use reflection in Attribute typing which would help for Types with complex values (Object, List) - remove all usages of AttributeMap & AttributeValue and also cast in test for nicer type checking
1 parent eda1c63 commit b5419e1

17 files changed

+195
-193
lines changed

src/dynamo/batchget/batch-get.request.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AttributeMap, BatchGetItemInput } from 'aws-sdk/clients/dynamodb'
1+
import { BatchGetItemInput } from 'aws-sdk/clients/dynamodb'
22
import { isObject, isString } from 'lodash'
33
import { Observable } from 'rxjs'
44
import { map } from 'rxjs/operators'
@@ -45,11 +45,11 @@ export class BatchGetRequest {
4545
this.tables.set(tableName, modelClazz)
4646

4747
const metadata = MetadataHelper.get(modelClazz)
48-
const attributeMaps: AttributeMap[] = []
48+
const attributeMaps: Attributes[] = []
4949

5050
// loop over all the keys
5151
keys.forEach(key => {
52-
const idOb: AttributeMap = {}
52+
const idOb: Attributes = {}
5353

5454
if (isString(key)) {
5555
// got a simple primary key
@@ -101,8 +101,8 @@ export class BatchGetRequest {
101101

102102
if (response.Responses && Object.keys(response.Responses).length) {
103103
Object.keys(response.Responses).forEach(tableName => {
104-
const mapped = response.Responses![tableName].map(attributeMap =>
105-
Mapper.fromDb(<Attributes>attributeMap, this.tables.get(tableName))
104+
const mapped = response.Responses![tableName].map(attributes =>
105+
Mapper.fromDb(<Attributes>attributes, this.tables.get(tableName))
106106
)
107107
r.Responses![tableName] = mapped
108108
})

src/dynamo/expression/condition-expression-builder.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { AttributeMap, AttributeValue } from 'aws-sdk/clients/dynamodb'
21
import { curryRight, forEach, isPlainObject } from 'lodash'
32
import { Metadata } from '../../decorator/metadata/metadata'
43
import { PropertyMetadata } from '../../decorator/metadata/property-metadata.model'
54
import { Mapper } from '../../mapper/mapper'
5+
import { Attribute, Attributes } from '../../mapper/type/attribute.type'
66
import { Util } from '../../mapper/util'
77
import { resolveAttributeNames } from './functions/attribute-names.function'
88
import { isFunctionOperator } from './functions/is-function-operator.function'
@@ -151,17 +151,15 @@ export class ConditionExpressionBuilder {
151151
existingValueNames: string[] | undefined,
152152
propertyMetadata: PropertyMetadata<any> | undefined
153153
): Expression {
154-
const attributeValues: AttributeMap = (<any[]>values[0])
155-
.map(value => Mapper.toDbOne(value, propertyMetadata))
156-
.reduce(
157-
(result, mappedValue: AttributeValue | null, index: number) => {
158-
if (mappedValue !== null) {
159-
result[`${valuePlaceholder}_${index}`] = mappedValue
160-
}
161-
return result
162-
},
163-
<AttributeMap>{}
164-
)
154+
const attributeValues: Attributes = (<any[]>values[0]).map(value => Mapper.toDbOne(value, propertyMetadata)).reduce(
155+
(result, mappedValue: Attribute | null, index: number) => {
156+
if (mappedValue !== null) {
157+
result[`${valuePlaceholder}_${index}`] = mappedValue
158+
}
159+
return result
160+
},
161+
<Attributes>{}
162+
)
165163

166164
const inStatement = (<any[]>values[0]).map((value: any, index: number) => `${valuePlaceholder}_${index}`).join(', ')
167165

@@ -181,7 +179,7 @@ export class ConditionExpressionBuilder {
181179
existingValueNames: string[] | undefined,
182180
propertyMetadata: PropertyMetadata<any> | undefined
183181
): Expression {
184-
const attributeValues: AttributeMap = {}
182+
const attributes: Attributes = {}
185183
const mappedValue1 = Mapper.toDbOne(values[0], propertyMetadata)
186184
const mappedValue2 = Mapper.toDbOne(values[1], propertyMetadata)
187185

@@ -192,13 +190,13 @@ export class ConditionExpressionBuilder {
192190
const value2Placeholder = uniqAttributeValueName(attributePath, [valuePlaceholder].concat(existingValueNames || []))
193191

194192
const statement = `${namePlaceholder} BETWEEN ${valuePlaceholder} AND ${value2Placeholder}`
195-
attributeValues[valuePlaceholder] = mappedValue1
196-
attributeValues[value2Placeholder] = mappedValue2
193+
attributes[valuePlaceholder] = mappedValue1
194+
attributes[value2Placeholder] = mappedValue2
197195

198196
return {
199197
statement,
200198
attributeNames,
201-
attributeValues,
199+
attributeValues: attributes,
202200
}
203201
}
204202

@@ -225,28 +223,28 @@ export class ConditionExpressionBuilder {
225223
statement = [namePlaceholder, operator, valuePlaceholder].join(' ')
226224
}
227225

228-
const attributeValues: AttributeMap = {}
226+
const attributes: Attributes = {}
229227
if (hasValue) {
230-
let value: AttributeValue | null
228+
let attribute: Attribute | null
231229
switch (operator) {
232230
case 'contains':
233231
// TODO think about validation
234232
// ConditionExpressionBuilder.validateValueForContains(values[0], propertyMetadata)
235-
value = Mapper.toDbOne(values[0], propertyMetadata)
233+
attribute = Mapper.toDbOne(values[0], propertyMetadata)
236234
break
237235
default:
238-
value = Mapper.toDbOne(values[0], propertyMetadata)
236+
attribute = Mapper.toDbOne(values[0], propertyMetadata)
239237
}
240238

241-
if (value) {
242-
attributeValues[valuePlaceholder] = value
239+
if (attribute) {
240+
attributes[valuePlaceholder] = attribute
243241
}
244242
}
245243

246244
return {
247245
statement,
248246
attributeNames,
249-
attributeValues,
247+
attributeValues: attributes,
250248
}
251249
}
252250

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { AttributeMap } from 'aws-sdk/clients/dynamodb'
1+
import { Attributes } from '../../../mapper/type/attribute.type'
22

33
export interface Expression {
44
attributeNames: { [key: string]: string }
5-
attributeValues: AttributeMap
5+
attributeValues: Attributes
66
statement: string
77
}

src/dynamo/expression/update-expression-builder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { AttributeMap, AttributeValue } from 'aws-sdk/clients/dynamodb'
21
import { Metadata } from '../../decorator/metadata/metadata'
32
import { PropertyMetadata } from '../../decorator/metadata/property-metadata.model'
43
import { Mapper } from '../../mapper/mapper'
4+
import { Attribute, Attributes } from '../../mapper/type/attribute.type'
55
import { Util } from '../../mapper/util'
66
import { ConditionExpressionBuilder } from './condition-expression-builder'
77
import { resolveAttributeNames } from './functions/attribute-names.function'
@@ -137,20 +137,20 @@ export class UpdateExpressionBuilder {
137137

138138
const hasValue = !UpdateExpressionBuilder.isNoValueAction(operator.action)
139139

140-
const attributeValues: AttributeMap = {}
140+
const attributes: Attributes = {}
141141
if (hasValue) {
142-
const value: AttributeValue | null = Mapper.toDbOne(values[0], propertyMetadata)
142+
const attribute: Attribute | null = Mapper.toDbOne(values[0], propertyMetadata)
143143

144-
if (value) {
145-
attributeValues[valuePlaceholder] = value
144+
if (attribute) {
145+
attributes[valuePlaceholder] = attribute
146146
}
147147
}
148148

149149
return {
150150
type: operator.actionKeyword,
151151
statement,
152152
attributeNames,
153-
attributeValues,
153+
attributeValues: attributes,
154154
}
155155
}
156156

src/dynamo/request/batchgetsingletable/batch-get-single-table.request.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { AttributeMap, BatchGetItemInput } from 'aws-sdk/clients/dynamodb'
1+
import { BatchGetItemInput } from 'aws-sdk/clients/dynamodb'
22
import { isObject } from 'lodash'
33
import { Observable } from 'rxjs'
44
import { map } from 'rxjs/operators'
55
import { Metadata } from '../../../decorator/metadata/metadata'
66
import { MetadataHelper } from '../../../decorator/metadata/metadata-helper'
77
import { Mapper } from '../../../mapper/mapper'
8-
import { Attributes } from '../../../mapper/type/attribute.type'
8+
import { Attributes, Attributes } from '../../../mapper/type/attribute.type'
99
import { ModelConstructor } from '../../../model/model-constructor'
1010
import { DynamoRx } from '../../dynamo-rx'
1111
import { BatchGetSingleTableResponse } from './batch-get-single-table.response'
@@ -81,10 +81,10 @@ export class BatchGetSingleTableRequest<T> {
8181
}
8282

8383
private addKeyParams(keys: any[]) {
84-
const attributeMaps: AttributeMap[] = []
84+
const attributeMaps: Attributes[] = []
8585

8686
keys.forEach(key => {
87-
const idOb: AttributeMap = {}
87+
const idOb: Attributes = {}
8888
if (isObject(key)) {
8989
// TODO add some more checks
9090
// got a composite primary key

src/dynamo/request/delete/delete.request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
AttributeMap,
32
DeleteItemInput,
43
DeleteItemOutput,
54
ReturnConsumedCapacity,
@@ -8,6 +7,7 @@ import {
87
import { Observable } from 'rxjs'
98
import { map } from 'rxjs/operators'
109
import { Mapper } from '../../../mapper/mapper'
10+
import { Attributes } from '../../../mapper/type/attribute.type'
1111
import { ModelConstructor } from '../../../model/model-constructor'
1212
import { DynamoRx } from '../../dynamo-rx'
1313
import { and } from '../../expression/logical-operator/and.function'
@@ -33,7 +33,7 @@ export class DeleteRequest<T> extends BaseRequest<T, DeleteItemInput> {
3333
throw new Error(`please provide the sort key for attribute ${this.metaData.getSortKey()}`)
3434
}
3535

36-
const keyAttributeMap: AttributeMap = {}
36+
const keyAttributeMap: Attributes = {}
3737

3838
// partition key
3939
const partitionKeyValue = Mapper.toDbOne(partitionKey, this.metaData.forProperty(this.metaData.getPartitionKey()))

src/dynamo/request/get/get.request.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('get requst', () => {
88

99
beforeEach(() => {
1010
request = new GetRequest(
11-
null,
11+
<any>null,
1212
SimpleWithPartitionKeyModel,
1313
getTableName(SimpleWithPartitionKeyModel),
1414
'partitionKeyValue'

src/dynamo/request/get/get.request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AttributeMap, ReturnConsumedCapacity } from 'aws-sdk/clients/dynamodb'
1+
import { ReturnConsumedCapacity } from 'aws-sdk/clients/dynamodb'
22
import { values as objValues } from 'lodash'
33
import { Observable } from 'rxjs'
44
import { map } from 'rxjs/operators'
@@ -26,7 +26,7 @@ export class GetRequest<T> extends BaseRequest<T, any> {
2626
throw new Error(`please provide the sort key for attribute ${this.metaData.getSortKey()}`)
2727
}
2828

29-
const keyAttributeMap: AttributeMap = {}
29+
const keyAttributeMap: Attributes = {}
3030

3131
// partition key
3232
const partitionKeyValue = Mapper.toDbOne(partitionKey, this.metaData.forProperty(this.metaData.getPartitionKey()))

src/dynamo/request/update/update.request.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import {
2-
AttributeMap,
3-
ReturnConsumedCapacity,
4-
ReturnItemCollectionMetrics,
5-
UpdateItemOutput,
6-
} from 'aws-sdk/clients/dynamodb'
1+
import { ReturnConsumedCapacity, ReturnItemCollectionMetrics, UpdateItemOutput } from 'aws-sdk/clients/dynamodb'
72
import { forEach } from 'lodash'
83
import { Observable } from 'rxjs'
94
import { map } from 'rxjs/operators'
105
import { Mapper } from '../../../mapper/mapper'
6+
import { Attributes } from '../../../mapper/type/attribute.type'
117
import { ModelConstructor } from '../../../model/model-constructor'
128
import { DynamoRx } from '../../dynamo-rx'
139
import { and } from '../../expression/logical-operator/and.function'
@@ -39,7 +35,7 @@ export class UpdateRequest<T> extends BaseRequest<T, any> {
3935
throw new Error(`please provide the sort key for attribute ${this.metaData.getSortKey()}`)
4036
}
4137

42-
const keyAttributeMap: AttributeMap = {}
38+
const keyAttributeMap: Attributes = {}
4339

4440
// partition key
4541
const partitionKeyValue = Mapper.toDbOne(partitionKey, this.metaData.forProperty(this.metaData.getPartitionKey()))
@@ -93,7 +89,7 @@ export class UpdateRequest<T> extends BaseRequest<T, any> {
9389
)
9490

9591
const actionStatements: string[] = []
96-
let attributeValues: AttributeMap = {}
92+
let attributeValues: Attributes = {}
9793
let attributeNames: { [key: string]: string } = {}
9894

9995
forEach(sortedByActionKeyWord, (value, key) => {

0 commit comments

Comments
 (0)