batch-mobile

Asynchronous batched iterable for (mongo) cursors

Usage no npm install needed!

<script type="module">
  import batchMobile from 'https://cdn.skypack.dev/batch-mobile';
</script>

README

batch-mobile 🦇

Batch Cave npm version vulnerabilities

Asynchronous batched iterable for (mongo) cursors

A library when one is not enough and all is to much.

Installation

npm i batch-mobile

API

getBatchedIterableFromCursor(cursor, batchSize)

  • cursor: Any iterable cursor which exposes an aynchronous next method
  • batchSize: The size of the yielded batch. Defaults to 200 if unset.

Usage

Simple example

const { getBatchedIterableFromCursor } = require('batch-mobile')
for await (const batchOfItems of getBatchedIterableFromCursor(cursor)) {
  await pushBatchToService(batchOfItems)
}

Mongo example

const { MongoClient } = require('mongodb')
const client = await (new MongoClient(process.env.MONGO_URI)).connect()
const { getBatchedIterableFromCursor } = require('batch-mobile')

try {
  const collection = client.db('application').collection('collection')
  const cursor = collection
    .find({ foo: 'bar' })

  for await (const batch of getBatchedIterableFromCursor(cursor, 1000)) {
    await processItemsFrom(batch)
  }
} finally {
  await client.close(true)
}