module botbuildercommunity import { config as awsConfig , Credentials as AWSCredentials } from 'aws-sdk' ; import { DocumentClient } from 'aws-sdk/clients/dynamodb' ; import { Storage , StoreItems } from 'botbuilder-core' ; interface AWSCredentialsOptions { /**
     * AWS access key ID.
     */ accessKeyId : string ; /**
     * AWS secret access key.
     */ secretAccessKey : string ; /**
     * AWS session token.
     */ sessionToken ? : string ; } remarks export class DynamoDBStorage implements Storage { private readonly tableName : string ; private readonly region : string ; private readonly credentials ? : AWSCredentialsOptions ; param region param tableName public constructor ( tableName , region , credentials ? ) { this . tableName = tableName ; this . region = region ; this . credentials = credentials ; } public async read ( keys ) { const response = await this . getDynamoDBDocumentClient ( ) . batchGet ( { RequestItems : { [ this . tableName ] : { Keys : keys . map ( ( k ) : { key : string ; } => ( { key : k } ) ) } } } ) . promise ( ) ; return response . Responses [ this . tableName ] . reduce ( ( acc , r ) : StoreItems => { if ( ! r ) { return acc ; } return { ... acc , [ r . key ] : r . document } ; } , { } ) ; } public async write ( changes ) { const client = this . getDynamoDBDocumentClient ( ) ; await Promise . all ( Object . keys ( changes ) . map ( async ( key ) : Promise < void > => { const document = { ... changes [ key ] } ; const eTag = changes [ key ] . eTag ; if ( ! eTag || eTag === '*' ) { await client . update ( { TableName : this . tableName , Key : { key : key } , UpdateExpression : 'set #document = :document' , ExpressionAttributeNames : { '#document' : 'document' } , ExpressionAttributeValues : { ':document' : document } } ) . promise ( ) ; } else if ( eTag . length > 0 ) { await client . put ( { TableName : this . tableName , Item : { key : eTag , document } } ) . promise ( ) ; } else { throw new Error ( 'eTag empty' ) ; } } ) ) ; } public async delete ( keys ) { await this . getDynamoDBDocumentClient ( ) . batchWrite ( { RequestItems : { [ this . tableName ] : keys . map ( ( k ) : { DeleteRequest : { Key : { key : string ; } ; } ; } => ( { DeleteRequest : { Key : { key : k } } } ) ) } } ) . promise ( ) ; } private getDynamoDBDocumentClient ( ) { awsConfig . update ( { region : this . region , ... ( this . credentials ? { credentials : new AWSCredentials ( this . credentials ) } : { } ) } ) ; return new DocumentClient ( { apiVersion : '2012-08-10' } ) ; } }