Skip to content

Commit d827d77

Browse files
Merge branch 'master' into #250-model-metadata
2 parents 4de844b + 9d8acc4 commit d827d77

34 files changed

+9774
-9176
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@
7373
"prettier": "^1.18.2",
7474
"reflect-metadata": "^0.1.13",
7575
"rimraf": "^3.0.0",
76-
"semantic-release": "^16.0.0-beta.18",
76+
"semantic-release": "^16.0.1",
7777
"sort-package-json": "^1.22.1",
7878
"ts-jest": "^24.0.2",
7979
"ts-node": "^8.3.0",
8080
"tslint": "^5.19.0",
8181
"tslint-config-prettier": "^1.18.0",
8282
"tsutils": "^3.17.1",
83-
"typedoc": "^0.15.0",
83+
"typedoc": "0.14.0",
8484
"typedoc-plugin-external-module-name": "^2.1.0",
8585
"typescript": ">=2.9.1",
8686
"uuid": "^3.3.2"
@@ -89,6 +89,7 @@
8989
"aws-sdk": "^2.401.0",
9090
"lodash": "^4.17.11",
9191
"reflect-metadata": "^0.1.12",
92+
"tslib": "^1.10.0",
9293
"uuid": "^3.3.2"
9394
}
9495
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,18 @@ export function doAddCondition<T, R extends ConditionalParamsHost>(
151151
/**
152152
* @hidden
153153
*/
154-
export function addPartitionKeyCondition<R extends StandardRequest<any, any, any>>(
154+
export function addPartitionKeyCondition<R extends StandardRequest<any, any, any, any>>(
155155
keyName: keyof any,
156156
keyValue: any,
157157
request: R,
158158
): R
159-
export function addPartitionKeyCondition<T, R extends StandardRequest<T, any, any>>(
159+
export function addPartitionKeyCondition<T, R extends StandardRequest<T, any, any, any>>(
160160
keyName: keyof T,
161161
keyValue: any,
162162
request: R,
163163
metadata: Metadata<T>,
164164
): R
165-
export function addPartitionKeyCondition<T, R extends StandardRequest<T, any, any>>(
165+
export function addPartitionKeyCondition<T, R extends StandardRequest<T, any, any, any>>(
166166
keyName: keyof T,
167167
keyValue: any,
168168
request: R,

src/dynamo/request/base.request.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ModelConstructor } from '../../model/model-constructor'
44
import { BaseRequest } from './base.request'
55

66
describe('base request', () => {
7-
class TestRequest<T> extends BaseRequest<T, any, BaseRequest<T, any, any>> {
7+
class TestRequest<T> extends BaseRequest<T, T, any, BaseRequest<T, T, any, any>> {
88
constructor(modelClazz: ModelConstructor<T>) {
99
super(<any>null, modelClazz)
1010
}

src/dynamo/request/base.request.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getTableName } from '../get-table-name.function'
1515
*/
1616
export abstract class BaseRequest<
1717
T,
18+
T2,
1819
I extends
1920
| DynamoDB.DeleteItemInput
2021
| DynamoDB.GetItemInput
@@ -26,7 +27,7 @@ export abstract class BaseRequest<
2627
| DynamoDB.BatchWriteItemInput
2728
| DynamoDB.TransactGetItemsInput
2829
| DynamoDB.TransactWriteItemsInput,
29-
R extends BaseRequest<T, I, any>
30+
R extends BaseRequest<T, T2, I, any>
3031
> {
3132
readonly dynamoDBWrapper: DynamoDbWrapper
3233

@@ -86,5 +87,5 @@ export abstract class BaseRequest<
8687
/**
8788
* execute request and return the parsed item(s) or void if none were requested.
8889
*/
89-
abstract exec(): Promise<T | T[] | void | null>
90+
abstract exec(): Promise<T2 | T2[] | void | null>
9091
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,25 @@ describe('batch get', () => {
6060
})
6161
})
6262

63-
it('ConsistentRead', () => {
63+
it('consistent read', () => {
6464
const request = new BatchGetSingleTableRequest<any>(<any>null, SimpleWithPartitionKeyModel, [{ id: 'myId' }])
6565
request.consistentRead()
6666
expect(request.params.RequestItems).toBeDefined()
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('age')
74+
expect(request.params.RequestItems).toBeDefined()
75+
const params = request.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)]
76+
expect(params).toBeDefined()
77+
expect(params.ProjectionExpression).toBe('#age')
78+
expect(params.ExpressionAttributeNames).toEqual({
79+
'#age': 'age',
80+
})
81+
})
7082
})
7183

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

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ 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'
1414
import { BaseRequest } from '../base.request'
15+
import { addProjectionExpressionParam } from '../helper/add-projection-expression-param.function'
1516
import { BatchGetSingleTableResponse } from './batch-get-single-table.response'
1617

1718
/**
1819
* Request class for BatchGetItem operation which supports a single model class only.
1920
*/
20-
export class BatchGetSingleTableRequest<T> extends BaseRequest<
21+
export class BatchGetSingleTableRequest<T, T2 = T> extends BaseRequest<
2122
T,
23+
T2,
2224
DynamoDB.BatchGetItemInput,
23-
BatchGetSingleTableRequest<T>
25+
BatchGetSingleTableRequest<T, T2>
2426
> {
2527
private readonly logger: Logger
2628

@@ -39,11 +41,23 @@ export class BatchGetSingleTableRequest<T> extends BaseRequest<
3941
}
4042
}
4143

42-
consistentRead(value: boolean = true): BatchGetSingleTableRequest<T> {
44+
/**
45+
* Determines the read consistency model: If set to true, then the operation uses strongly consistent reads; otherwise, the operation uses eventually consistent reads.
46+
*/
47+
consistentRead(value: boolean = true): this {
4348
this.params.RequestItems[this.tableName].ConsistentRead = value
4449
return this
4550
}
4651

52+
/**
53+
* Specifies the list of model attributes to be returned from the table instead of returning the entire document
54+
* @param attributesToGet List of model attributes to be returned
55+
*/
56+
projectionExpression(...attributesToGet: Array<keyof T | string>): BatchGetSingleTableRequest<T, Partial<T>> {
57+
addProjectionExpressionParam(attributesToGet, this.params.RequestItems[this.tableName], this.metadata)
58+
return this
59+
}
60+
4761
/**
4862
* fetch all entries and return the raw response (without parsing the attributes to js objects)
4963
* @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
@@ -64,7 +78,7 @@ export class BatchGetSingleTableRequest<T> extends BaseRequest<
6478
execFullResponse(
6579
backoffTimer = randomExponentialBackoffTimer,
6680
throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT,
67-
): Promise<BatchGetSingleTableResponse<T>> {
81+
): Promise<BatchGetSingleTableResponse<T2>> {
6882
return this.fetch(backoffTimer, throttleTimeSlot)
6983
.then(this.mapResponse)
7084
.then(promiseTap(response => this.logger.debug('mapped items', response.Items)))
@@ -75,18 +89,18 @@ export class BatchGetSingleTableRequest<T> extends BaseRequest<
7589
* @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
7690
* @param throttleTimeSlot the duration of a time slot in ms
7791
*/
78-
exec(backoffTimer = randomExponentialBackoffTimer, throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT): Promise<T[]> {
92+
exec(backoffTimer = randomExponentialBackoffTimer, throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT): Promise<T2[]> {
7993
return this.fetch(backoffTimer, throttleTimeSlot)
8094
.then(this.mapResponse)
8195
.then(r => r.Items)
8296
.then(promiseTap(items => this.logger.debug('mapped items', items)))
8397
}
8498

8599
private mapResponse = (response: DynamoDB.BatchGetItemOutput) => {
86-
let items: T[] = []
100+
let items: T2[] = []
87101
if (response.Responses && Object.keys(response.Responses).length && response.Responses[this.tableName]) {
88-
const mapped: T[] = response.Responses[this.tableName].map(attributeMap =>
89-
fromDb(<Attributes<T>>attributeMap, this.modelClazz),
102+
const mapped: T2[] = response.Responses[this.tableName].map(attributeMap =>
103+
fromDb(<Attributes<T2>>attributeMap, <any>this.modelClazz),
90104
)
91105
items = mapped
92106
}

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/batchwritesingletable/batch-write-single-table.request.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import { BaseRequest } from '../base.request'
1414
/**
1515
* Request class for BatchWriteItem operation which supports a single model class only.
1616
*/
17-
export class BatchWriteSingleTableRequest<T> extends BaseRequest<
17+
export class BatchWriteSingleTableRequest<T, T2 = T> extends BaseRequest<
1818
T,
19+
T2,
1920
DynamoDB.BatchWriteItemInput,
20-
BatchWriteSingleTableRequest<T>
21+
BatchWriteSingleTableRequest<T, T2>
2122
> {
2223
private readonly logger: Logger
2324
private toKey = createToKeyFn(this.modelClazz)
@@ -33,19 +34,20 @@ export class BatchWriteSingleTableRequest<T> extends BaseRequest<
3334
/**
3435
* return item collection metrics.
3536
*/
36-
returnItemCollectionMetrics(value: DynamoDB.ReturnItemCollectionMetrics) {
37+
returnItemCollectionMetrics(value: DynamoDB.ReturnItemCollectionMetrics): this {
3738
this.params.ReturnItemCollectionMetrics = value
39+
return this
3840
}
3941

40-
delete(items: Array<Partial<T>>): BatchWriteSingleTableRequest<T> {
42+
delete(items: Array<Partial<T>>): this {
4143
if (this.params.RequestItems[this.tableName].length + items.length > BATCH_WRITE_MAX_REQUEST_ITEM_COUNT) {
4244
throw new Error(`batch write takes at max ${BATCH_WRITE_MAX_REQUEST_ITEM_COUNT} items`)
4345
}
4446
this.params.RequestItems[this.tableName].push(...items.map(this.createDeleteRequest))
4547
return this
4648
}
4749

48-
put(items: T[]): BatchWriteSingleTableRequest<T> {
50+
put(items: T[]): this {
4951
if (this.params.RequestItems[this.tableName].length + items.length > BATCH_WRITE_MAX_REQUEST_ITEM_COUNT) {
5052
throw new Error(`batch write takes at max ${BATCH_WRITE_MAX_REQUEST_ITEM_COUNT} items`)
5153
}
1.85 KB
Binary file not shown.

0 commit comments

Comments
 (0)