import { BoosterConfig , ConnectionDataEnvelope } from '@boostercloud/framework-types' ; import { ApiGatewayManagementApi , DynamoDB } from 'aws-sdk' ; import { environmentVarNames , connectionsStoreAttributes } from '../constants' ; export async function storeConnectionData ( db , config , connectionID , data ) { await db . put ( { TableName : config . resourceNames . connectionsStore , Item : { ... data , [ connectionsStoreAttributes . partitionKey ] : connectionID , } , } ) . promise ( ) ; } export async function fetchConnectionData ( db , config , connectionID ) { const response = await db . get ( { TableName : config . resourceNames . connectionsStore , Key : { [ connectionsStoreAttributes . partitionKey ] : connectionID } , ConsistentRead : true , } ) . promise ( ) ; return response . Item as ConnectionDataEnvelope ; } export async function deleteConnectionData ( db , config , connectionID ) { await db . delete ( { TableName : config . resourceNames . connectionsStore , Key : { [ connectionsStoreAttributes . partitionKey ] : connectionID } , } ) . promise ( ) ; } export async function sendMessageToConnection ( config , connectionID , data ) { await new ApiGatewayManagementApi ( { endpoint : config . mustGetEnvironmentVar ( environmentVarNames . websocketAPIURL ) , } ) . postToConnection ( { ConnectionId : connectionID , Data : JSON . stringify ( data ) , } ) . promise ( ) ; }