Skip to main content

Streaming

Use async stream on any Select for reducing memory allocation to improve efficiency.

// Stream all fruitslet mut stream = Fruit::find().stream(db).await?;
while let Some(item) = stream.try_next().await? {    let item: fruit::ActiveModel = item.into();    // do something with item}
// Stream all fruits with name contains character "a"let mut stream = Fruit::find()    .filter(fruit::Column::Name.contains("a"))    .order_by_asc(fruit::Column::Name)    .stream(db)    .await?;

Note that the stream object will exclusively hold onto the connection until being dropped, preventing the connection to be borrowed by others.

{    let s1 = Fruit::find().stream(db).await?;    let s2 = Fruit::find().stream(db).await?;    let s3 = Fruit::find().stream(db).await?;    // 3 connections are held}// All streams are dropped and connections are returned to the connection pool