API
Actions
updateResources()
updateResources(payload: Object, storeUpdater: Function | Object)
payload GraphQL payload JsonAPI payload
storeUpdater
Redux: dispatch
MobX: @observable resources = {};
React Component State: setState
This function will take in a GraphQl or JsonAPI compliant object normalize it and send an update to the store adding the new resource data. Each resource will either be added to the appropriate resources store or replace the existing resource if one exists with the same id.
BlueChip will automatically detect which format the payload is and what state management system you are using from storeUpdater.
updateResource()
updateResource(resource: Object, storeUpdater: Function | Object)
resource
{ id, type, attributes, relationships }
storeUpdater
Redux: dispatch
MobX: @observable resources = {};
React Component State: setState
This function takes a single resource and adds or updates it in the proper resource store.
Here is an example resources object:
removeResource()
removeResource(resource: Object, storeUpdater: Function | Object)
resource
{ id, type }
storeUpdater
Redux: dispatch
MobX: @observable resources = {};
React Component State: setState
Removes a resource from the store by id.
BaseModel
Create models by extending form BaseModel
hasMany()
To define hasMany relationships, define a static getter function like so:
Defining this function will dynamically add functions that can be called on model instances to retrieve related data. In this example you could now the following to get an array of RelatedModel instances.
belongsTo()
TODO
toObject()
TODO
customInstanceFunction()
To define custom instance functions, just simply define the functions on the model class.
Now you can call this function on any instance returned form the selector api.
ORM Selectors
query()
query(resources: Object) => Query
resources
{ exampleResources: { id, type, attributes, relationships } }
Query Instance of the Query class
This method is always called first when querying for resources. It takes in the whole resources store and returns a Query
instance which you will be able to chain the other selector methods onto.
all()
all() => Query
Query Instance of the Query class
This method returns a Query that can be converted into the list of all the resources in the store for that resource type.
where()
where(params: Object) => Query
params
{ exampleParameter: "Value that will be used to filter by" }
Query Instance of the Query class
The where
method allows you to filter the resources you are selecting.
find()
find(id: string | number) => ModelInstance
id
1
or "1"
The find
method returns a model instance with the id that was passed to the method.
whereRelated()
whereRelated(relationship: ModelClass, params: Object) => Query
relationship Related model class
params
{ exampleParameter: "Value that will be used to filter by" }
Query Instance of the Query class
The whereRelated
method allows you to filter resources by related model attributes.
includes()
includes(relationships: Array<string>) => Query
relationships
["exampleResources"]
Query Instance of the Query class
The includes
method will setup the query object to included the related models when converting it to an object.
This example will return an array of objects with the related models.
toModels()
toModels() => Array<ModelInstance>
Array<Modelnstance> An array of model instances
This function is what executes the query converting it into an array of model instances.
toObjects()
toObjects() => Array<Object>
Array<Object> An array of objects.
This function is what executes the query converting it into an array of objects.
first()
first() => ModelInstance
This function returns the first object in the store.
last()
last() => ModelInstance
This function returns the first object in the store.
Last updated