kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.71k stars 271 forks source link

[IDEA] Allow streaming of database query results #105

Closed elderapo closed 2 years ago

elderapo commented 2 years ago

Already mentioned in the discord channel. Basically something like this:

const peopleStream = await db
    .selectFrom('person')
    .innerJoin('pet', 'pet.owner_id', 'person.id')
    .select(['first_name', 'pet.name as pet_name'])
    .executeStream(); // or maybe `.execute({ stream: true })`

for await (const person of peopleStream) {
  if (person.first_name === "John") {
    break; // early terminate the stream based on some condition
  }

  console.log(person);
}

I think async iterators provide a cleaner API for this case than node streams.

Some links that could help with internal implementation:

koskimas commented 2 years ago

This would be a nice feature. If you start implementing this, remember that Kysely has zero dependencies. You can't refer to any libraries inside kysely code or even to any typing packages. That means all needed libraries and modules need to be provided through Dialect constructors just like the pool is provided now.

Since most users won't use this feature, they don't want to install extra dependencies for this, so all extra deps provided through the dialect constructors need to be optional.

Other hints: You probably want to add another method for the DatabaseConnection interface. Something like createQueryStream