Skip to content

Commit

Permalink
core: warn when model is added to a different collection.
Browse files Browse the repository at this point in the history
This can lead to inconsistencies in model data (if both collections try to persist the model)
  • Loading branch information
ivandotv committed Oct 22, 2021
1 parent 66d03e1 commit 082abd6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-schools-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuerte/core': patch
---

core: Warn when model is added to a different collection.
9 changes: 9 additions & 0 deletions packages/core/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ export abstract class Model<

// @internal
_onAdded(collection: TCollection): void {
if (__DEV__) {
if (this.collection) {
console.warn(
`Model=> onAdded: model is already in a different collection. This can lead to inconsistencies in model state.
Old collection: ${this.collection.constructor}
New collection: ${collection.constructor}`
)
}
}
this.collection = collection
this.onAdded()
}
Expand Down

0 comments on commit 082abd6

Please sign in to comment.