@@ -2,21 +2,22 @@ import {
2
2
IdempotencyItemAlreadyExistsError ,
3
3
IdempotencyItemNotFoundError ,
4
4
} from '../errors' ;
5
- import { IdempotencyRecordStatus } from '../types' ;
6
5
import type { DynamoPersistenceOptions } from '../types' ;
6
+ import { IdempotencyRecordStatus } from '../types' ;
7
7
import {
8
+ AttributeValue ,
9
+ DeleteItemCommand ,
8
10
DynamoDBClient ,
9
11
DynamoDBClientConfig ,
10
12
DynamoDBServiceException ,
11
- DeleteItemCommand ,
12
13
GetItemCommand ,
13
14
PutItemCommand ,
14
15
UpdateItemCommand ,
15
- AttributeValue ,
16
16
} from '@aws-sdk/client-dynamodb' ;
17
17
import { marshall , unmarshall } from '@aws-sdk/util-dynamodb' ;
18
18
import { IdempotencyRecord } from './IdempotencyRecord' ;
19
19
import { BasePersistenceLayer } from './BasePersistenceLayer' ;
20
+ import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons' ;
20
21
21
22
/**
22
23
* 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';
28
29
* @implements {BasePersistenceLayer}
29
30
*/
30
31
class DynamoDBPersistenceLayer extends BasePersistenceLayer {
31
- private client ? : DynamoDBClient ;
32
+ private client ! : DynamoDBClient ;
32
33
private clientConfig : DynamoDBClientConfig = { } ;
33
34
private dataAttr : string ;
34
35
private expiryAttr : string ;
@@ -64,18 +65,18 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
64
65
if ( config ?. awsSdkV3Client instanceof DynamoDBClient ) {
65
66
this . client = config . awsSdkV3Client ;
66
67
} else {
67
- console . warn (
68
- 'Invalid AWS SDK V3 client passed to DynamoDBPersistenceLayer. Using default client.'
69
- ) ;
68
+ throw Error ( 'Not valid DynamoDBClient provided.' ) ;
70
69
}
71
70
} else {
72
71
this . clientConfig = config ?. clientConfig ?? { } ;
72
+ this . client = new DynamoDBClient ( this . clientConfig ) ;
73
73
}
74
+
75
+ addUserAgentMiddleware ( this . client , 'idempotency' ) ;
74
76
}
75
77
76
78
protected async _deleteRecord ( record : IdempotencyRecord ) : Promise < void > {
77
- const client = this . getClient ( ) ;
78
- await client . send (
79
+ await this . client . send (
79
80
new DeleteItemCommand ( {
80
81
TableName : this . tableName ,
81
82
Key : this . getKey ( record . idempotencyKey ) ,
@@ -86,8 +87,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
86
87
protected async _getRecord (
87
88
idempotencyKey : string
88
89
) : Promise < IdempotencyRecord > {
89
- const client = this . getClient ( ) ;
90
- const result = await client . send (
90
+ const result = await this . client . send (
91
91
new GetItemCommand ( {
92
92
TableName : this . tableName ,
93
93
Key : this . getKey ( idempotencyKey ) ,
@@ -111,8 +111,6 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
111
111
}
112
112
113
113
protected async _putRecord ( record : IdempotencyRecord ) : Promise < void > {
114
- const client = this . getClient ( ) ;
115
-
116
114
const item = {
117
115
...this . getKey ( record . idempotencyKey ) ,
118
116
...marshall ( {
@@ -163,7 +161,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
163
161
] . join ( ' OR ' ) ;
164
162
165
163
const now = Date . now ( ) ;
166
- await client . send (
164
+ await this . client . send (
167
165
new PutItemCommand ( {
168
166
TableName : this . tableName ,
169
167
Item : item ,
@@ -195,8 +193,6 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
195
193
}
196
194
197
195
protected async _updateRecord ( record : IdempotencyRecord ) : Promise < void > {
198
- const client = this . getClient ( ) ;
199
-
200
196
const updateExpressionFields : string [ ] = [
201
197
'#response_data = :response_data' ,
202
198
'#expiry = :expiry' ,
@@ -219,7 +215,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
219
215
expressionAttributeValues [ ':validation_key' ] = record . payloadHash ;
220
216
}
221
217
222
- await client . send (
218
+ await this . client . send (
223
219
new UpdateItemCommand ( {
224
220
TableName : this . tableName ,
225
221
Key : this . getKey ( record . idempotencyKey ) ,
@@ -230,14 +226,6 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
230
226
) ;
231
227
}
232
228
233
- private getClient ( ) : DynamoDBClient {
234
- if ( ! this . client ) {
235
- this . client = new DynamoDBClient ( this . clientConfig ) ;
236
- }
237
-
238
- return this . client ;
239
- }
240
-
241
229
/**
242
230
* Build primary key attribute simple or composite based on params.
243
231
*
0 commit comments