Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Batch multiple Feathers service calls into one

License

NotificationsYou must be signed in to change notification settings

feathersjs-ecosystem/feathers-batch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CIDependency StatusDownload StatusSlack Status

Batch multiple Feathers service calls into one

About

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:

npm install feathers-batch --save

Service

The batch service is a normal Feathers service that executes the batch calls.

Usage

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));}

Batch calls

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 */}}]

Authentication

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')]}});

Client

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.).

Usage

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).

Options

The following options are available for thebatchClient:

  • batchService (required) - The name of the batch service
  • exclude (optional) - An array of service names that should be excluded from batching
  • timeout (optional) (default:50) - The number of milliseconds to wait when collecting parallel requests.

Parallelizing 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}})]);

License

Copyright (c) 2020 Feathers contributors

Licensed under theMIT license.

About

Batch multiple Feathers service calls into one

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors8


[8]ページ先頭

©2009-2025 Movatter.jp