Skip to content

Commit 1792360

Browse files
Merge pull request #142 from shiftcode/#97-unify-dynamodb-imports
#97 unify dynamodb imports
2 parents aeeb430 + cfa6031 commit 1792360

38 files changed

+208
-140
lines changed

package-lock.json

Lines changed: 12 additions & 3 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"test": "jest",
3939
"test:ci": "jest --coverage --no-cache",
4040
"test:watch": "jest --watch",
41+
"tslint:custom-rule:build": "tsc ./tools/tslint/noDynamoNamedImportRule.ts",
42+
"tslint:custom-rule:test": "tslint --test ./tools/tslint/test",
4143
"prettier": "prettier --write --config ./.prettierrc.yml '{src,test}/**/*.ts'"
4244
},
4345
"devDependencies": {
@@ -69,6 +71,7 @@
6971
"tsc-watch": "^1.0.31",
7072
"tslint": "^5.12.0",
7173
"tslint-config-prettier": "^1.17.0",
74+
"tsutils": "^3.6.0",
7275
"typedoc": "^0.13.0",
7376
"typescript": "^3.2.2",
7477
"uuid": "^3.3.2"

src/decorator/impl/index/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { KeyType } from 'aws-sdk/clients/dynamodb'
1+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
22
import { PropertyMetadata } from '../../metadata/property-metadata.model'
33
import { initOrUpdateProperty } from '../property/init-or-update-property.function'
44
import { KEY_PROPERTY } from '../property/key-property.const'
55
import { IndexType } from './index-type.enum'
66

77
export interface IndexData {
88
name: string
9-
keyType: KeyType
9+
keyType: DynamoDB.KeyType
1010
}
1111

1212
export function initOrUpdateIndex(indexType: IndexType, indexData: IndexData, target: any, propertyKey: string): void {
@@ -34,7 +34,7 @@ export function initOrUpdateIndex(indexType: IndexType, indexData: IndexData, ta
3434
initOrUpdateProperty(propertyMetadata, target, propertyKey)
3535
}
3636

37-
function initOrUpdateGSI(indexes: Record<string, KeyType>, indexData: IndexData): Partial<PropertyMetadata<any>> {
37+
function initOrUpdateGSI(indexes: Record<string, DynamoDB.KeyType>, indexData: IndexData): Partial<PropertyMetadata<any>> {
3838
if (indexes[indexData.name]) {
3939
// TODO LOW:INVESTIGATE when we throw an error we have a problem where multiple different classes extend one base class, this will be executed by multiple times
4040
// throw new Error(

src/decorator/impl/model/model.decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { KeyType } from 'aws-sdk/clients/dynamodb'
1+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
22
import { kebabCase } from 'lodash'
33
import { ModelMetadata } from '../../metadata'
44
import { PropertyMetadata } from '../../metadata/property-metadata.model'
@@ -57,7 +57,7 @@ export function Model(opts: ModelData = {}): ClassDecorator {
5757

5858
function testForGSI<T>(
5959
property: PropertyMetadata<T>,
60-
): property is PropertyMetadata<T> & { keyForGSI: Record<string, KeyType> } {
60+
): property is PropertyMetadata<T> & { keyForGSI: Record<string, DynamoDB.KeyType> } {
6161
return !!(property.keyForGSI && Object.keys(property.keyForGSI).length)
6262
}
6363

src/decorator/metadata/property-metadata.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MapperForType } from '../../mapper/for-type/base.mapper'
22

33
// def good
4-
import { KeyType } from 'aws-sdk/clients/dynamodb'
4+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
55
import { Attribute } from '../../mapper/type/attribute.type'
66
import { ModelConstructor } from '../../model/model-constructor'
77

@@ -13,7 +13,7 @@ export interface TypeInfo {
1313
}
1414

1515
export interface Key {
16-
type: KeyType
16+
type: DynamoDB.KeyType
1717
uuid?: boolean
1818
}
1919

@@ -41,7 +41,7 @@ export interface PropertyMetadata<T, R extends Attribute = Attribute> {
4141
mapper?: () => MapperForType<any, R>
4242

4343
// maps the index name to the key type to describe for which GSI this property describes a key attribute
44-
keyForGSI?: Record<string, KeyType>
44+
keyForGSI?: Record<string, DynamoDB.KeyType>
4545

4646
// holds all the the index names for which this property describes the sort key attribute
4747
sortKeyForLSI?: string[]
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// tslint:disable-next-line:interface-over-type-literal
2-
import { BatchGetRequestMap, ConsumedCapacityMultiple } from 'aws-sdk/clients/dynamodb'
2+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
33
import { BatchGetResponse } from './batch-get.response'
44

55
export interface BatchGetFullResponse {
@@ -10,9 +10,9 @@ export interface BatchGetFullResponse {
1010
/**
1111
* A map of tables and their respective keys that were not processed with the current response. The UnprocessedKeys value is in the same form as RequestItems, so the value can be provided directly to a subsequent BatchGetItem operation. For more information, see RequestItems in the Request Parameters section. Each element consists of: Keys - An array of primary key attribute values that define specific items in the table. ProjectionExpression - One or more attributes to be retrieved from the table or index. By default, all attributes are returned. If a requested attribute is not found, it does not appear in the result. ConsistentRead - The consistency of a read operation. If set to true, then a strongly consistent read is used; otherwise, an eventually consistent read is used. If there are no unprocessed keys remaining, the response contains an empty UnprocessedKeys map.
1212
*/
13-
UnprocessedKeys?: BatchGetRequestMap
13+
UnprocessedKeys?: DynamoDB.BatchGetRequestMap
1414
/**
1515
* The read capacity units consumed by the entire BatchGetItem operation. Each element consists of: TableName - The table that consumed the provisioned throughput. CapacityUnits - The total number of capacity units consumed.
1616
*/
17-
ConsumedCapacity?: ConsumedCapacityMultiple
17+
ConsumedCapacity?: DynamoDB.ConsumedCapacityMultiple
1818
}
Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
import { DynamoDB } from 'aws-sdk'
1+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
22
import { of } from 'rxjs'
33
import { DynamoRx } from '../dynamo-rx'
44
import { batchGetItemsFetchAll, combineBatchGetResponses, hasUnprocessedKeys } from './batch-get-utils'
55

66
describe('batch-get utils', () => {
7-
87
describe('hasUnprocessedKeys', () => {
98
it('should return bool according to given object', () => {
109
expect(hasUnprocessedKeys({})).toBeFalsy()
1110
expect(hasUnprocessedKeys({ Responses: {} })).toBeFalsy()
1211
expect(hasUnprocessedKeys({ UnprocessedKeys: {} })).toBeFalsy()
13-
expect(hasUnprocessedKeys({ UnprocessedKeys: { 'aTableName': { Keys: [] } } })).toBeFalsy()
14-
expect(hasUnprocessedKeys({ UnprocessedKeys: { 'aTableName': { Keys: [{ id: { S: 'id' } }] } } })).toBeTruthy()
12+
expect(hasUnprocessedKeys({ UnprocessedKeys: { aTableName: { Keys: [] } } })).toBeFalsy()
13+
expect(hasUnprocessedKeys({ UnprocessedKeys: { aTableName: { Keys: [{ id: { S: 'id' } }] } } })).toBeTruthy()
1514
})
1615
})
1716

1817
describe('combineBatchGetResponses', () => {
1918
const resp1: DynamoDB.BatchGetItemOutput = {
2019
Responses: {
21-
'tableA': [
22-
{ id: { S: 'id-a1' } },
23-
],
24-
'tableB': [
25-
{ id: { S: 'id-b' } },
26-
],
20+
tableA: [{ id: { S: 'id-a1' } }],
21+
tableB: [{ id: { S: 'id-b' } }],
2722
},
2823
UnprocessedKeys: {
2924
'tableA:': { Keys: [{ id: { S: 'id-a2' } }] },
@@ -33,29 +28,18 @@ describe('batch-get utils', () => {
3328
}
3429
const resp2: DynamoDB.BatchGetItemOutput = {
3530
Responses: {
36-
'tableA': [
37-
{ id: { S: 'id-a2' } },
38-
],
39-
'tableC': [
40-
{ id: { S: 'id-c' } },
41-
],
31+
tableA: [{ id: { S: 'id-a2' } }],
32+
tableC: [{ id: { S: 'id-c' } }],
4233
},
4334
UnprocessedKeys: {
4435
'tableD:': { Keys: [{ id: { S: 'id-d' } }] },
4536
},
4637
}
4738
const expectedOutput: DynamoDB.BatchGetItemOutput = {
4839
Responses: {
49-
'tableA': [
50-
{ id: { S: 'id-a1' } },
51-
{ id: { S: 'id-a2' } },
52-
],
53-
'tableB': [
54-
{ id: { S: 'id-b' } },
55-
],
56-
'tableC': [
57-
{ id: { S: 'id-c' } },
58-
],
40+
tableA: [{ id: { S: 'id-a1' } }, { id: { S: 'id-a2' } }],
41+
tableB: [{ id: { S: 'id-b' } }],
42+
tableC: [{ id: { S: 'id-c' } }],
5943
},
6044
UnprocessedKeys: {
6145
'tableD:': { Keys: [{ id: { S: 'id-d' } }] },
@@ -64,7 +48,6 @@ describe('batch-get utils', () => {
6448
it('should combine correctly', () => {
6549
expect(combineBatchGetResponses(resp1)(resp2)).toEqual(expectedOutput)
6650
})
67-
6851
})
6952

7053
describe('batchGetItemsFetchAll', () => {
@@ -74,17 +57,17 @@ describe('batch-get utils', () => {
7457

7558
const output1: DynamoDB.BatchGetItemOutput = {
7659
Responses: {
77-
'tableA': [{ id: { S: 'id-A' } }],
60+
tableA: [{ id: { S: 'id-A' } }],
7861
},
7962
UnprocessedKeys: {
80-
'tableA': {
63+
tableA: {
8164
Keys: [{ id: { S: 'id-A' } }],
8265
},
8366
},
8467
}
8568
const output2: DynamoDB.BatchGetItemOutput = {
8669
Responses: {
87-
'tableA': [{ id: { S: 'id-A' } }],
70+
tableA: [{ id: { S: 'id-A' } }],
8871
},
8972
}
9073

@@ -93,15 +76,9 @@ describe('batch-get utils', () => {
9376
dynamoRx = <any>{ batchGetItems: batchGetItemsSpy }
9477
backoffTimerMock = { next: jasmine.createSpy().and.returnValue({ value: 0 }) }
9578

96-
await batchGetItemsFetchAll(
97-
dynamoRx,
98-
<any>{},
99-
<IterableIterator<number>><any>backoffTimerMock,
100-
0,
101-
).toPromise()
79+
await batchGetItemsFetchAll(dynamoRx, <any>{}, <IterableIterator<number>>(<any>backoffTimerMock), 0).toPromise()
10280
})
10381

104-
10582
it('should use UnprocessedKeys for next request', () => {
10683
expect(batchGetItemsSpy).toHaveBeenCalledTimes(2)
10784
expect(batchGetItemsSpy.calls.mostRecent().args[0]).toBeDefined()
@@ -112,7 +89,5 @@ describe('batch-get utils', () => {
11289
it('should backoff when UnprocessedItems', () => {
11390
expect(backoffTimerMock.next).toHaveBeenCalledTimes(1)
11491
})
115-
11692
})
117-
11893
})

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { DynamoDB } from 'aws-sdk'
2-
import { BatchGetRequestMap } from 'aws-sdk/clients/dynamodb'
1+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
32
import { Observable, of } from 'rxjs'
43
import { delay, map, mergeMap } from 'rxjs/operators'
54
import { DynamoRx } from '../dynamo-rx'
@@ -44,7 +43,7 @@ export function batchGetItemsFetchAll(
4443

4544
export type BatchGetItemOutputWithUnprocessedKeys =
4645
DynamoDB.BatchGetItemOutput
47-
& { UnprocessedKeys: BatchGetRequestMap }
46+
& { UnprocessedKeys: DynamoDB.BatchGetRequestMap }
4847

4948
export function hasUnprocessedKeys(response: DynamoDB.BatchGetItemOutput): response is BatchGetItemOutputWithUnprocessedKeys {
5049
if (!response.UnprocessedKeys) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DynamoDB } from 'aws-sdk'
1+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
22
import { Observable } from 'rxjs'
33
import { map } from 'rxjs/operators'
44
import { metadataForClass } from '../../decorator/metadata/metadata-helper'

src/dynamo/batchwrite/batch-write-utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DynamoDB } from 'aws-sdk'
1+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
22
import { of } from 'rxjs'
33
import { DynamoRx } from '../dynamo-rx'
44
import { batchWriteItemsWriteAll, hasUnprocessedItems } from './batch-write-utils'

0 commit comments

Comments
 (0)