import { getChildCollection , assemblePrimaryKeyValue , unwrap , } from '../base/util' ; import { QueryInput , Key , Converter } from 'aws-sdk/clients/dynamodb' ; import { Context } from '../context' ; import { DocumentWithId , WrappedDocument } from '../base/common' ; import debugDynamo from '../debug/debugDynamo' ; import { CompositeCondition } from '../base/conditions' ; import { createNameMapper , createValueMapper } from '../base/mappers' ; /**
 * The results of a [[findChildren]] operation.
 */ export type FindChildrenResults < DocumentType extends DocumentWithId > = { /** the items that were returned in this batch */ items : DocumentType [ ] ; /** The pagination token. If this value is specified, it means
     * there is more results for the query. Provide it to another
     * call to `findChildren` to get the next set of results.
     */ nextToken ? : Key ; } ; /**
 * The options to a [[findChildren]] operation
 */ export type FindChildrenOptions = { limit ? : number ; /**
     * `true` (default) to scan the index forward, `false` to scan it backward
     */ scanForward ? : boolean ; /**
     * An optional filter expression for the
     * find operation
     */ filter ? : CompositeCondition ; } ; param ctx param childCollectionName param rootObjectId param nextToken param options param options . limit param options . scanForward param options . filter throws export async function findChildren < DocumentType extends DocumentWithId > ( ctx , childCollectionName , rootObjectId , nextToken ? , options = { } ) { const childCollection = getChildCollection ( ctx , childCollectionName ) ; const nameMapper = createNameMapper ( ) ; const valueMapper = createValueMapper ( ) ; const { parentCollectionName , layout : { primaryKey : { partitionKey , sortKey } , } , } = childCollection ; const parentId = assemblePrimaryKeyValue ( parentCollectionName , rootObjectId , childCollection . layout . indexKeySeparator ) ; const childCollectionPrefix = assemblePrimaryKeyValue ( childCollectionName , '' , childCollection . layout . indexKeySeparator ) ; const keyConditionExpression = `${ nameMapper . map ( partitionKey ) } = ${ valueMapper . map ( parentId ) } ` + `AND begins_with(${ nameMapper . map ( sortKey ) }, ${ valueMapper . map ( childCollectionPrefix ) })` ; const request = { TableName : childCollection . layout . tableName , KeyConditionExpression : keyConditionExpression , ExpressionAttributeNames : nameMapper . get ( ) , ExpressionAttributeValues : valueMapper . get ( ) , ExclusiveStartKey : nextToken , Limit : options ?. limit , ScanIndexForward : options ?. scanForward ?? true , } ; debugDynamo ( 'Query' , request ) ; const results = await ctx . ddb . query ( request ) . promise ( ) ; return { items : ( results . Items || [ ] ) . map ( ( item ) => unwrap ( Converter . unmarshall ( item ) as WrappedDocument < DocumentType > ) ) , nextToken : results . LastEvaluatedKey , } ; }