11import { AttributeMap , BatchGetItemInput } from 'aws-sdk/clients/dynamodb'
2- import { isObject } from 'lodash'
2+ import { isObject , isString } from 'lodash'
33import { Observable } from 'rxjs/Observable'
44import { MetadataHelper } from '../../decorator/metadata/metadata-helper'
55import { Mapper } from '../../mapper/mapper'
66import { ModelConstructor } from '../../model/model-constructor'
77import { DEFAULT_SESSION_VALIDITY_ENSURER } from '../default-session-validity-ensurer.const'
88import { DEFAULT_TABLE_NAME_RESOLVER } from '../default-table-name-resolver.const'
99import { DynamoRx } from '../dynamo-rx'
10+ import { PrimaryKey } from '../primary-key.type'
1011import { REGEX_TABLE_NAME } from '../request/regex'
1112import { SessionValidityEnsurer } from '../session-validity-ensurer.type'
1213import { 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
2317export 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 } )
0 commit comments