Skip to content

Commit 80e3560

Browse files
Merge pull request #195 from shiftcode/#194-possibility-to-provide-DynamoDB-instance
feat(dynamo-store): use provided DynamoDB instance if any
2 parents 40f525e + 067347a commit 80e3560

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/dynamo/dynamo-db-wrapper.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// tslint:disable:no-empty
22
// tslint:disable:no-unnecessary-callback-wrapper
33

4-
import { Config, Credentials } from 'aws-sdk/global'
4+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
55
import { resetDynamoEasyConfig } from '../../test/helper/resetDynamoEasyConfig.function'
66
import { updateDynamoEasyConfig } from '../config/update-config.function'
77
import { DynamoDbWrapper } from './dynamo-db-wrapper'
@@ -130,10 +130,12 @@ describe('dynamo rx', () => {
130130
expect(makeRequest.calls.mostRecent().args[0]).toEqual({ ok: true })
131131
})
132132

133-
it('should update the credentials', () => {
134-
const dynamoDBWrapper = new DynamoDbWrapper()
135-
const credentials = new Credentials({ secretAccessKey: '', sessionToken: '', accessKeyId: '' })
136-
dynamoDBWrapper.updateAwsConfigCredentials(new Config({ credentials }))
137-
expect(dynamoDBWrapper.dynamoDB.config.credentials).toBe(credentials)
133+
it('should use given dynamoDB client', () => {
134+
const dynamoDB = new DynamoDB()
135+
const dynamoDBWrapper = new DynamoDbWrapper(dynamoDB)
136+
expect(dynamoDBWrapper.dynamoDB).toBe(dynamoDB)
137+
138+
const dynamoDBWrapper2 = new DynamoDbWrapper()
139+
expect(dynamoDBWrapper2.dynamoDB).not.toBe(dynamoDB)
138140
})
139141
})

src/dynamo/dynamo-db-wrapper.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @module dynamo-easy
33
*/
44
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
5-
import { Config } from 'aws-sdk/global'
65
import { dynamoEasyConfig } from '../config/dynamo-easy-config'
76

87
/**
@@ -13,13 +12,9 @@ import { dynamoEasyConfig } from '../config/dynamo-easy-config'
1312
export class DynamoDbWrapper {
1413
readonly dynamoDB: DynamoDB
1514

16-
constructor() {
15+
constructor(dynamoDB?: DynamoDB) {
1716
// create the actual dynamoDB client
18-
this.dynamoDB = new DynamoDB()
19-
}
20-
21-
updateAwsConfigCredentials(newConfig: Config): void {
22-
this.dynamoDB.config.update({ credentials: newConfig.credentials })
17+
this.dynamoDB = dynamoDB || new DynamoDB()
2318
}
2419

2520
/*

src/dynamo/dynamo-store.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tslint:disable:max-classes-per-file
22
// tslint:disable:no-unnecessary-class
33
// tslint:disable:no-unused-expression
4+
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
45
import { resetDynamoEasyConfig } from '../../test/helper/resetDynamoEasyConfig.function'
56
import { SimpleWithPartitionKeyModel } from '../../test/models'
67
import { updateDynamoEasyConfig } from '../config/update-config.function'
@@ -95,4 +96,13 @@ describe('dynamo store', () => {
9596
const store = new DynamoStore(SimpleWithPartitionKeyModel)
9697
expect(store.dynamoDB).toBeDefined()
9798
})
99+
100+
describe('use provided dynamoDB instance', () => {
101+
const dynamoDB = new DynamoDB()
102+
const store = new DynamoStore(SimpleWithPartitionKeyModel, dynamoDB)
103+
expect(store.dynamoDB).toBe(dynamoDB)
104+
105+
const store2 = new DynamoStore(SimpleWithPartitionKeyModel)
106+
expect(store2.dynamoDB).not.toBe(dynamoDB)
107+
})
98108
})

src/dynamo/dynamo-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export class DynamoStore<T> {
3030
private readonly logger: Logger
3131
private readonly dynamoDBWrapper: DynamoDbWrapper
3232

33-
constructor(private modelClazz: ModelConstructor<T>) {
33+
constructor(private modelClazz: ModelConstructor<T>, dynamoDB?: DynamoDB) {
3434
this.logger = createLogger('dynamo.DynamoStore', modelClazz)
35-
this.dynamoDBWrapper = new DynamoDbWrapper()
35+
this.dynamoDBWrapper = new DynamoDbWrapper(dynamoDB)
3636
this.tableName = getTableName(modelClazz)
3737
this.logger.debug('instance created')
3838
}

0 commit comments

Comments
 (0)