Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for streaming #569

Merged
merged 1 commit into from
Apr 14, 2024

Conversation

EloB
Copy link
Contributor

@EloB EloB commented Feb 7, 2024

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 #568

Copy link

vercel bot commented Feb 7, 2024

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

export interface IDatabaseConnection {
query: (query: string, bindings: any[]) => Promise<{ rows: any[] }>;
stream?: (query: string, bindings: any[]) => ICursor<any[]>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this optional to not break backwards compatibility. If you use it anyway it will throw an error.

Copy link
Owner

@adelsz adelsz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @EloB. Looks really good.
Will you be able to add a change to docs to document this?

@EloB
Copy link
Contributor Author

EloB commented Apr 10, 2024

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 adelsz merged commit f2e6faa into adelsz:master Apr 14, 2024
4 checks passed
@adelsz
Copy link
Owner

adelsz commented Apr 14, 2024

Thanks @EloB

zth pushed a commit to zth/pgtyped-rescript that referenced this pull request Apr 27, 2024
@ungeordnet
Copy link

@adelsz when will you release this?

@EloB EloB deleted the feature/support-streaming branch October 10, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for streaming/cursor
3 participants