1
1
import * as https from 'https' ;
2
2
import { env } from 'process' ;
3
- import { DynamoDB } from 'aws-sdk' ;
3
+ import { DynamoDBClient } from '@aws-sdk/client-dynamodb' ;
4
+ import * as dynamodb from '@aws-sdk/lib-dynamodb' ;
5
+ import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb' ;
6
+ import { NodeHttpHandler } from '@aws-sdk/node-http-handler' ;
4
7
import { PrimaryEntity } from './model' ;
5
8
6
9
const agent = new https . Agent ( {
7
10
keepAlive : true ,
8
11
} ) ;
9
- export const dynamoClient : DynamoDB . DocumentClient = new DynamoDB . DocumentClient ( { httpOptions : { agent } } ) ;
12
+ export const dynamoClient : DynamoDBDocumentClient = DynamoDBDocumentClient . from ( new DynamoDBClient ( {
13
+ requestHandler : new NodeHttpHandler ( { httpsAgent : agent } ) ,
14
+ } ) ) ;
10
15
11
16
export const TABLE_NAME : string = env . TABLE ! ;
12
17
13
- export async function getItem < E extends PrimaryEntity < any , any > > ( pk : E [ 'PK' ] , sk : E [ 'SK' ] , options ?: Omit < DynamoDB . DocumentClient . GetItemInput , 'TableName' | 'Key' > ) : Promise < E | undefined > {
14
- const res = await dynamoClient . get ( {
18
+ export async function getItem < E extends PrimaryEntity < any , any > > ( pk : E [ 'PK' ] , sk : E [ 'SK' ] , options ?: Omit < dynamodb . GetCommandInput , 'TableName' | 'Key' > ) : Promise < E | undefined > {
19
+ const res = await dynamoClient . send ( new dynamodb . GetCommand ( {
15
20
TableName : TABLE_NAME ,
16
21
Key : {
17
22
PK : pk ,
18
23
SK : sk ,
19
24
} ,
20
25
...options ,
21
- } ) . promise ( ) ;
26
+ } ) ) ;
22
27
return res . Item ? res . Item as E : undefined ;
23
28
}
24
29
25
- export async function deleteItem < E extends PrimaryEntity < any , any > > ( pk : E [ 'PK' ] , sk : E [ 'SK' ] , options ?: Omit < DynamoDB . DocumentClient . DeleteItemInput , 'TableName' | 'Key' > ) : Promise < void > {
26
- await dynamoClient . delete ( {
30
+ export async function deleteItem < E extends PrimaryEntity < any , any > > ( pk : E [ 'PK' ] , sk : E [ 'SK' ] , options ?: Omit < dynamodb . DeleteCommandInput , 'TableName' | 'Key' > ) : Promise < void > {
31
+ await dynamoClient . send ( new dynamodb . DeleteCommand ( {
27
32
TableName : TABLE_NAME ,
28
33
Key : {
29
34
PK : pk ,
30
35
SK : sk ,
31
36
} ,
32
37
...options ,
33
- } ) . promise ( ) ;
38
+ } ) ) ;
34
39
}
35
40
36
41
export async function putNewItem < E extends PrimaryEntity < any , any > > ( pk : E [ 'PK' ] , sk : E [ 'SK' ] , item : Omit < E , 'PK' | 'SK' > ) : Promise < E > {
@@ -39,35 +44,35 @@ export async function putNewItem<E extends PrimaryEntity<any, any>>(pk: E['PK'],
39
44
SK : sk ,
40
45
...item ,
41
46
} ;
42
- await dynamoClient . put ( {
47
+ await dynamoClient . send ( new dynamodb . PutCommand ( {
43
48
TableName : TABLE_NAME ,
44
49
Item,
45
50
ConditionExpression : 'attribute_not_exists(PK) and attribute_not_exists(SK)' ,
46
- } ) . promise ( ) ;
51
+ } ) ) ;
47
52
return Item as E ;
48
53
}
49
54
50
55
export async function updateExistingItem < E extends PrimaryEntity < any , any > > ( pk : E [ 'PK' ] , sk : E [ 'SK' ] , item : Partial < E > ) : Promise < E | undefined > {
51
- const res = await dynamoClient . update ( createUpdate < E > ( {
56
+ const res = await dynamoClient . send ( createUpdate < E > ( {
52
57
Key : {
53
58
PK : pk ,
54
59
SK : sk ,
55
60
} ,
56
61
ConditionExpression : 'attribute_exists(PK) and attribute_exists(SK)' ,
57
62
ReturnValues : 'ALL_NEW' ,
58
- } , item ) ) . promise ( ) ;
63
+ } , item ) ) ;
59
64
return res . Attributes ? res . Attributes as E : undefined ;
60
65
}
61
66
62
- export async function pagedQuery < T > ( query : Omit < DynamoDB . DocumentClient . QueryInput , 'TableName' > ) : Promise < T [ ] > {
67
+ export async function pagedQuery < T > ( query : Omit < dynamodb . QueryCommandInput , 'TableName' > ) : Promise < T [ ] > {
63
68
let startKey ;
64
69
const result : T [ ] = [ ] ;
65
70
do {
66
- const res : DynamoDB . DocumentClient . QueryOutput = await dynamoClient . query ( {
71
+ const res : dynamodb . QueryCommandOutput = await dynamoClient . send ( new dynamodb . QueryCommand ( {
67
72
...query ,
68
73
TableName : TABLE_NAME ,
69
74
ExclusiveStartKey : startKey ,
70
- } ) . promise ( ) ;
75
+ } ) ) ;
71
76
if ( res . Items ) {
72
77
result . push ( ...res . Items as T [ ] ) ;
73
78
}
@@ -76,15 +81,15 @@ export async function pagedQuery<T>(query: Omit<DynamoDB.DocumentClient.QueryInp
76
81
return result ;
77
82
}
78
83
79
- export async function pagedScan < T > ( query : Omit < DynamoDB . DocumentClient . ScanInput , 'TableName' > ) : Promise < T [ ] > {
84
+ export async function pagedScan < T > ( query : Omit < dynamodb . ScanCommandInput , 'TableName' > ) : Promise < T [ ] > {
80
85
let startKey ;
81
86
const result : T [ ] = [ ] ;
82
87
do {
83
- const res : DynamoDB . DocumentClient . ScanOutput = await dynamoClient . scan ( {
88
+ const res : dynamodb . ScanCommandOutput = await dynamoClient . send ( new dynamodb . ScanCommand ( {
84
89
...query ,
85
90
TableName : TABLE_NAME ,
86
91
ExclusiveStartKey : startKey ,
87
- } ) . promise ( ) ;
92
+ } ) ) ;
88
93
if ( res . Items ) {
89
94
result . push ( ...res . Items as T [ ] ) ;
90
95
}
@@ -101,7 +106,7 @@ export function padLeftZeros(val: number | string | undefined) {
101
106
return ( '00' + val ) . slice ( - 2 ) ;
102
107
}
103
108
104
- export function createUpdate < T > ( request : Omit < DynamoDB . DocumentClient . UpdateItemInput , 'TableName' > , data : Partial < T > ) : DynamoDB . DocumentClient . UpdateItemInput {
109
+ export function createUpdate < T > ( request : Omit < dynamodb . UpdateCommandInput , 'TableName' > , data : Partial < T > ) : dynamodb . UpdateCommand {
105
110
const fieldsToSet = [ ] ;
106
111
const fieldsToRemove = [ ] ;
107
112
const expressionNames : any = { } ;
@@ -129,7 +134,7 @@ export function createUpdate<T>(request: Omit<DynamoDB.DocumentClient.UpdateItem
129
134
if ( request . UpdateExpression ) {
130
135
update += request . UpdateExpression ;
131
136
}
132
- return {
137
+ return new dynamodb . UpdateCommand ( {
133
138
...request ,
134
139
TableName : TABLE_NAME ,
135
140
UpdateExpression : update ,
@@ -147,5 +152,5 @@ export function createUpdate<T>(request: Omit<DynamoDB.DocumentClient.UpdateItem
147
152
...expressionValues ,
148
153
} ,
149
154
} ,
150
- } ;
155
+ } ) ;
151
156
}
0 commit comments