import { Converter , GetItemInput } from 'aws-sdk/clients/dynamodb' ; import { Context } from '../context' ; import { unwrap , assemblePrimaryKeyValue , getRootCollection , } from '../base/util' ; import { DocumentWithId , WrappedDocument } from '../base/common' ; import debugDynamo from '../debug/debugDynamo' ; param context param collectionName param id returns throws export async function findById < DocumentType extends DocumentWithId > ( context , collectionName , id ) { const collection = getRootCollection ( context , collectionName ) ; const request = { TableName : collection . layout . tableName , Key : Converter . marshall ( { [ collection . layout . primaryKey . partitionKey ] : assemblePrimaryKeyValue ( collectionName , id , collection . layout . indexKeySeparator ) , [ collection . layout . primaryKey . sortKey ] : assemblePrimaryKeyValue ( collectionName , id , collection . layout . indexKeySeparator ) , } ) , } ; debugDynamo ( 'GetItem' , request ) ; const result = await context . ddb . getItem ( request ) . promise ( ) ; if ( result . Item ) { const wrapped = Converter . unmarshall ( result . Item ) ; return unwrap ( wrapped as WrappedDocument < DocumentType > ) ; } return undefined ; }