Skip to content

Commit d14811a

Browse files
author
Alexander Melnyk
committed
remove singleton for ddb client, add useragent in constructor
1 parent fe431b7 commit d14811a

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ import {
22
IdempotencyItemAlreadyExistsError,
33
IdempotencyItemNotFoundError,
44
} from '../errors';
5-
import { IdempotencyRecordStatus } from '../types';
65
import type { DynamoPersistenceOptions } from '../types';
6+
import { IdempotencyRecordStatus } from '../types';
77
import {
8+
AttributeValue,
9+
DeleteItemCommand,
810
DynamoDBClient,
911
DynamoDBClientConfig,
1012
DynamoDBServiceException,
11-
DeleteItemCommand,
1213
GetItemCommand,
1314
PutItemCommand,
1415
UpdateItemCommand,
15-
AttributeValue,
1616
} from '@aws-sdk/client-dynamodb';
1717
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
1818
import { IdempotencyRecord } from './IdempotencyRecord';
1919
import { BasePersistenceLayer } from './BasePersistenceLayer';
20+
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
2021

2122
/**
2223
* DynamoDB persistence layer for idempotency records. This class will use the AWS SDK V3 to write and read idempotency records from DynamoDB.
@@ -28,7 +29,7 @@ import { BasePersistenceLayer } from './BasePersistenceLayer';
2829
* @implements {BasePersistenceLayer}
2930
*/
3031
class DynamoDBPersistenceLayer extends BasePersistenceLayer {
31-
private client?: DynamoDBClient;
32+
private client!: DynamoDBClient;
3233
private clientConfig: DynamoDBClientConfig = {};
3334
private dataAttr: string;
3435
private expiryAttr: string;
@@ -64,18 +65,18 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
6465
if (config?.awsSdkV3Client instanceof DynamoDBClient) {
6566
this.client = config.awsSdkV3Client;
6667
} else {
67-
console.warn(
68-
'Invalid AWS SDK V3 client passed to DynamoDBPersistenceLayer. Using default client.'
69-
);
68+
throw Error('Not valid DynamoDBClient provided.');
7069
}
7170
} else {
7271
this.clientConfig = config?.clientConfig ?? {};
72+
this.client = new DynamoDBClient(this.clientConfig);
7373
}
74+
75+
addUserAgentMiddleware(this.client, 'idempotency');
7476
}
7577

7678
protected async _deleteRecord(record: IdempotencyRecord): Promise<void> {
77-
const client = this.getClient();
78-
await client.send(
79+
await this.client.send(
7980
new DeleteItemCommand({
8081
TableName: this.tableName,
8182
Key: this.getKey(record.idempotencyKey),
@@ -86,8 +87,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
8687
protected async _getRecord(
8788
idempotencyKey: string
8889
): Promise<IdempotencyRecord> {
89-
const client = this.getClient();
90-
const result = await client.send(
90+
const result = await this.client.send(
9191
new GetItemCommand({
9292
TableName: this.tableName,
9393
Key: this.getKey(idempotencyKey),
@@ -111,8 +111,6 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
111111
}
112112

113113
protected async _putRecord(record: IdempotencyRecord): Promise<void> {
114-
const client = this.getClient();
115-
116114
const item = {
117115
...this.getKey(record.idempotencyKey),
118116
...marshall({
@@ -163,7 +161,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
163161
].join(' OR ');
164162

165163
const now = Date.now();
166-
await client.send(
164+
await this.client.send(
167165
new PutItemCommand({
168166
TableName: this.tableName,
169167
Item: item,
@@ -195,8 +193,6 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
195193
}
196194

197195
protected async _updateRecord(record: IdempotencyRecord): Promise<void> {
198-
const client = this.getClient();
199-
200196
const updateExpressionFields: string[] = [
201197
'#response_data = :response_data',
202198
'#expiry = :expiry',
@@ -219,7 +215,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
219215
expressionAttributeValues[':validation_key'] = record.payloadHash;
220216
}
221217

222-
await client.send(
218+
await this.client.send(
223219
new UpdateItemCommand({
224220
TableName: this.tableName,
225221
Key: this.getKey(record.idempotencyKey),
@@ -230,14 +226,6 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
230226
);
231227
}
232228

233-
private getClient(): DynamoDBClient {
234-
if (!this.client) {
235-
this.client = new DynamoDBClient(this.clientConfig);
236-
}
237-
238-
return this.client;
239-
}
240-
241229
/**
242230
* Build primary key attribute simple or composite based on params.
243231
*

packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,14 @@ describe('Class: DynamoDBPersistenceLayer', () => {
163163
);
164164
});
165165

166-
test('when passed an invalid AWS SDK client it logs a warning', () => {
167-
// Prepare
168-
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
169-
170-
// Act
171-
new TestDynamoDBPersistenceLayer({
172-
tableName: dummyTableName,
173-
awsSdkV3Client: {} as DynamoDBClient,
174-
});
175-
176-
// Assess
177-
expect(consoleWarnSpy).toHaveBeenCalledWith(
178-
'Invalid AWS SDK V3 client passed to DynamoDBPersistenceLayer. Using default client.'
179-
);
166+
test('when passed an invalid AWS SDK client, it throws an error', () => {
167+
// Act & Assess
168+
expect(() => {
169+
new TestDynamoDBPersistenceLayer({
170+
tableName: dummyTableName,
171+
awsSdkV3Client: {} as DynamoDBClient,
172+
});
173+
}).toThrow();
180174
});
181175

182176
test('when passed a client config it stores it for later use', () => {

0 commit comments

Comments
 (0)