Class: ResourceCollection

ResourceCollection

A pagination aware collection of Resource objects.

This class should not be used directly, but should instead be obtained from ResourcefulEndpoint methods


<private> new ResourceCollection(rawData, initialCriteria, resourcefulEndpoint)

Parameters:
Name Type Description
rawData Object

Raw JSON response from the Sequoia endpoint

initialCriteria string

the initial filter criteria used when requesting from this endpoint

resourcefulEndpoint ResourcefulEndpoint

The ResourcefulEndpoint that created this ResourceCollection

Properties:
Name Type Description
rawData Object

Raw JSON response from the Sequoia endpoint

initialCriteria string

the initial filter criteria used when requesting from this endpoint

resourcefulEndpoint ResourcefulEndpoint

The ResourcefulEdnpoint that created this ResourceCollection

collection Array.<Resource>

Array of Resource objects

Since:
  • 0.0.2
Source:
Returns:
Type
Resource

Members


collection

Properties:
Type Description
Array.<Resource>

Array of Resource objects

Source:

initialCriteria

Properties:
Type Description
string

the initial filter criteria used when requesting from this endpoint

Source:

page :number

Current page in the pagination set

Type:
  • number
Source:

perPage :number

Number of items per page in the pagination set

Type:
  • number
Source:

resourcefulEndpoint

Properties:
Type Description
ResourcefulEndpoint

The ResourcefulEndpoint that created this ResourceCollection

Source:

totalCount :number

Total number of Resources in the catalogue matching our criteria

Type:
  • number
Source:

Methods


add(data)

Add a Resource to the local collection Note: this method does not implement any uniqueness constraints on the refs of objects/Resources being added. If this is required, use ResourceCollection#findOrCreate

Parameters:
Name Type Description
data Object | Resource

Can be either an existing Resource or a JSON object to create a new Resource from

Source:
See:
Returns:
Type
Resource

destroy()

Destroy (DELETE) all the Resources in this ResourceCollection. If the ResourceCollection has more items than the batchSize specified in the descriptor, multiple calls will be made to the backend

Source:
See:
Returns:
Type
Promise
Example

Destroy all content that went out of availability this year

contents.all(where(field("availabilityEndAt").lessThan("2017"))).then(resources => resources.destroy());

explode( [size])

Get an array of ResourceCollections populated with a maximum of the size paramater Resources in each.

Sequoia has limits to how many Resources can be saved at once. This method is used internally by ResourceCollection#save and ResourceCollection#destroy to send the right amount of data.

Parameters:
Name Type Argument Default Description
size number <optional>
ResourcefulEndpoint#batchSize

The number of Resources to return in each ResourceCollection

Source:
See:
Returns:
Type
Array.<ResourceCollection>

<private> fetch(newCriteria)

Update the collection with new data from the server. This will also return the new ResourceCollection as a convenience.

Parameters:
Name Type Argument Description
newCriteria string <nullable>

query string criteria to send to ResourcefulEndpoint#browse

Source:
Returns:
Type
Promise

find(ref)

Returns a Resource if it exists in the local collection with the supplied ref

Parameters:
Name Type Description
ref string

The ref of the resource to find

Source:
Returns:
Type
Resource

findOrCreate(resource)

Find a resource in the local collection or create (and add to the local collection) a Resource from the supplied object

Parameters:
Name Type Description
resource Resource | Object

the resource to find or create

Source:
Returns:
Type
Resource

findWhere(criteria)

Find a Resource in the local collection or null if not found

Parameters:
Name Type Description
criteria Criteria

See ResourceCollection#where

Source:
See:
Returns:
Type
Resource

firstPage()

Fetch the first page of results

Source:
See:
Returns:
Type
Promise

getPage(pageNumber)

Fetch a specific page of results

Parameters:
Name Type Description
pageNumber number

the number of the page to fetch

Source:
See:
To Do:
  • Boundary checking?
  • This could likely be easier to use with the initialCriteria
Returns:
Type
Promise

hasNextPage()

Indicates whether there is next page of results available

Source:
Returns:

true - if there is a next page


lastPage()

Fetch the last page of results

Source:
See:
Returns:
Type
Promise

nextPage()

Fetch the next page of results

Source:
See:
Returns:
Type
Promise

previousPage()

Fetch the previous page of results

Source:
See:
Returns:
Type
Promise

remove(ref)

Remove a Resource from the local collection Will return the found Resource or null if it does not exist

Parameters:
Name Type Description
ref string

the ref of the resource to remove

Source:
Returns:
Type
Resource

save( [size])

Save (create or update) (POST/PUT) all the Resources in this ResourceCollection. If the ResourceCollection has more items than the batchSize specified in the descriptor (or supplied batchSize), multiple calls will be made to the backend

Parameters:
Name Type Argument Default Description
size number <optional>
ResourcefulEndpoint#batchSize

The number of Resources to save in each request

Source:
See:
Returns:
Type
Promise

serialise()

Get a stringified version of this ResourceCollection that is suitable for saving to sequoia.

Simply wraps the JSON of the resource collection as an array in the [] property

Source:
Returns:
Type
string

<private> setData(rawData)

Updates rawData with the json returned from the Sequoia service and sets collection to an array of `Resources

Linked resources are collated into the individual Resources created for each item in the collection if they have relationship info. e.g. Assets that are linked to Contents will have a contentRef - if this is present, each Content instance will have a linked.assets[] with only the related Assets

If there is no relationship specified in linked resources, (e.g. linked Customers against Subscriptions from the Payment service) then each Subscription instance will have all of the Customers avaliable as linked.customers[]

Parameters:
Name Type Description
rawData Object

json from a Sequoia browse request

Source:
Returns:
  • self
Type
ResourceCollection

toJSON()

Get a JSON representation of this collection's keys/values

Source:
Returns:
Type
Object

validate()

Validate all the Resources in this collection.

Source:
See:
Returns:
Type
Promise
Example
resourceCollection.validate().catch((resource) => {
  // Show resource.errors[]
}).then(resourceCollection => resourceCollection.save())
.then(() => {
  // do something on successfully saving
});

where(criteria)

Find a Resource in the local collection or null if not found The below examples assume the variable contents is populated with the result of client.service('metadata').then(s => s.resourcefulEndpoint('contents').all())

Parameters:
Name Type Description
criteria Criteria

The criteria to use for filtering the local collection

Source:
See:
Returns:
Type
Array.<Resource>
Examples

Find all with a custom filter function

// Find all of the Resources that have a title including 'die hard' that haven't had any tags applied yet
contents.where(r => r.title.toLowerCase().includes('die hard') && !Array.isArray(r.tags))

Find all that match a given object

// Find all of the Resources that are active and have a type of 'show'
contents.where({ type: 'show', active: true })