pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

feat: add support for streaming

Open EloB opened this issue 1 year ago • 1 comments

Link to my feature request issue!

More context:

pg-cursor is maintained by the same author as pg and also part of his official documentation.

Links:

  • pg-cursor is documented on README https://github.com/brianc/node-postgres

  • pg-cursor is located in same monorepo as pg https://github.com/brianc/node-postgres/tree/master/packages/pg-cursor

  • Official pg-cursor documentation https://node-postgres.com/apis/cursor

Usage

import ( IDatabaseConnection } from "@pgtyped/runtime/lib/tag";
import { Client } from "pg";
import Cursor from "pg-cursor";

const client = new Client({ /* ... */ });
const connection: IDatabaseConnection = {
  query: (query, bindings) => client.query(query, bindings),
  stream: (query, bindings) => client.query(new Cursor(query, bindings)),
};

const getBigData = sql`SELECT * FROM big_data`;
const cursor = getBigData.stream({}, connection);
let rows = await cursor.read(1000);
while (rows.length) {
  // Do something with the fetched rows...
  
  // Then fetch another 1000 rows.
  rows = await cursor.read(1000);
}

Fixes https://github.com/adelsz/pgtyped/issues/568

EloB avatar Feb 07 '24 15:02 EloB

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
pgtyped ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 7, 2024 3:28pm

vercel[bot] avatar Feb 07 '24 15:02 vercel[bot]

Maybe when I have time. Would appreciate if this got merged. Right now I patched it for my client using pnpm patch. I'll end my contract this friday. Maybe after I can add it.

EloB avatar Apr 10 '24 18:04 EloB

Thanks @EloB

adelsz avatar Apr 14 '24 09:04 adelsz