Class: BaseService

BaseService

This class provides the base for all concrete Service objects used within the context of a VISTA application. The base class sets up and provides several functional utilities behind the scenes:

  • Data Access API
  • Event Emitter (and auto-emit on access through the Data Access API)
  • Context Wrapping

While Data Access and Event Emitting are pretty self-explanatory, Context Wrapping bears some explanation. Each concrete Service is intended to exist as a singleton. However, there may be cases where we want to execute API functionality in the context of session variables. This can be accomplished via the Context Wrapping API:

concreteService.withContext(session).find(queryOptions);

new BaseService(domainName, serviceName)

Constructor

Parameters:
Name Type Description
domainName string

Domain of the service (e.g. 'VITALS', 'ALLERGIES', etc.)

serviceName string

Optional specific name of this service. This name will be used to register the service instance with the Service manager.

Methods


<static> getGenericErrors()

Return the generic errors defined for all services.

Returns:

The list of generic errors (Array of Objects)

Type
Array

<static> loadServiceAPI(classDef, dirName)

Dynamically add API functionality from a JS modules within a specified directory

Parameters:
Name Type Description
classDef Object

Class definition to

dirName String

Name of the directory relative to the class to run


cloneService()

Create a temporary shallow clone of the service object. This is a utility method used by the context wrapper methods.

Returns:

A shallow clone of the service object.

Type
object

count(dataType, criteria, cb)

Count objects in a collection according to a specified criteria

Parameters:
Name Type Description
dataType string

The name of the data class to perform the data query on.

criteria object

The search criteria used for the count (default: empty object)

cb function

Callback to call on completion, will return the count in the second parameter

Returns:

Promise object.

Type
Promise

create(dataType, data, cb)

Perform a create type operation for an object on the data store.

Parameters:
Name Type Description
dataType string

The name of the data class to perform the data query on.

data object

The object to save to the data store. The object must be compatible, schema-wise with the data class specified in the 'dataType' parameter.

cb function

Callback to call on completion

Returns:

Promise object if no callback is defined

Type
Promise

delete(dataObj, cb)

Delete an object in the data store

Parameters:
Name Type Description
dataObj Object

The object to delete in the data store. This should have been retrieved previously by either a 'find' or a 'create'.

cb function

Callback to call on completion

Returns:

Promise object.

Type
Promise

disableEmitEvent()

Disable auto-emit of events on data access. By default, successes and failures via the data access API automatically trigger events to be emitted. Calling this method will turn off the auto-emit functionality of the service.


emitContextEvent(event, context, data)

Emit an event with a context object. This method will emit an event through the object using the data specified in the parameters.

Parameters:
Name Type Description
event string

Name of the event to emit

context object

Context/Session object to include in with the event

data object

Data object to send with the event (will be sent in the 'data' field)


emitEvent(event, data)

Emit an event, using the member 'context' object as the event context.

Parameters:
Name Type Description
event string

Name of the event to emit

data object

Data object to send with the event (will be sent in the 'data' field)


error(input, optionalArg)

Create a ServiceError

Parameters:
Name Type Description
input number | string

The service specific id or name field

optionalArg string

Optional argument to include in ServiceError output

Returns:

The created ServiceError with id, type, name, description, optionalArg (optional) properties

Type
Object

find(dataType, options, cb)

Perform a read/query type operation against the data store.

Parameters:
Name Type Description
dataType string

The name of the data class to perform the data query on.

options object

Query options

Properties
Name Type Description
query object

MongoDB-style query object.

projection object

MongoDB-style projection object.

options object

MongoDB-style options object.

cb function

Callback to call on completion

Returns:

Promise object.

Type
Promise
Example
// Query an Accession object with 'id' field containing 'NAME', return only the 'name', at most, 5 records
svc.find('Accession', {
    query: { id: 'NAME' },
    projection: { _id: 0, name: 1 },
    options: { limit: 5 }
});

getErrors()

Return the error mapping for current service

Returns:

The list of errors for this service (Array of Objects)

Type
Array

setContext(context)

Set the context of the service object.

Parameters:
Name Type Description
context object

The context object to use.


setDataAccess(dataAccess)

Set the data access object manually.

Parameters:
Name Type Description
dataAccess object

The data access object to use.


update(dataObj, cb)

Update an object in the data store

Parameters:
Name Type Description
dataObj object

The object to update in the data store. This should have been retrieved previously by either a 'find' or a 'create'.

cb function

Callback to call on completion

Returns:

Promise object.

Type
Promise

withContext(context)

Utilty method to create a cloned version of the service object with the context member set. This is intended be used in a chained function call.

Parameters:
Name Type Description
context object

The context object to use.

Returns:

A shallow clone of the service object with the 'context' field set.

Type
object
Example
svc.withContext(ctx).doSomething();

withDataAccess(dataAccess)

Utilty method to create a cloned version of the service object with the data access member set. This is intended be used in a chained function call.

Parameters:
Name Type Description
dataAccess object

The data access object to use.

Returns:

A shallow clone of the service object with the 'data access' field set.

Type
object
Example
svc.withDataAccess(dao).doSomething();