-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Design pattern: versioning #52
Comments
That's why "fromSnapshot" in models are there. E.g. // first version
@model("...")
class M extends Model({
}) {
_version: prop(1)
fullName: prop<string>()
} {}
// second version
@model("...")
class M extends Model({
}) {
_version: prop(2)
firstName: prop<string>(),
lastName: prop<string>()
} {
fromSnapshot(maybeOldSnapshot: any) {
if (maybeOldSnapshot._version >= 2) {
return maybeOldSnapshot // up to date
}
// update to latest
return {
_version: 2,
firstName: maybeOldSnapshot.fullName.split(" ")[0],
lastName: maybeOldSnapshot.fullName.split(" ")[1],
}
}
} That way whenever an old snapshot is loaded with Also this way parent models don't have to worry about the versioning of its children, just about their own. |
I implemented some kind of persist library with built-in support for migrations for mobx-state-tree. The idea here is to have The The And the With mobx-keystone, it should work even better since we have the exact model name inside each entry in the snapshot, so it might help to resolve and work with it. |
See #326 for the latest API related to versioning. |
@xaviergonz, regarding #52 (comment): is there a preferred, canonical way to handle more complex scenarios?
|
I've been thinking how to deal with model versioning. The scenario I'm looking at is saving a snapshot of the current state in local storage, so when the browser is closed and re-opened (or the app is reloaded), the previous state is automatically loaded again. All is good as long as the state layout does not change. But what happens when a snapshot is saved, I change a model class (or multiple model classes, or the entire state layout), re-deploy the app, and then the snapshot, which has the old layout, is loaded by the new state implementation? A way to migrate from one/any (old) snapshot version to the current version is needed, I think.
A couple of questions I've been asking myself:
fromSnapshot
method?The text was updated successfully, but these errors were encountered: