/** * Dynamo Gateway */ import { DocumentClient, GetItemOutput, Key, QueryOutput, } from "aws-sdk/clients/dynamodb"; import { IDENTIFIERS } from "constant/Identifiers"; import { inject, injectable } from "inversify"; import "reflect-metadata"; import { IDynamoGateway, IGetitemParams } from "repository/IDynamoGateway"; import { Observable, of } from "rxjs"; import { tag } from "rxjs-spy/operators"; import { map, mapTo, switchMap } from "rxjs/operators"; /** * LambdaClient to send data do DynamoDB */ @injectable() export class DynamoGateway implements IDynamoGateway { private readonly _client: DocumentClient; constructor(@inject(IDENTIFIERS.AwsDocumentClient) client: DocumentClient) { this._client = client; } public getItem(table: string, key: object): Observable { const params: IGetitemParams = { TableName: table, Key: key, }; return of(1).pipe( switchMap(async () => this._client.get(params).promise()), tag("DynamoGateway | getItem"), map((item: GetItemOutput) => item.Item) ); } public put(data: object, table: string): Observable { const params: { TableName: string; Item: object; } = { TableName: table, Item: data, }; return of(1).pipe( switchMap(async () => this._client.put(params).promise()), tag("DynamoGateway | put"), mapTo(true) ); } public query(params: DocumentClient.QueryInput): Observable { return of(1).pipe( switchMap(async () => this._client.query(params).promise()), tag("DynamoGateway | query") ); } }