Skip to content

Commit

Permalink
perf: directly collect join results to separate containers
Browse files Browse the repository at this point in the history
  • Loading branch information
jooakar committed Feb 2, 2024
1 parent ce7b7f1 commit d6d41c0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/database/invoices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::DatabaseConnection;
use crate::api::invoices::{CreateInvoice, PopulatedInvoice};
use crate::error::Error;
use crate::models::*;
use futures::TryStreamExt;

use diesel::prelude::*;
use diesel_async::RunQueryDsl;
Expand Down Expand Up @@ -110,10 +111,17 @@ impl DatabaseConnection {
invoices::table
.inner_join(parties::table)
.select((Invoice::as_select(), Party::as_select()))
.load::<(Invoice, Party)>(&mut self.0)
.load_stream::<(Invoice, Party)>(&mut self.0)
.await?
.try_fold(
(Vec::new(), Vec::new()),
|(mut invoices, mut parties), (invoice, party)| {
invoices.push(invoice);
parties.push(party);
futures::future::ready(Ok((invoices, parties)))
},
)
.await?
.into_iter()
.unzip()
};
let invoice_rows = InvoiceRow::belonging_to(&invoices)
.select(InvoiceRow::as_select())
Expand Down

0 comments on commit d6d41c0

Please sign in to comment.