ORM-04: Add ModelState #4
Labels
No labels
area:core
area:database
area:migrations
area:model
area:relations
area:schema
area:tests
breaking-change
size:M
size:S
tracking
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
NoctisWolfshadow/tessera-orm#4
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Depends on: #3
Context
is_dirty()andsave()need to know what a model looked like when it was loaded or last saved. Like Eloquent's original attributes, each model carries a hiddenstate: ModelStatefield holding a snapshot of its values.Tasks
pub struct ModelState { original: Option<Vec<Value>> }withnew(),Default,Debug,Clone,is_persisted(),pub(crate) snapshot(Vec<Value>)andpub(crate) clear().PartialEqthat always returns true, so a derivedPartialEqon a model ignores state. A loaded model must still equal an identical freshly built one.fn state(&self) -> &ModelStateandfn state_mut(&mut self) -> &mut ModelState.insert_values().Userfixture: addstate: ModelState,User::newinitialises it, and the struct literal init_worksis updated.Example
Acceptance criteria
snapshotit is.clonekeeps the snapshot.Notes
Because
stateis a field, struct literals must supply it (ModelState::new()). Prefer constructor functions likeUser::new(..). A derive macro that hides this is in the backlog.Model#3Queryterminals and a singlehydratepath #14