-
Notifications
You must be signed in to change notification settings - Fork 0
Description
We need real row streaming in prisma-client-js.
From: prisma#5055
DO NOT USE cursors for this. The rows should stream as they come from the network.
Let's say I have 1.000.000 rows in my database and I need to export them to csv.
As of now, Prisma Client doesn't allow you to do that easily. You need to work with a for loop and manually paginate through the findMany api.
Async iterators are a perfect fit for this situation. Prisma Client could expose an async iterator for any findMany call.
It can look like this:
const iterator: AsyncIterator<User> = prisma.user.findMany(...).stream()
As findMany doesn't have any other methods right now, there is no conflict with stream, we can just name it findMany().stream().
The stream would for now not have any args, but could later if needed support chunking and advanced configuration of the streaming.
This can then be consumed like so:
const users = []
for await (const user of prisma.user.findMany().stream()) {
users.push(user)
}
Or for example directly streaming it into a file:
const file = fs.createWriteStream('users.csv')
file.write('id\tname\n')
for await (const user of prisma.user.findMany().stream()) {
file.write(`${user.id}\t${user.name}\n`)
}
file.end()