# API

## Actions

### **updateResources()**

**`updateResources(payload: Object, storeUpdater: Function | Object)`**

***payload***\
GraphQL payload\
JsonAPI payload

***storeUpdater***\
\&#xNAN;*Redux*: `dispatch`\
\&#xNAN;*MobX*: `@observable resources = {};`\
\&#xNAN;*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***\
\&#xNAN;*Redux*: `dispatch`\
\&#xNAN;*MobX*: `@observable resources = {};`\
\&#xNAN;*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:

```javascript
const resource = { id: 1, type: "tasks", attributes, links, relationships }
```

### removeResource()

**`removeResource(resource: Object, storeUpdater: Function | Object)`**

***resource***\
`{ id, type }`

***storeUpdater***\
\&#xNAN;*Redux*: `dispatch`\
\&#xNAN;*MobX*: `@observable resources = {};`\
\&#xNAN;*React Component State*: `setState`

Removes a resource from the store by id.

## BaseModel

Create models by extending form BaseModel

```javascript
import { BaseModel } from "blue-chip";

export default class ExampleModel extends BaseModel {}
```

### hasMany()

To define hasMany relationships, define a static getter function like so:

```javascript
export default class ExampleModel extends BaseModel { 
  static get hasMany() { return [RelatedModel]; }
}
```

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.

```javascript
const relatedModels = ExampleModel.query(resources).find(1).relatedModles().toModels();
```

### belongsTo()

TODO

### toObject()

TODO

### customInstanceFunction()

To define custom instance functions, just simply define the functions on the model class.

```javascript
export default class Person extends BaseModel { 
  fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}
```

Now you can call this function on any instance returned form the selector api.

```javascript
const person = Person.query(resources).first();
person.firstName // returns "Mike"
person.lastName // returns "Piccolo"
person.fullName() // returns "Mike Piccolo"
```

## 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.

```javascript
Person.query(resources).all().toObjects() 
// returns an array of all the person resources in the store.
```

### 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.

```
BlogPost.query(resources).where({active: true}).toModels();
```

### 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.

```
const activeTasks = Task.whereRelated(Checklist, {active: true}).toModels();
```

### 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.

```javascript
Post.query(resources).includes(["comments"]).toObjects();
```

This example will return an array of objects with the related models.

```javascript
{
  id: 1,
  name: "Example Post",
  comments: [
    {id: 1, text: "Example comment"}
  ]
}
```

### 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.
