Service Basics
Services
One of the main concepts within Angel, which is borrowed from FeathersJS, is a service. You more than likely have already dealt with another implementation of the service concept. In Angel, a service is a class that acts as a Web interface and exposes CRUD actions operating on a set of data. Angel services extend Routable
, and thus can be mounted on a certain path and become REST endpoints.
The Angel core includes the Service
base class, as well as two in-memory service classes. Database adapter packages, such as package:angel_mongo
include service classes that let you interact with a database without writing complex code yourself.
Services can also be filtered or reacted to with service hooks.
A service looks like this:
Service Parameters and Middleware
You might notice that each service method accepts an optional Map
of parameters. When accessed via HTTP (i.e., not over Websockets), req.query
or req.body
is passed here (query
for index
, read
and delete
, body
for create
, update
and modify
). To pass custom parameters to a service, you should create a middleware to do so. @Middleware
annotations can be prepended to service classes or service methods. For example, the following will pass foo='bar'
to every method in the service:
Additionally, when accessed by a client, params
will contain a field called provider
.
provider
will be a Providers
class, whose String via
will tell you where the service is being accessed from, i.e. 'rest'
or 'websocket'
.
Mounting Services
As mentioned above, services extend Routable
, so you can simply app.use()
them. You can also supplement them with additional routes or middleware, placed before the mounting of a service:
Next Up...
Reflectively serialize and deserialize data within services by wrapping them in a TypedService
.
Last updated