import { DynamoDB } from 'aws-sdk' ; import { DocumentClient } from 'aws-sdk/lib/dynamodb/document_client' ; import AttributeMap = DocumentClient . AttributeMap ; import ScanOutput = DocumentClient . ScanOutput ; import PutItemInput = DocumentClient . PutItemInput ; import ScanInput = DocumentClient . ScanInput ; export class ToDo { public id : string ; public text : string ; public state : string ; constructor ( attr ) { this . id = attr . id ; this . text = attr . text ; this . state = attr . state ; } } export const save = async ( todo ) : Promise < ToDo > => { const params = { Item : todo , TableName : process . env . TABLE_NAME , } ; const dynamoClient = new DynamoDB . DocumentClient ( ) ; await dynamoClient . put ( params ) . promise ( ) ; return todo ; } ; const scanDynamoDB = async ( query ) : Promise < ToDo [ ] > => { const dynamoClient = new DynamoDB . DocumentClient ( ) ; const data = await dynamoClient . scan ( query ) . promise ( ) ; if ( ! data . Items ) { return [ ] ; } const todos = data . Items . map ( ( attr ) => new ToDo ( attr ) ) ; if ( data . LastEvaluatedKey ) { query . ExclusiveStartKey = data . LastEvaluatedKey ; const list = await scanDynamoDB ( query ) ; todos . forEach ( ( item ) => { list . push ( item ) ; } ) ; return list ; } return todos ; } ; export const listTodos = ( ) : Promise < ToDo [ ] > => { return scanDynamoDB ( { TableName : process . env . TABLE_NAME , Limit : 1000 , } ) ; } ;