npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

feathers-batch

v1.1.1

Published

Batch multiple Feathers service calls into one

Downloads

1,184

Readme

CI Dependency Status Download Status Slack 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 only performing authentication once.

It also comes with a client side module that automatically collects API requests from a Feathers 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 your src/services/index.js|ts:

const { BatchService } = require('feathers-batch');

module.exports = function (app) {
  // ...
  app.use('batch', new BatchService(app));
}

Batch calls

Now multiple service calls can be made by sending a create (POST) call to /batch with a { calls: [] } property. calls is an array in the same format as the Socket.io direct connection events:

{
  "calls": [
    [ "method", "serviceName", /* list of parameters */ ],
    ...
  ]
}

Note: When using a Feathers client with the batch client this will be done automatically.

For example, the following will execute a batch call to app.service('users').get(1, { query: { active: true } }) and app.service('messages').find({ query: { userId } }):

{
  "calls": [
    [ "get", "users", 1, { active: true } ],
    [ "find", "messages", { userId } ]
  ]
}

The return value will be the information as returned by Promise.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 the authenticate hook to the batch service create method:

app.service('batch').hooks({
  before: {
    create: [ authenticate('jwt') ]
  }
});

Client

feathers-batch also exports a client side module that can be used with Feathers 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 field
import { batchClient } from 'feathers-batch';
// Alternatively
import { batchClient } from 'feathers-batch/client';

const client = feathers();
// configure Feathers client here

// `batchClient` should be configured *after*
// any other application level hooks
client.configure(batchClient({
  batchService: 'batch'
}));

Now you can continue to make normal service calls and whenever possible they will be automatically combined into a batch (see parallelizing requests for more information).

Options

The following options are available for the batchClient:

  • 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 will NOT be collected into a batch since the calls run sequentially using await:

const user = await client.service('users').get(userId);
const messages = await client.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 ] = await Promise.all([
  client.service('users').get(userId),
  client.service('messages').find({
    query: { userId }
  })
]);

License

Copyright (c) 2020 Feathers contributors

Licensed under the MIT license.