11import * as https from 'https' ;
22import { 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' ;
47import { PrimaryEntity } from './model' ;
58
69const agent = new https . Agent ( {
710 keepAlive : true ,
811} ) ;
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+ } ) ) ;
1015
1116export const TABLE_NAME : string = env . TABLE ! ;
1217
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 ( {
1520 TableName : TABLE_NAME ,
1621 Key : {
1722 PK : pk ,
1823 SK : sk ,
1924 } ,
2025 ...options ,
21- } ) . promise ( ) ;
26+ } ) ) ;
2227 return res . Item ? res . Item as E : undefined ;
2328}
2429
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 ( {
2732 TableName : TABLE_NAME ,
2833 Key : {
2934 PK : pk ,
3035 SK : sk ,
3136 } ,
3237 ...options ,
33- } ) . promise ( ) ;
38+ } ) ) ;
3439}
3540
3641export 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'],
3944 SK : sk ,
4045 ...item ,
4146 } ;
42- await dynamoClient . put ( {
47+ await dynamoClient . send ( new dynamodb . PutCommand ( {
4348 TableName : TABLE_NAME ,
4449 Item,
4550 ConditionExpression : 'attribute_not_exists(PK) and attribute_not_exists(SK)' ,
46- } ) . promise ( ) ;
51+ } ) ) ;
4752 return Item as E ;
4853}
4954
5055export 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 > ( {
5257 Key : {
5358 PK : pk ,
5459 SK : sk ,
5560 } ,
5661 ConditionExpression : 'attribute_exists(PK) and attribute_exists(SK)' ,
5762 ReturnValues : 'ALL_NEW' ,
58- } , item ) ) . promise ( ) ;
63+ } , item ) ) ;
5964 return res . Attributes ? res . Attributes as E : undefined ;
6065}
6166
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 [ ] > {
6368 let startKey ;
6469 const result : T [ ] = [ ] ;
6570 do {
66- const res : DynamoDB . DocumentClient . QueryOutput = await dynamoClient . query ( {
71+ const res : dynamodb . QueryCommandOutput = await dynamoClient . send ( new dynamodb . QueryCommand ( {
6772 ...query ,
6873 TableName : TABLE_NAME ,
6974 ExclusiveStartKey : startKey ,
70- } ) . promise ( ) ;
75+ } ) ) ;
7176 if ( res . Items ) {
7277 result . push ( ...res . Items as T [ ] ) ;
7378 }
@@ -76,15 +81,15 @@ export async function pagedQuery<T>(query: Omit<DynamoDB.DocumentClient.QueryInp
7681 return result ;
7782}
7883
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 [ ] > {
8085 let startKey ;
8186 const result : T [ ] = [ ] ;
8287 do {
83- const res : DynamoDB . DocumentClient . ScanOutput = await dynamoClient . scan ( {
88+ const res : dynamodb . ScanCommandOutput = await dynamoClient . send ( new dynamodb . ScanCommand ( {
8489 ...query ,
8590 TableName : TABLE_NAME ,
8691 ExclusiveStartKey : startKey ,
87- } ) . promise ( ) ;
92+ } ) ) ;
8893 if ( res . Items ) {
8994 result . push ( ...res . Items as T [ ] ) ;
9095 }
@@ -101,7 +106,7 @@ export function padLeftZeros(val: number | string | undefined) {
101106 return ( '00' + val ) . slice ( - 2 ) ;
102107}
103108
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 {
105110 const fieldsToSet = [ ] ;
106111 const fieldsToRemove = [ ] ;
107112 const expressionNames : any = { } ;
@@ -129,7 +134,7 @@ export function createUpdate<T>(request: Omit<DynamoDB.DocumentClient.UpdateItem
129134 if ( request . UpdateExpression ) {
130135 update += request . UpdateExpression ;
131136 }
132- return {
137+ return new dynamodb . UpdateCommand ( {
133138 ...request ,
134139 TableName : TABLE_NAME ,
135140 UpdateExpression : update ,
@@ -147,5 +152,5 @@ export function createUpdate<T>(request: Omit<DynamoDB.DocumentClient.UpdateItem
147152 ...expressionValues ,
148153 } ,
149154 } ,
150- } ;
155+ } ) ;
151156}
0 commit comments