ORM-14: Ambient Query terminals and a single hydrate path #14

Open
opened 2026-10-13 12:49:39 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #4, #6

Context

Queries should run against the ambient connection (User::query().all()), with an _on(&Connection) escape hatch. Every row loaded from the database must also get its ModelState snapshot, so there must be exactly one place where rows become models. Relations will reuse it.

Tasks

  • Query::all() -> Result<Vec<M>> and Query::first() -> Result<Option<M>> run through Database::with.
  • The current connection-taking versions become all_on(&Connection) and first_on(&Connection).
  • One internal hydrate::<M>(row) that calls M::from_row and then snapshots with state_mut().snapshot(model.insert_values()). All read paths use it.
  • Minimal edits to existing tests (.all(&conn) becomes .all_on(&conn)) so the suite stays green. #17 does the full port.

Example

// before
let users = User::query().filter(User::AGE.greater_equals(18)).all(&conn)?;

// after: ambient connection
let adults = User::query()
    .filter(User::AGE.greater_equals(18))
    .order_by(User::NAME.asc())
    .all()?;

// after: explicit connection
let conn = rusqlite::Connection::open("other.db")?;
let first = User::query().first_on(&conn)?;
pub(crate) fn hydrate<M: Model>(row: &Row) -> rusqlite::Result<M> {
    let mut model = M::from_row(row)?;
    let values = model.insert_values();
    model.state_mut().snapshot(values);
    Ok(model)
}

impl<M: Model> Query<M> {
    pub fn all(&self) -> Result<Vec<M>> {
        Database::with(|conn| self.all_on(conn))
    }

    pub fn all_on(&self, conn: &Connection) -> Result<Vec<M>> {
        let mut statement = conn.prepare_cached(&self.to_sql())?;
        let rows = statement.query_map(params_from_iter(self.params()), hydrate::<M>)?;
        Ok(rows.collect::<rusqlite::Result<Vec<M>>>()?)
    }
}
#[test]
fn loaded_rows_are_persisted() {
    let users = User::query().all_on(&setup_conn()).unwrap();
    assert!(users.iter().all(|u| u.state().is_persisted()));
}

Acceptance criteria

  • Rows loaded through all, first and all_on report state().is_persisted().
  • Existing query tests pass apart from the _on rename.
**Depends on:** #4, #6 ## Context Queries should run against the ambient connection (`User::query().all()`), with an `_on(&Connection)` escape hatch. Every row loaded from the database must also get its `ModelState` snapshot, so there must be exactly one place where rows become models. Relations will reuse it. ## Tasks - [ ] `Query::all() -> Result<Vec<M>>` and `Query::first() -> Result<Option<M>>` run through `Database::with`. - [ ] The current connection-taking versions become `all_on(&Connection)` and `first_on(&Connection)`. - [ ] One internal `hydrate::<M>(row)` that calls `M::from_row` and then snapshots with `state_mut().snapshot(model.insert_values())`. All read paths use it. - [ ] Minimal edits to existing tests (`.all(&conn)` becomes `.all_on(&conn)`) so the suite stays green. #17 does the full port. ## Example ```rust // before let users = User::query().filter(User::AGE.greater_equals(18)).all(&conn)?; // after: ambient connection let adults = User::query() .filter(User::AGE.greater_equals(18)) .order_by(User::NAME.asc()) .all()?; // after: explicit connection let conn = rusqlite::Connection::open("other.db")?; let first = User::query().first_on(&conn)?; ``` ```rust pub(crate) fn hydrate<M: Model>(row: &Row) -> rusqlite::Result<M> { let mut model = M::from_row(row)?; let values = model.insert_values(); model.state_mut().snapshot(values); Ok(model) } impl<M: Model> Query<M> { pub fn all(&self) -> Result<Vec<M>> { Database::with(|conn| self.all_on(conn)) } pub fn all_on(&self, conn: &Connection) -> Result<Vec<M>> { let mut statement = conn.prepare_cached(&self.to_sql())?; let rows = statement.query_map(params_from_iter(self.params()), hydrate::<M>)?; Ok(rows.collect::<rusqlite::Result<Vec<M>>>()?) } } ``` ```rust #[test] fn loaded_rows_are_persisted() { let users = User::query().all_on(&setup_conn()).unwrap(); assert!(users.iter().all(|u| u.state().is_persisted())); } ``` ## Acceptance criteria - [ ] Rows loaded through `all`, `first` and `all_on` report `state().is_persisted()`. - [ ] Existing query tests pass apart from the `_on` rename.
Sign in to join this conversation.
No description provided.