Mobx collection store
Model definition
This is a complete migration guide. If you want to move to datx
as fast and possible and refactor later, check out CompatCollection
and CompatModel
.
TypeScript
-- import { Collection, Model } from 'mobx-collection-store';
++ import { Collection, Model, prop } from 'datx';
import { computed } from 'mobx';
class Person extends Model {
public static type = 'person';
-- public static refs = {
-- spouse: 'person',
-- pets: {model: 'pet', property: 'owner'}
-- };
-- public static defaults = {
-- firstName: undefined, // Required to make the props observable
-- lastName: undefined, // Required to make the props observable
-- age: 0
-- };
++ @prop
public firstName: string;
++ @prop
public lastName: string;
++ @prop.defaultValue(0)
public age: number;
++ @prop.toOne(Person)
public spouse: Person;
++ @prop.toMany(Pet, 'owner')
public pets: Array<Pet>;
@computed public get fullName(): string {
return `${this.firstName} ${this.lastName}`;
}
}
For @prop
details check out the prop documentation.
JavaScript without decorators
-- import { Collection, Model } from 'mobx-collection-store';
++ import { Collection, Model, setupModel } from 'datx';
import { computed } from 'mobx';
-- class Person extends Model {
++ const Person = setupModel(Model, {
-- public static type = 'person';
++ type: 'person',
-- public static refs = {
++ references: {
spouse: 'person',
pets: {model: 'pet', property: 'owner'}
-- };
++ },
-- public static defaults = {
++ fields: {
firstName: undefined, // Required to make the props observable
lastName: undefined, // Required to make the props observable
age: 0
-- };
++ }
}
Collection changes
To migrate your collections, you should move to either Collection
(ideally) or CompatCollection
:
Collection
Changed:
insert
can receive only an array, not a single objecttoJS
was renamed totoJSON
- Removed
patchListen
andapplyPatch
- Removed type getters - use
fetchAll
instead
CompatCollection
Changed:
insert
can receive only an array, not a single object- Removed
patchListen
andapplyPatch
Model changes
Changed:
To migrate your models, you should move to either Model
/PureModel
(ideally) or CompatModel
:
CompatModel
The CompatModel class was created to take advantage of most datx
features, but keeping the configuration almost the same as before:
Same:
assign
- Stays the sameassignRef
- Stays the samesnapshot
- Stays the sametoJS
- Stays the samegetRecordId
- Stays the samegetRecordType
- Stays the same
Changed:
patchListen
- RemovedapplyPatch
- Removed- Reference ids - Instead of
model[refName + 'Id']
usegetRefId(refName)
__collection
(undocumented) - Stays the same, but shouldn't use it anyway...
Model
The Model class is exposing some methods equivalent to the old Model methods:
Same:
assign
- Stays the sameassignRef
- Stays the same
Changed:
snapshot
- Usemodel.meta.snapshot
toJS
- Renamed totoJSON
or usemodel.meta.snapshot
patchListen
- RemovedapplyPatch
- RemovedgetRecordId
- Usemodel.meta.id
getRecordType
- Usemodel.meta.type
- Reference ids - Instead of
model[refName + 'Id']
usemodel.meta.refs[refName]
__collection
(undocumented) - Usemodel.meta.collection
PureModel
The PureModel class doesn't expose anything directly. Instead, you need to use helper methods:
Changed:
assign
- UseassignModel(model, key, value)
assignRef
- UseupdateModel(model, data)
snapshot
- UsemodelToJSON(model)
toJS
- UsemodelToJSON(model)
patchListen
- RemovedapplyPatch
- RemovedgetRecordId
- UsegetModelId(model)
getRecordType
- UsegetModelType(model)
- Reference ids - Instead of
model[refName + 'Id']
usegetRefId(refName)
__collection
(undocumented) - UsegetModelCollection(model)