adelsz / pgtyped

pgTyped - Typesafe SQL in TypeScript
https://pgtyped.dev
MIT License
2.91k stars 94 forks source link

feat: add support for streaming #569

Closed EloB closed 4 months ago

EloB commented 7 months ago

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:

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

vercel[bot] commented 7 months ago

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
EloB commented 5 months ago

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.

adelsz commented 4 months ago

Thanks @EloB