- Notifications
You must be signed in to change notification settings - Fork8
Batch multiple Feathers service calls into one
License
feathersjs-ecosystem/feathers-batch
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Batch multiple Feathers service calls into one
feathers-batch allows to batch multiple service requests into one. This is useful for minimizing client side requests to any Feathers API and can additionally speed up batched requests by onlyperforming authentication once.
It also comes with a client side module that automatically collects API requests from aFeathers client into a batch.
feathers-batch consists of two parts:
- The server sidebatch service to execute batch calls
- The client sidebatch client to collect parallel requests from aFeathers client into a batch service request
npm install feathers-batch --saveThe batch service is a normal Feathers service that executes the batch calls.
It can be registered by adding the following to yoursrc/services/index.js|ts:
const{ BatchService}=require('feathers-batch');module.exports=function(app){// ...app.use('batch',newBatchService(app));}
Now multiple service calls can be made by sending acreate (POST) call to/batch with a{ calls: [] } property.calls is an array in the same format as theSocket.io direct connection events:
{"calls":[["method","serviceName",/* list of parameters */], ...]}
Note: When using a Feathers client with thebatch client this will be done automatically.
For example, the following will execute a batch call toapp.service('users').get(1, { query: { active: true } }) andapp.service('messages').find({ query: { userId } }):
{"calls":[["get","users",1,{active:true}],["find","messages",{ userId}]]}
The return value will be the information as returned byPromise.allSettled:
[{"status": :"fulfilled","value":{/* user object returned by app.service('users').get(1) */}},{"status": :"fulfilled","value":{/* page returned by app.service('messages').find({ query: { userId }}) */}}]
If an error happened:
[{"status": :"fulfilled","value":{/* user object returned by app.service('users').get(1) */}},{"status": :"rejected","reason":{/* error JSON or object with error message */}}]
If you are batching authenticated requests, it is possible to perform the authentication step only once (instead of on every service call) in a batch by adding theauthenticate hook to the batch servicecreate method:
app.service('batch').hooks({before:{create:[authenticate('jwt')]}});
feathers-batch also exports a client side module that can be used withFeathers on the client that automatically collects multiple requests that are made at the same time into a single batch call. This works for any transport mechanism (REST, Socket.io etc.).
Batching on the client can be enabled like this:
// If your module loader supports the `browser` package.json fieldimport{batchClient}from'feathers-batch';// Alternativelyimport{batchClient}from'feathers-batch/client';constclient=feathers();// configure Feathers client here// `batchClient` should be configured *after*// any other application level hooksclient.configure(batchClient({batchService:'batch'}));
Now you can continue to make normal service calls and whenever possible they will be automatically combined into a batch (seeparallelizing requests for more information).
The following options are available for thebatchClient:
batchService(required) - The name of the batch serviceexclude(optional) - An array of service names that should be excluded from batchingtimeout(optional) (default:50) - The number of milliseconds to wait when collecting parallel requests.
At the same time means e.g. multiple components making requests to the API in parallel. The following example willNOT be collected into a batch since the calls run sequentially usingawait:
constuser=awaitclient.service('users').get(userId);constmessages=awaitclient.service('messages').find({query:{ userId}});
If the requests are not dependent on each other and you want to batch them,Promise.all needs to be used:
const[user,messages]=awaitPromise.all([client.service('users').get(userId),client.service('messages').find({query:{ userId}})]);
Copyright (c) 2020 Feathers contributors
Licensed under theMIT license.
About
Batch multiple Feathers service calls into one
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors8
Uh oh!
There was an error while loading.Please reload this page.