prisma-cursorstream 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -5
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,19 +43,22 @@ for await (const post of stream) {
|
|
|
43
43
|
|
|
44
44
|
## Advanced API
|
|
45
45
|
|
|
46
|
-
The following options provide additional controls over the streaming process.
|
|
46
|
+
The following options (passed as a second argument) provide additional controls over the streaming process.
|
|
47
47
|
|
|
48
|
-
- **Batch size**:
|
|
49
|
-
- **Pre-fill size**: Since the streaming happens asynchronously, it may be useful to fetch more rows well before current batch is completely consumed.
|
|
48
|
+
- **Batch size**: `batchSize` specifies how many rows are fetched each batch. Defaults to `100`
|
|
49
|
+
- **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`).
|
|
50
50
|
- **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.
|
|
51
51
|
|
|
52
52
|
```js
|
|
53
53
|
const stream = db.post.cursorStream(
|
|
54
54
|
{
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
where: {
|
|
56
|
+
published: true,
|
|
57
|
+
},
|
|
57
58
|
},
|
|
58
59
|
{
|
|
60
|
+
batchSize: 42, // batches of 42 posts at a time
|
|
61
|
+
prefill: 69, // prefetch up to 69+ posts
|
|
59
62
|
async batchTransformer(posts) {
|
|
60
63
|
const translations = await translate(posts.map((p) => p.title));
|
|
61
64
|
return posts.map((p, i) => ({
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ import { Prisma, PrismaClientExtends } from "@prisma/client/extension";
|
|
|
2
2
|
import { DefaultArgs } from "@prisma/client/runtime/library";
|
|
3
3
|
declare const _default: (client: any) => PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {
|
|
4
4
|
$allModels: {
|
|
5
|
-
cursorStream<T, A extends Prisma.Args<T, "findMany">, R extends Prisma.Result<T, A, "findMany">[number], C extends ((dataset: R[]) => Promise<unknown[]>) | undefined>(this: T, findManyArgs: A, { batchTransformer }?: {
|
|
5
|
+
cursorStream<T, A extends Prisma.Args<T, "findMany">, R extends Prisma.Result<T, A, "findMany">[number], C extends ((dataset: R[]) => Promise<unknown[]>) | undefined>(this: T, findManyArgs: A, { batchSize, prefill, batchTransformer }?: {
|
|
6
|
+
batchSize?: number | undefined;
|
|
7
|
+
prefill?: number | undefined;
|
|
6
8
|
batchTransformer?: C | undefined;
|
|
7
9
|
}): Iterable<C extends Function ? Awaited<ReturnType<C>>[number] extends object ? Awaited<ReturnType<C>>[number] : R : R>;
|
|
8
10
|
};
|
package/dist/index.js
CHANGED
|
@@ -6,10 +6,10 @@ exports.default = extension_1.Prisma.defineExtension((client) => {
|
|
|
6
6
|
return client.$extends({
|
|
7
7
|
model: {
|
|
8
8
|
$allModels: {
|
|
9
|
-
cursorStream(findManyArgs, { batchTransformer } = {}) {
|
|
9
|
+
cursorStream(findManyArgs, { batchSize, prefill, batchTransformer } = {}) {
|
|
10
10
|
const context = extension_1.Prisma.getExtensionContext(this);
|
|
11
|
-
const take =
|
|
12
|
-
const highWaterMark =
|
|
11
|
+
const take = batchSize || 100;
|
|
12
|
+
const highWaterMark = prefill || take * 2;
|
|
13
13
|
const cursorField = Object.keys(findManyArgs.cursor || {})[0] || "id";
|
|
14
14
|
if (findManyArgs.select && !findManyArgs.select[cursorField]) {
|
|
15
15
|
throw new Error(`Must select cursor field "${cursorField}"`);
|