corbel-js

Stories in Ready Build Status npm version Bower version Coverage Status Dependency status Dev Dependency Status

A SDK for corbel compatible with browsers and node.

Homepage

Quickstart

Instance a new driver

var corbelDriver = corbel.getDriver(options);

Driver options

var options = {
    'clientId': 'clientId',
    'clientSecret': 'clientSecret',

    'urlBase': 'http://localhost:8080/{{module}}',

    'scopes': 'scopes',

    'device_id'
}

Get an application token

corbelDriver.iam.token().create().then(function() {
    return corbelDriver.resources.collection(collectionName).add('application/json', params);
}).then(function(response) {
    var resourceId = response.data;
    return corbelDriver.resources.resource(collectionName, respurceId).get();
}).then(function(response) {
    console.log('resource', response.data);
}).catch(function(error) {
    console.error('some.error', error);
});

Resources

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:

Collection

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

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:

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){ });

Relations

Relation API

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.

Resources

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

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(){ });

Chainable API

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();

Manage application session

//get value from local storage
corbelDriver.session.get('key');

//add value to the local storage
corbelDriver.session.add('key', value, isPersistent);

//return true if the session driver is active
corbelDriver.session.gatekeeper();

//clear the current session data
corbelDriver.session.destroy();

//in nodejs, remove the directory/files of the current session
corbelDriver.session.removeDir();

library static methods

corbel.jwt.generate(claimsObject, secret);
corbel.request.send(params);
corbel.Resources
// ... more