prisma-cursorstream 0.1.1 → 0.3.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 CHANGED
@@ -23,6 +23,21 @@ import cursorStream from "prisma-cursorstream";
23
23
  const db = new PrismaClient().$extends(cursorStream);
24
24
  ```
25
25
 
26
+ <details>
27
+ <summary>Using CommonJS `require`?</summary>
28
+
29
+ ### CommonJS `require`
30
+
31
+ ```js
32
+ const cursorStream = require("prisma-cursorstream").default;
33
+
34
+ const db = new PrismaClient().$extends(cursorStream);
35
+ ```
36
+
37
+ Refer to [#1](https://github.com/etabits/prisma-cursorstream/issues/1).
38
+
39
+ </details>
40
+
26
41
  ## Usage
27
42
 
28
43
  ```js
@@ -43,19 +58,22 @@ for await (const post of stream) {
43
58
 
44
59
  ## Advanced API
45
60
 
46
- The following options provide additional controls over the streaming process.
61
+ The following options (passed as a second argument) provide additional controls over the streaming process.
47
62
 
48
- - **Batch size**: The `take` property of the findMany arg is repurposed to specify 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. Use the `skip` property of the findMany arg which is repurposed to specify the internal `highWaterMark` of the `Readable` stream. Defaults to double the batch size (usually `200`).
63
+ - **Batch size**: `batchSize` specifies how many rows are fetched each batch. Defaults to `100`
64
+ - **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
65
  - **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
66
 
52
67
  ```js
53
68
  const stream = db.post.cursorStream(
54
69
  {
55
- take: 42, // batches of 42 posts at a time
56
- skip: 69, // prefetch up to 69 posts
70
+ where: {
71
+ published: true,
72
+ },
57
73
  },
58
74
  {
75
+ batchSize: 42, // batches of 42 posts at a time
76
+ prefill: 69, // prefetch up to 69+ posts
59
77
  async batchTransformer(posts) {
60
78
  const translations = await translate(posts.map((p) => p.title));
61
79
  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"> | undefined, 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,11 @@ 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
+ findManyArgs = findManyArgs !== null && findManyArgs !== void 0 ? findManyArgs : {};
10
11
  const context = extension_1.Prisma.getExtensionContext(this);
11
- const take = findManyArgs.take || 100;
12
- const highWaterMark = findManyArgs.skip || take * 2;
12
+ const take = batchSize || 100;
13
+ const highWaterMark = prefill || take * 2;
13
14
  const cursorField = Object.keys(findManyArgs.cursor || {})[0] || "id";
14
15
  if (findManyArgs.select && !findManyArgs.select[cursorField]) {
15
16
  throw new Error(`Must select cursor field "${cursorField}"`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-cursorstream",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Prisma Stream Client Extension (Cursor-based Implementation)",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",