What are Services?
Services are the core building blocks of a Feathers application. A service is simply an object or class that implements one or more of the standard service methods for CRUD operations.Service Methods
Feathers services can implement the following standard methods:find(params)- Retrieve multiple recordsget(id, params)- Retrieve a single record by IDcreate(data, params)- Create one or more recordsupdate(id, data, params)- Replace a recordpatch(id, data, params)- Merge data into a recordremove(id, params)- Remove a record
Creating a Basic Service
The simplest way to create a service is with an object that implements service methods:Service methods must be
async or return a Promise. Synchronous methods are not supported.Registering Services
Register services with your application using theapp.use() method:
1
Choose a Path
The first argument is the service path - this will be the endpoint for your service:
2
Provide the Service
The second argument is your service object or class instance:
3
Add Service Options (Optional)
The third argument allows you to configure service behavior:
Accessing Services
Once registered, access services usingapp.service():
Accessing Services
Service Parameters
All service methods receive aparams object as their last argument. This object can contain:
query- Query parameters for filtering, sorting, paginationuser- The authenticated user (if using authentication)provider- The transport used (rest, socketio, etc.)- Custom properties added by hooks or middleware
Using Parameters
Memory Service
Feathers provides a built-in in-memory service adapter for prototyping and testing:Memory Service
Database Adapters
Feathers provides adapters for popular databases that implement all service methods:Service Options
When registering a service, you can configure its behavior with options:Service Options
Default Service Events
By default, services emit events for data-modifying operations:created- Aftercreate()updated- Afterupdate()patched- Afterpatch()removed- Afterremove()
Listening to Service Events
Custom Service Methods
You can add custom methods to your services beyond the standard CRUD operations:Custom Methods
Service Lifecycle
Services can implementsetup() and teardown() lifecycle methods:
Service Lifecycle
setup(app, path)- Called when the application starts or when the service is registered after startupteardown(app, path)- Called when the application shuts down
Removing Services
You can dynamically remove services usingapp.unuse():
Removing Services
Complete Service Example
Here’s a complete example of a custom service with validation and lifecycle methods:Complete Service Example