Skip to content

Commit 8d144ff

Browse files
fix(batchRequest): make sure the response can be mapped
1 parent 1cdf79b commit 8d144ff

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
import { AttributeMap, BatchGetItemInput } from 'aws-sdk/clients/dynamodb'
2-
import { isObject } from 'lodash'
2+
import { isObject, isString } from 'lodash'
33
import { Observable } from 'rxjs/Observable'
44
import { MetadataHelper } from '../../decorator/metadata/metadata-helper'
55
import { Mapper } from '../../mapper/mapper'
66
import { ModelConstructor } from '../../model/model-constructor'
77
import { DEFAULT_SESSION_VALIDITY_ENSURER } from '../default-session-validity-ensurer.const'
88
import { DEFAULT_TABLE_NAME_RESOLVER } from '../default-table-name-resolver.const'
99
import { DynamoRx } from '../dynamo-rx'
10+
import { PrimaryKey } from '../primary-key.type'
1011
import { REGEX_TABLE_NAME } from '../request/regex'
1112
import { SessionValidityEnsurer } from '../session-validity-ensurer.type'
1213
import { TableNameResolver } from '../table-name-resolver.type'
14+
import { BatchGetResponse } from './batch-get.response'
1315

14-
interface TableConfig<T> {
15-
tableName: string
16-
modelClazz: ModelConstructor<T>
17-
keys: any[]
18-
}
19-
20-
// tslint:disable-next-line:interface-over-type-literal
21-
export type BatchGetItemResponse = { [tableName: string]: any[] }
22-
16+
// TODO add support for indexes
2317
export class BatchGetRequest {
2418
private readonly dynamoRx: DynamoRx
2519

26-
private tables: Map<string, TableConfig<any>> = new Map()
20+
private tables: Map<string, ModelConstructor<any>> = new Map()
2721
readonly params: BatchGetItemInput
2822

2923
constructor(
@@ -41,11 +35,12 @@ export class BatchGetRequest {
4135
* @param {any[]} keys either a simple string for partition key or an object with partitionKey and sortKey
4236
* @returns {BatchGetSingleTableRequest}
4337
*/
44-
forModel<T>(modelClazz: ModelConstructor<T>, keys: any[]): BatchGetRequest {
38+
forModel<T>(modelClazz: ModelConstructor<T>, keys: Array<string | PrimaryKey>): BatchGetRequest {
4539
const tableName = this.getTableName(modelClazz, this.tableNameResolver)
4640
if (this.tables.has(tableName)) {
4741
throw new Error('table name already exists, please provide all the keys for the same table at once')
4842
}
43+
this.tables.set(tableName, modelClazz)
4944

5045
const metadata = MetadataHelper.get(modelClazz)
5146
const attributeMaps: AttributeMap[] = []
@@ -54,8 +49,15 @@ export class BatchGetRequest {
5449
keys.forEach(key => {
5550
const idOb: AttributeMap = {}
5651

57-
if (isObject(key)) {
58-
// TODO add some more checks
52+
if (isString(key)) {
53+
// got a simple primary key
54+
const value = Mapper.toDbOne(key)
55+
if (value === null) {
56+
throw Error('please provide an actual value for partition key')
57+
}
58+
59+
idOb[metadata.getPartitionKey()] = value
60+
} else if (isObject(key) && key.partitionKey !== undefined && key.partitionKey !== null) {
5961
// got a composite primary key
6062

6163
// partition key
@@ -73,13 +75,7 @@ export class BatchGetRequest {
7375

7476
idOb[metadata.getSortKey()!] = mappedSortKey
7577
} else {
76-
// got a simple primary key
77-
const value = Mapper.toDbOne(key)
78-
if (value === null) {
79-
throw Error('please provide an actual value for partition key')
80-
}
81-
82-
idOb[metadata.getPartitionKey()] = value
78+
throw new Error('a key must either be a string or a PrimaryKey')
8379
}
8480

8581
attributeMaps.push(idOb)
@@ -88,21 +84,20 @@ export class BatchGetRequest {
8884
this.params.RequestItems[tableName] = {
8985
Keys: attributeMaps,
9086
}
87+
9188
return this
9289
}
9390

9491
execFullResponse() {}
9592

96-
// TODO fix any
97-
// TODO add support for indexes
98-
exec(): Observable<BatchGetItemResponse> {
93+
exec(): Observable<BatchGetResponse> {
9994
return this.dynamoRx.batchGetItems(this.params).map(response => {
100-
const r = <BatchGetItemResponse>{}
95+
const r = <BatchGetResponse>{}
10196
if (response.Responses && Object.keys(response.Responses).length) {
10297
const responses: { [key: string]: AttributeMap } = {}
10398
Object.keys(response.Responses).forEach(tableName => {
10499
const mapped = response.Responses![tableName].map(attributeMap =>
105-
Mapper.fromDb(attributeMap, this.tables.get(tableName)!.modelClazz)
100+
Mapper.fromDb(attributeMap, this.tables.get(tableName))
106101
)
107102
r[tableName] = mapped
108103
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// tslint:disable-next-line:interface-over-type-literal
2+
export type BatchGetResponse = { [tableName: string]: any[] }

src/dynamo/primary-key.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface PrimaryKey {
2+
partitionKey: any
3+
sortKey?: any
4+
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import { ModelConstructor } from '../../../model/model-constructor'
88
import { DynamoRx } from '../../dynamo-rx'
99
import { BatchGetSingleTableResponse } from './batch-get-single-table.response'
1010

11-
interface TableConfig<T> {
12-
tableName: string
13-
modelClazz: ModelConstructor<T>
14-
keys: any[]
15-
}
16-
11+
// TODO add support for indexes
1712
export class BatchGetSingleTableRequest<T> {
1813
readonly dynamoRx: DynamoRx
1914
readonly params: BatchGetItemInput
@@ -67,8 +62,6 @@ export class BatchGetSingleTableRequest<T> {
6762
})
6863
}
6964

70-
// TODO fix any
71-
// TODO add support for indexes
7265
exec(): Observable<T[]> {
7366
return this.dynamoRx.batchGetItems(this.params).map(response => {
7467
if (response.Responses && Object.keys(response.Responses).length && response.Responses[this.tableName]) {

0 commit comments

Comments
 (0)