A SDK for corbel compatible with browsers and node.
-
browser
bower install corbel-js --save # for environments without promises support bower install es6-promise --save -
node
npm install corbel-js --save
var corbelDriver = corbel.getDriver(options);var options = {
'clientId': 'clientId',
'clientSecret': 'clientSecret',
'audience': 'audience',
'urlBase': 'http://localhost:8080/{{module}}',
'scopes': 'scopes',
'device_id'
}corbelDriver.iam.token().create().then(function() {
return corbelDriver.resources.collection(collectionName).add('application/json', params);
}).then(function(resourceId) {
return corbelDriver.resources.resource(collectionName, resourceId).get();
}).then(function(response) {
console.log('resource', response.data);
}).catch(function(error) {
console.error('some.error', error);
});The Resources API is a flexible programming interface for retrieval of resource's representations. Using the patterns described by this API we can deploy any kind of resource in our Corbel ecosystem with minimal impact on clients and server code. A request can contain URL parameters which can modify the content of representation returned or its transmission to the client. Parameter names must be specified on using its canonical form.
*More info: http://docs.silkroadresources.apiary.io/
https://confluence.bq.com/pages/viewpage.action?title=SilkRoad+-+Resources+API&spaceKey=SILKROAD*
Resources API
Resources is exposed to corbelDriver instance and It has static methods and variables inside corbel namespace:
-
Statics properties and methods in corbel.Resources:
-
create: Factory method for instantiating a Resources object
-
sort: Sort constants object
corbel.Resources.sort.ASC corbel.Resources.sort.DESC
-
ALL: Constant for use to specify all resources wildcard
corbel.Resources.ALL
-
-
Instance methods
-
collection: Collection factory method collection('collectionName')
corbelDriver.resources .collection('collectionName')
-
resource: Resource factory method resource('resourceName',id)
corbelDriver.resources .resource('resourceName',id)
-
relation: Relation factory method relation('sourceResourceName', sourceResourceId, 'destResourceName')
corbelDriver.resources .relation('sourceResourceName', sourceResourceId, 'destResourceName')
A collection is a container of resources that share the same type. For instance:
/resource/music:Album => All resources of type music:Album
/resource/book:Book => All resources of type book:Book
/resource/music:Artist => All resources of type music:Artist
Collection API
-
Factory method
corbelDriver.resources .collection('collectionName')
-
get: Gets a collection of elements, filtered, paginated or sorted (requestOptionsObject)
corbelDriver.resources .collection('collectionName') .get(requestOptionsObject)
-
add: Adds a new element to a collection (objectData,requestOptionsObject)
corbelDriver.resources .collection('collectionName') .add(objectData,requestOptionsObject) .then(function(resourceCreatedId){ // });
Examples:
collection.add({
//related data
name: 'New model name',
lastName: 'New model last name'
},{
//request options
}).then(function(idNewModel){ });Collection request params API
Following params can be passed both as request options object and as chainable methods:
-
Pagination:
{ pagination: { page: 1, pageSize: 5 } } -
Aggregations:
{ aggregation: { $count: '*' } } -
Sort:
{ sort: { title: corbel.Resources.order.sort } } -
Querys:
{ query: [{$like: {} }, {$:{} } ] }
Examples:
var collection = corbelDriver.resources
.collection('books:book');
collection.get({
//request options
query: [{
'$like': {
'name': 'Default name'
}
}]).then(function(collectionData){ });
collection.get({
//request options
dataType: 'application/json',
pagination: {
page: 1,
size: 7
},
{
sort: {
title: corbel.Resources.order.sort
}
}
}]).then(function(collectionData){ });Relation API
-
Factory method
corbelDriver.resources .relation('resourceName', srcId, 'relationName')
-
get: List elements of a resource's relation (destId, requestOptionsObject)
corbelDriver.resources .relation('resourceName',15,'relationName') .get(destId, requestOptionsObject)
-
add: Add new relation (destId, relationData, requestOptionsObject)
corbelDriver.resources .relation('resourceName',15,'relationName') .add(destId, relationData, requestOptionsObject)
-
move: Move a relation (destId, pos, requestOptionsObject)
corbelDriver.resources .relation('resourceName',15,'relatio nName') .move(destId, pos, requestOptionsObject)
-
delete: Delete a relation (destId, requestOptionsObject)
corbelDriver.resources .relation('resourceName',15,'relationName') .delete(destId, requestOptionsObject)
Examples:
var relation = corbelDriver.resources
.relation('books:book','id1','id2');
relation.get('destId', {
//request options
}).then(function(collectionData){ });
relation.add('15658', {
//related data
name: 'New model name',
lastName: 'New model last name'
}, {
//request options
query: [{
$eq:{
'name': 'Juanfran'
}
}]
}).then(function(data){ });
relation.move('15658','pos', {
//request options
}).then(function(){ });
relation.delete('15658', {
//request options
}).then(function(){ });Relation request params API
The same request params previously listed in the collection API.
A resource is a single object in a collection. For instance
/resource/music:Album/123 => The representation of a single object of type music:Album whose identifier is 123
Resources API
-
Factory method
corbelDriver.resources .resource('resourceName', resourceId)
-
get: Get a resource representation (requestOptionsObject)
corbelDriver.resources .resource('resourceName',resourceId) .get(requestOptionsObject)
-
update: Update a resource (resourceData, requestOptionsObject)
corbelDriver.resources .resource('resourceName',resourceId) .update(resourceData, requestOptionsObject)
-
delete: Delete a resource (requestOptionsObject)
corbelDriver.resources .resource('resourceName',resourceId) .delete(requestOptionsObject)
Examples:
var resource = corbelDriver.resources
.resource('books:book', 15);
resource.get({
//request options
}).then(function(resourceData){ });
resource.update('resource', {
//related data
name: 'Update model name',
lastName: 'Update model last name'
}, {
//request options
}).then(function(data){ });
resource.delete('resourceId', {
//request options
}).then(function(){ });####Assets
Assets are dynamic groups of scopes that a user can have for a certain period of time.
This is an example of an asset:
{
_id: "0b2dff5c39934c210d80056a94eb27bc6c6d6378",
userId: "fooid",
name: "assettest13",
productId: "producttest13",
expire: ISODate("2025-01-01T00:01:00Z"),
active: true,
scopes: [
"ec:purchase:user"
]
}Imagine assets as a group of items that extend the user permissions. For example, a user buys a digital good, then an asset is created with the permission for accessing that digital book.
When you login a user, you may want to load the assets of the user (by default they are not loaded because we try to mantain each service isolated).
For doing that, you can access corbel assets with corbelDriver this way:
var corbelDriver = corbel.getDriver({
iamToken : {
'accessToken' : 'USERACCESSTOKEN'
},
baseUrl : 'CORBELURL'
});
//Enable the user permissions for that accessToken
corbelDriver.assets().access();You can also request all the user assets if you want, for example, listing al the goods that a user has purchased:
corbelDriver.assets().get();Assets API
-
Factory method
corbelDriver.assets()
-
get: Get all the assets (requestOptionsObject)
corbelDriver.assets() .get(requestOptionsObject)
-
create: Create an asset (assetData)
corbelDriver.assets() .create(assetdata)
-
delete: Delete an asset (assetId)
corbelDriver.assets(assetId).delete();
You can use a chainable api to set defaults parameters over any kind of resource:
Example:
var collection = corbelDriver.resources
.collection('books:book');
collection
.like('name','default name')
.page(5)
.pageSize(7)
.get();The parameters specified with the chainable api will be removed when a corbel-request is maden.
var collection = corbelDriver.resources
.collection('books:book');
collection
.like('name','default name')
.page(5)
.pageSize(7)
.get();
//Collection doesn't have the defaults chainable params
//this get request will not use any request params previously defined
collection.get();corbel.jwt.generate(claimsObject, secret);
corbel.request.send(params);
corbel.Resources
// ... more


