Skip to content

Commit f92737f

Browse files
Merge pull request #242 from LeandroDG/master
feat: projectionExpression for single table batch get, scan and query
2 parents 083f9b7 + d2ec86f commit f92737f

File tree

8 files changed

+84
-14
lines changed

8 files changed

+84
-14
lines changed

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ describe('batch get', () => {
6767
expect(request.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)]).toBeDefined()
6868
expect(request.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)].ConsistentRead).toBeTruthy()
6969
})
70+
71+
it('projection expression', () => {
72+
const request = new BatchGetSingleTableRequest<any>(<any>null, SimpleWithPartitionKeyModel, [{ id: 'myId' }])
73+
request.projectionExpression('name')
74+
expect(request.params.RequestItems).toBeDefined()
75+
expect(request.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)]).toBeDefined()
76+
expect(request.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)].ProjectionExpression).toBe('#name')
77+
expect(request.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)].ExpressionAttributeNames).toEqual({
78+
'#name': 'name',
79+
})
80+
})
7081
})
7182

7283
describe('should return appropriate value', () => {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ModelConstructor } from '../../../model/model-constructor'
1111
import { batchGetItemsFetchAll } from '../../batchget/batch-get-utils'
1212
import { BATCH_GET_DEFAULT_TIME_SLOT, BATCH_GET_MAX_REQUEST_ITEM_COUNT } from '../../batchget/batch-get.const'
1313
import { DynamoDbWrapper } from '../../dynamo-db-wrapper'
14+
import { resolveAttributeNames } from '../../expression/functions/attribute-names.function'
1415
import { BaseRequest } from '../base.request'
1516
import { BatchGetSingleTableResponse } from './batch-get-single-table.response'
1617

@@ -39,11 +40,31 @@ export class BatchGetSingleTableRequest<T> extends BaseRequest<
3940
}
4041
}
4142

43+
/**
44+
* Determines the read consistency model: If set to true, then the operation uses strongly consistent reads; otherwise, the operation uses eventually consistent reads.
45+
*/
4246
consistentRead(value: boolean = true): BatchGetSingleTableRequest<T> {
4347
this.params.RequestItems[this.tableName].ConsistentRead = value
4448
return this
4549
}
4650

51+
/**
52+
* Specifies the list of document attributes to be returned from the table instead of returning the entire document
53+
* @param attributesToGet List of document attributes to be returned
54+
*/
55+
projectionExpression(...attributesToGet: string[]): BatchGetSingleTableRequest<T> {
56+
// tslint:disable-next-line:no-unnecessary-callback-wrapper
57+
const resolved = attributesToGet.map(a => resolveAttributeNames(a))
58+
this.params.RequestItems[this.tableName].ProjectionExpression = resolved.map(attr => attr.placeholder).join(', ')
59+
Object.values(resolved).forEach(r => {
60+
this.params.RequestItems[this.tableName].ExpressionAttributeNames = {
61+
...this.params.RequestItems[this.tableName].ExpressionAttributeNames,
62+
...r.attributeNames,
63+
}
64+
})
65+
return this
66+
}
67+
4768
/**
4869
* fetch all entries and return the raw response (without parsing the attributes to js objects)
4970
* @param backoffTimer when unprocessed keys are returned the next value of backoffTimer is used to determine how many time slots to wait before doing the next request

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as DynamoDB from 'aws-sdk/clients/dynamodb'
88
*/
99
export interface BatchGetSingleTableResponse<T> {
1010
/**
11-
* A map of table name to a list of items. Each object in Responses consists of a table name, along with a map of attribute data consisting of the data type and attribute value.
11+
* A map of table name to a list of items. Each object in Responses consists of a table name, along with a map of attribute data consisting of the data type and attribute value, as specified by ProjectionExpression.
1212
*/
1313
Items: T[]
1414
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class GetRequest<T> extends StandardRequest<T, DynamoDB.GetItemInput, Get
3232
return this
3333
}
3434

35+
/**
36+
* Specifies the list of document attributes to be returned from the table instead of returning the entire document
37+
* @param attributesToGet List of document attributes to be returned
38+
*/
3539
projectionExpression(...attributesToGet: string[]): GetRequest<T> {
3640
// tslint:disable-next-line:no-unnecessary-callback-wrapper
3741
const resolved = attributesToGet.map(a => resolveAttributeNames(a))

src/dynamo/request/query/query.response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as DynamoDB from 'aws-sdk/clients/dynamodb'
99
*/
1010
export interface QueryResponse<T> {
1111
/**
12-
* An array of item attributes that match the query criteria. Each element in this array consists of an attribute name and the value for that attribute.
12+
* An array of item attributes that match the query criteria. Each element in this array consists of an attribute name and the value for that attribute, as specified by ProjectionExpression.
1313
*/
1414
Items: T[]
1515
/**

src/dynamo/request/read-many.request.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { fromDb } from '../../mapper/mapper'
1010
import { Attributes } from '../../mapper/type/attribute.type'
1111
import { ModelConstructor } from '../../model/model-constructor'
1212
import { DynamoDbWrapper } from '../dynamo-db-wrapper'
13+
import { resolveAttributeNames } from '../expression/functions/attribute-names.function'
1314
import { and } from '../expression/logical-operator/and.function'
1415
import { addExpression } from '../expression/param-util'
1516
import { addCondition } from '../expression/request-expression-builder'
@@ -106,6 +107,20 @@ export abstract class ReadManyRequest<
106107
return <any>this
107108
}
108109

110+
/**
111+
* Specifies the list of document attributes to be returned from the table instead of returning the entire document
112+
* @param attributesToGet List of document attributes to be returned
113+
*/
114+
projectionExpression(...attributesToGet: string[]): R {
115+
// tslint:disable-next-line:no-unnecessary-callback-wrapper
116+
const resolved = attributesToGet.map(a => resolveAttributeNames(a))
117+
this.params.ProjectionExpression = resolved.map(attr => attr.placeholder).join(', ')
118+
Object.values(resolved).forEach(r => {
119+
this.params.ExpressionAttributeNames = { ...this.params.ExpressionAttributeNames, ...r.attributeNames }
120+
})
121+
return <any>this
122+
}
123+
109124
/**
110125
* add a condition for propertyPath
111126
* @example req.whereAttribute('path.to.prop').eq('value')

src/dynamo/request/scan/scan.response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as DynamoDB from 'aws-sdk/clients/dynamodb'
55

66
export interface ScanResponse<T> {
77
/**
8-
* An array of item attributes that match the scan criteria. Each element in this array consists of an attribute name and the value for that attribute.
8+
* An array of item attributes that match the scan criteria. Each element in this array consists of an attribute name and the value for that attribute, as specified by ProjectionExpression.
99
*/
1010
Items: T[]
1111
/**

0 commit comments

Comments
 (0)