BlueChip
Search…
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:
1
const resource = { id: 1, type: "tasks", attributes, links, relationships }
Copied!

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
1
import { BaseModel } from "blue-chip";
2
3
export default class ExampleModel extends BaseModel {}
Copied!

hasMany()

To define hasMany relationships, define a static getter function like so:
1
export default class ExampleModel extends BaseModel {
2
static get hasMany() { return [RelatedModel]; }
3
}
Copied!
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.
1
const relatedModels = ExampleModel.query(resources).find(1).relatedModles().toModels();
Copied!

belongsTo()

TODO

toObject()

TODO

customInstanceFunction()

To define custom instance functions, just simply define the functions on the model class.
1
export default class Person extends BaseModel {
2
fullName() {
3
return `${this.firstName} ${this.lastName}`;
4
}
5
}
Copied!
Now you can call this function on any instance returned form the selector api.
1
const person = Person.query(resources).first();
2
person.firstName // returns "Mike"
3
person.lastName // returns "Piccolo"
4
person.fullName() // returns "Mike Piccolo"
Copied!

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.
1
Person.query(resources).all().toObjects()
2
// returns an array of all the person resources in the store.
Copied!

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.
1
BlogPost.query(resources).where({active: true}).toModels();
Copied!

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.
1
const activeTasks = Task.whereRelated(Checklist, {active: true}).toModels();
Copied!

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.
1
Post.query(resources).includes(["comments"]).toObjects();
Copied!
This example will return an array of objects with the related models.
1
{
2
id: 1,
3
name: "Example Post",
4
comments: [
5
{id: 1, text: "Example comment"}
6
]
7
}
Copied!

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 modified 4yr ago