effect-aws icon indicating copy to clipboard operation
effect-aws copied to clipboard

Try generate method returning Stream from paginated results

Open floydspace opened this issue 8 months ago • 2 comments

As requested here, could be good to make streaming api for generated clients

todo:

  • investigate if AWS manifest provides some useful pagination info so we can generate streaming api
  • consider creating overloaded methods (DynamoDB.scan({}, { stream: true })) or suffixed methods (DynamoDB.scanStream({}))

currently it can be done like this:

//        ┌─── Stream.Stream<Record<string, AttributeValue>, SdkError | ... 4 more ... | ResourceNotFoundError, DynamoDBService>
//        â–¼
const streamed = Stream.paginateChunkEffect(
  undefined as Record<string, AttributeValue> | undefined,
  (key) =>
    DynamoDB.scan({
      TableName: "example-table",
      Limit: 25,
      ExclusiveStartKey: key,
    }).pipe(
      Effect.map((result) => [
        Chunk.fromIterable(result.Items!),
        Option.fromNullable(result.LastEvaluatedKey),
      ])
    )
);

floydspace avatar Apr 02 '25 18:04 floydspace

AWS SDK provides async iterators for pagination operations, so you can use the following:

import { CloudWatchLogsClient, paginateFilterLogEvents } from '@aws-sdk/client-cloudwatch-logs'

const logs = new CloudWatchLogsClient()

const paginator = paginateFilterLogEvents(
  { client: logs },
  {
    logGroupName,
    filterPattern,
    startTime,
    endTime,
    limit: 500
  }
)

const stream = Stream.fromAsyncIterator(paginator, (error) => { console.error('Error:', error) })

mixja avatar Jul 27 '25 06:07 mixja

elegant, thanks for posting it here

floydspace avatar Jul 27 '25 17:07 floydspace