Skip to content

3. DataService

shturec edited this page Oct 26, 2017 · 11 revisions

under construction. check back later

DataService

Module: arestme/data_service

Usage

DataService maps the HTTP contract defined by HTTP Data Service protocol to concrete handler functions provided by a HandlersProvider object. It expects the HandlersProvider to expose a method with a specific name for each protocol operation that it will support. Upon request, the corresponding function is invoked with arguments context object (with request path parameters if any and query parameters map) and io object (with current request and response objects).

The mapping between HTTP Data Service operations and HandlerProvider functions is the following:

  • List Entities - query
  • Get Entity - get
  • Create Entity - create
  • Count - count
  • Get Entity association - associationListGetHandler
  • Remove - remove
  • Update - update

DataService will setup resource handler configurations only for the available functions so you can choose which operations would you like to support or not.

There is a default HandlersProvider called DAOHandlersProvider, which maps the handler functions required by DataService to functions exposed by data access object such as daoism/DAO. For example, the HandlersProvider function query will validate and adapt the query and path parameters to arguments suitable for the daosim/DAO.list method, invoke it with these arguments, then adapt the results for streaming and finally send back HTTP response. In case you intend to use the default HandlersProvider you don't need to provide one to the DataService constructor. Instead you are expected to supply a daosim/DAO-like object that will be invoked in the fashion described above.

Example using the default DAOHandlersProvider

In ScriptingServices/forums.js provide the following code
//create an object similar to DAOs from daoism (in this case we define a list method) or use a real daosim DAO.
var MyDAO = function(){};
MyDAO.prototype.list = function(){
    return [{id:1, text:'b'}];
};
MyDAO.prototype.count = function(){
    return this.list().length;
};
var svc = new DataService(new MyDAO())
svc.service();

Send an HTTP request:
GET /services/js/forums.js should return [{id:1, text:'b'}] 

For an example of using a custom Handlers provider, see the wiki for HandlersProvider

DataService API

Constructor

  • DataService

    • handler arestme/data_service/HandlerProvider Object | daoism/dao/DAO Object

      (required) If the first argument is HandlerProvider object, then it is the one used to bind to the DataService protocol requests. In case this is a DAO-like object then it is used to initialize the default DAOHandlerProvider. Note that there is not hard dependency to the daoism project except for functional compliance., i.e. the argument type is not checked against the DAO type. That means that you can provide any object that complies with the DAO function definitions from daoism and it will fit just fine.

    • loggerName String

      (optional) By default the logger name for DataService instances is "Data Service". Use this parameter to override the logger name.

Properties

  • logger logs/Logger Object

    The logs/Logger instance for this DataService instance.

  • handlersProvider arestme/data_service/HandlersProvider Object

    The HandlersProvider used by this DataService instance. It supplies DataService with the handler functions that will be used to setup the resource handling configuration in the service.

  • handlers Object

    An object mapping of handler function names expected by DataService and functions provided by its HandlersProvider. The DataService constructor will install a resource handler specification for each existing function accordingly. This means that you can omit automatically some endpoints if you provide a handlersProvider that does not feature them.

Methods

See the inherited HttpController methods.