<private> new Resource(rawData, resourcefulEndpoint)
Parameters:
Name | Type | Description |
---|---|---|
rawData |
Object | the raw json response from Sequoia |
resourcefulEndpoint |
ResourcefulEndpoint | the ResourcefulEndpoint that this Resource is a member of |
- Source:
Returns:
- Type
- Resource
Example
const service = await client.service('metadata'); const contents = service.resourcefulEndpoint('contents'); // Get an existing resource: const content = await contents.readOne('some:ref'); // Create a new Resource in metadata/contents: const newContent = contents.newResource({ title: 'something', synopsis: 'a really long synopsis' }); // `service`, `content` and `newContent` are used throughtout the rest of the examples
Methods
-
destroy()
-
Destroy (DELETE) this Resource.
- Source:
- See:
Returns:
- Type
- Promise
-
<private> flush()
-
Flush (save) Resources to the backend. Performs the actual write of data to sequoia for save()
- Source:
- See:
Returns:
- the
fetch
Promise
- Type
- Promise
-
<private> getRelationshipFor(resource)
-
Get the relationship info from the descriptor. When passing in a Resource, the relationship will be inferred from its ResourcefulEndpoint#pluralName. Pass a string to be explicit. Note, when using a String value, this uses the key name in the descriptor's relationship info, not the resourceType.
Parameters:
Name Type Description resource
string | Resource A
string
or Resource to get relationship information forReturns:
- Type
- undefined
-
hasLinked(relationship)
-
Query where the current resource has linked items of a particular relationship kind.
Parameters:
Name Type Description relationship
string The name of the relationship in the descriptor e.g. 'assets', 'categories', 'parent'
- Source:
Returns:
- Type
- boolean
Example
content.hasLinked('assets'); content.hasLinked('categories');
-
link(resource, as)
-
Link one or many Resources with this Resource When linking an array of resources, if the relationship is not an array type the last member of the array will become the only link. For example,
content.link([...contents], 'parent')
will end up withresource.parentRef === contents[contents.length - 1].ref
. Due to this, it is expected to onlylink
certain resources at a time e.g.content.link(parentContent).link(assets).link(members, 'members').save()
Parameters:
Name Type Description resource
Array.<Resource> the Resource (or array or Resources) to link
as
string Override the relationship type for this link.
- Source:
Returns:
- the current Resource instance
- Type
- Resource
Example
const assets = service.resourcefulEndpoint('assets'); const asset = await assets.readOne('some:asset-ref'); // If it's a known relationship, you don't have to bother providing the // relationship type: content.link(asset).save(); // Link and save // You can link a ResourceCollection (or any iterable): const assets = await assets.browse(); // Get all assets already stored content.link(assets).save(); // Link them all and save // You can provide the relationship type: const relatedContent = await contents.readOne('some:other-ref'); content.link(relatedContent).save(); // Will default to the 'parent' relationship content.link(relatedContent, 'members').save(); // Will now be a 'member' of the content (e.g. episodes of a series)
-
<private> linkRefToResource(ref, relationship, resource)
-
Add a resource ref to the appropriate relationship so it is linked to this Resource.
Parameters:
Name Type Argument Description ref
string the Resource's
ref
to linkrelationship
Object The relationship from the descriptor obtained from Resource#getRelationshipFor
resource
Resource <nullable>
Specify this when linking an indirect relationship. Defaults to the current Resource instance
- Source:
- See:
-
<private> linkResource(resource, as)
-
Link a Resource with this Resource
If the link is
direct
, simply update this Reource with the relationship info. If the link isindirect
, add a potential link (not yet saved) to theindirectlLinkedResources
array to be flush when calling save() later.Parameters:
Name Type Description resource
Resource the Resource to link
as
string Override the relationship type for this link.
- Source:
- See:
-
save()
-
Save this Resource to the sequoia backend.
Any links that have also been applied in will also be saved at this point.
- Source:
- See:
Returns:
- Type
- Promise
-
<private> saveIndirectLinks()
-
Save any links that have been applied or resolve immediately if there are none
- Source:
Returns:
- Type
- Promise
-
serialise()
-
Get a stringified version of this Resource that is suitable for saving to sequoia.
Simply wraps the JSON of the resource as an array in the
[] property - Source:
Returns:
- Type
- string
Example
const resource = new Resource({ ref: 'test:testcontent', name: 'testcontent', title: 'A test resource' }, contents); resource.serialise(); // Returns: '{"contents":[{"ref":"test:testcontent","name":"testcontent","title":"A test resource"}]}'
-
toJSON()
-
Get a JSON representation of this Resources's keys/values
If this Resource is tied to a ResourcefulEndpoint it will take the keys and default values from the endpoint's descriptor. Otherwise it will return which ever keys/values have been populated.
- Source:
Returns:
- Type
- Object
-
validate()
-
Validate the resource.
This method uses Resource#validateField on each field in the descriptor and adds any invalid fields to the Resource.errors array for later querying.
- Source:
- See:
Returns:
- Type
- Promise
Example
contents.validate().catch((resource) => { // Show resource.errors[] }).then((resource) => { if (!resource.errors.length) { return resource.save().then(() => { // do something on successfully saving }); } return Promise.reject(resource); }).catch((resource) => { // Or show resource.errors[] here (or errors from save()ing) });
-
validateField(field)
-
Validate a specific field in the Resource.
Parameters:
Name Type Description field
string the field to validate e.g. 'ratings'
- Source:
Returns:
- Type
- Validation