ORM-15: Add insert, update, delete, create, find and all to Model #15

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

Depends on: #14

Context

The Eloquent-style surface: User::insert(&user), User::find(1), User::create(..). The write methods take &self / &mut self, so the call form User::insert(&user) and the method form user.insert() both work.

Tasks

  • insert(&self) -> Result<i64>: plain INSERT. Doc warning: it does not set the id or mark the model clean, so insert followed by save inserts twice.
  • update(&self) -> Result<usize>: UPDATE of all non-PK columns WHERE pk = ?, returning rows affected.
  • delete(&mut self) -> Result<usize>: DELETE by PK and clear the snapshot so the model counts as not persisted. Static form: User::delete(&mut user).
  • create(model: Self) -> Result<Self>: insert, set_id, snapshot, return the model.
  • find(id: i64) -> Result<Option<Self>> and all() -> Result<Vec<Self>>.
  • insert_on(&Connection), update_on and delete_on for an explicit connection.

Example

let user = User::new("dana", None, 28, true);
let id = User::insert(&user)?;                  // INSERT INTO users (name, email, age, active) VALUES (?, ?, ?, ?)

let found: Option<User> = User::find(id)?;
let everyone: Vec<User> = User::all()?;

let mut erin = User::create(User::new("erin", None, 31, true))?; // id set, state clean
User::update(&erin)?;                           // UPDATE users SET name = ?, ... WHERE id = ?
User::delete(&mut erin)?;                       // DELETE FROM users WHERE id = ?
assert!(!erin.state().is_persisted());

The footgun the docs must call out:

let mut u = User::new("eve", None, 30, true);
u.insert()?; // row 1
u.save()?;   // row 2! insert() did not touch the model's state
fn find(id: i64) -> Result<Option<Self>> {
    Self::query()
        .filter(Column::<Self, i64>::new(Self::PRIMARY_KEY).equals(id))
        .first()
}

Acceptance criteria

  • Inserting the same model twice gives two rows.
  • find of a missing id returns None.
  • update touches only its own row.
  • delete removes the row and is_persisted() becomes false.
  • create returns a model with a real id and a clean state.
**Depends on:** #14 ## Context The Eloquent-style surface: `User::insert(&user)`, `User::find(1)`, `User::create(..)`. The write methods take `&self` / `&mut self`, so the call form `User::insert(&user)` and the method form `user.insert()` both work. ## Tasks - [ ] `insert(&self) -> Result<i64>`: plain INSERT. Doc warning: it does not set the id or mark the model clean, so `insert` followed by `save` inserts twice. - [ ] `update(&self) -> Result<usize>`: UPDATE of all non-PK columns `WHERE pk = ?`, returning rows affected. - [ ] `delete(&mut self) -> Result<usize>`: DELETE by PK and clear the snapshot so the model counts as not persisted. Static form: `User::delete(&mut user)`. - [ ] `create(model: Self) -> Result<Self>`: insert, `set_id`, snapshot, return the model. - [ ] `find(id: i64) -> Result<Option<Self>>` and `all() -> Result<Vec<Self>>`. - [ ] `insert_on(&Connection)`, `update_on` and `delete_on` for an explicit connection. ## Example ```rust let user = User::new("dana", None, 28, true); let id = User::insert(&user)?; // INSERT INTO users (name, email, age, active) VALUES (?, ?, ?, ?) let found: Option<User> = User::find(id)?; let everyone: Vec<User> = User::all()?; let mut erin = User::create(User::new("erin", None, 31, true))?; // id set, state clean User::update(&erin)?; // UPDATE users SET name = ?, ... WHERE id = ? User::delete(&mut erin)?; // DELETE FROM users WHERE id = ? assert!(!erin.state().is_persisted()); ``` The footgun the docs must call out: ```rust let mut u = User::new("eve", None, 30, true); u.insert()?; // row 1 u.save()?; // row 2! insert() did not touch the model's state ``` ```rust fn find(id: i64) -> Result<Option<Self>> { Self::query() .filter(Column::<Self, i64>::new(Self::PRIMARY_KEY).equals(id)) .first() } ``` ## Acceptance criteria - [ ] Inserting the same model twice gives two rows. - [ ] `find` of a missing id returns `None`. - [ ] `update` touches only its own row. - [ ] `delete` removes the row and `is_persisted()` becomes false. - [ ] `create` returns a model with a real id and a clean state.
Sign in to join this conversation.
No description provided.