You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be pretty nifty to be able to use the for await (const datum of <something>) {} construction with streaming queries, with proper types.
Desired Behavior
constexampleQuery=sql.type(z.object({bar: z.string()}))`SELECT 'foo' as bar`;awaitpool.streamIterable(exampleQuery,async(s)=>{forawait(const{ row }ofs){console.log(`This has typechecking: ${row.bar}`);}}
Motivation
For slower computations where storing all data in memory at once (or fetching it over the network at once) is not feasible, AsyncIterables offer convenient data processing facilities, while still being typed.
The stream method offers similar facilities, but seems to not have any typing due to Readable not being Readable<T>.
Implementation
The most low-effort way to me seems to offer the possibility of StreamHandler to be an async function, in which case building my own asTypedIterable on top of this will be trivial.
One issue is that the user somehow has to make sure that the Readable is actually closed/destroyed. This is done automagically when one runs a for await (const _ of <readable>){}, but the Readable obviously does not get closed/destroyed if there is no such loop.
OTOH, this is also currently the case for the existing stream, so we might consider this a case of foot meet gun.
constexampleQuery=sql.type(z.object({bar: z.string()}))`SELECT 'foo' as bar`;awaitpool.stream(exampleQuery,(s)=>{console.log('This prints');});console.log('This does not for at least a while');
The text was updated successfully, but these errors were encountered:
I wonder about the safety of the following approach:
asyncfunction*streamMARCsByLibraryID(connection: DatabaseConnection,libraryId: number,){conststream=awaitnewPromise<Readable>((resolve,reject)=>connection.stream(sql.unsafe`SELECT id, marc FROM unitas_library.harvested_record WHERE library_id = ${libraryId}`,(stream)=>resolve(stream),).catch((reason)=>reject(reason)),);forawait(const{ row }ofstream){yield<{id: string;marc: string}>row;}stream.destroy();}
It would be pretty nifty to be able to use the
for await (const datum of <something>) {}
construction with streaming queries, with proper types.Desired Behavior
Motivation
For slower computations where storing all data in memory at once (or fetching it over the network at once) is not feasible, AsyncIterables offer convenient data processing facilities, while still being typed.
The
stream
method offers similar facilities, but seems to not have any typing due toReadable
not beingReadable<T>
.Implementation
The most low-effort way to me seems to offer the possibility of
StreamHandler
to be anasync
function, in which case building my ownasTypedIterable
on top of this will be trivial.One issue is that the user somehow has to make sure that the Readable is actually closed/destroyed. This is done automagically when one runs a
for await (const _ of <readable>){}
, but the Readable obviously does not get closed/destroyed if there is no such loop.OTOH, this is also currently the case for the existing
stream
, so we might consider this a case of foot meet gun.The text was updated successfully, but these errors were encountered: