Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The following options (passed as a second argument) provide additional controls
- **Batch size**: `batchSize` specifies how many rows are fetched each batch. Defaults to `100`
- **Pre-fill size**: Since the streaming happens asynchronously, it may be useful to fetch more rows well before current batch is completely consumed. `prefill` specifies the internal `highWaterMark` of the `Readable` stream. Defaults to double the batch size (usually `200`).
- **Batch transformation**: Sometimes you may need to pre-process the resulting rows in batches before they are consumed. This is where the `batchTransformer` comes in. If provided, it receives an array of the last fetched batch of rows, can operate on them asynchronously, and then return an array of transformed rows to be consumed instead. Types are properly handled.
- **Cursor field**: `cursor` Specifies the model field to be used as the cursor.

```js
const stream = db.post.cursorStream(
Expand All @@ -81,6 +82,7 @@ const stream = db.post.cursorStream(
translatedTitle: translations[i],
}));
},
cursor: 'another_id'
}
);

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export default Prisma.defineExtension(
>(
this: T,
findManyArgs: A,
{ batchSize, prefill, batchTransformer } = {} as {
{ batchSize, prefill, batchTransformer, cursor } = {} as {
batchSize?: number;
prefill?: number;
batchTransformer?: C;
cursor?: Extract<keyof R, string> | undefined;
}
): AsyncIterable<
C extends Function
Expand All @@ -33,7 +34,7 @@ export default Prisma.defineExtension(

const take = batchSize || 100;
const highWaterMark = prefill || take * 2;
const cursorField =
const cursorField = cursor ||
Object.keys(findManyArgs.cursor || {})[0] || "id";

if (findManyArgs.select && !findManyArgs.select[cursorField]) {
Expand Down