Skip to content

Reduce requests to backend services by batching calls and caching records.

License

Notifications You must be signed in to change notification settings

feathersjs-ecosystem/batch-loader

Repository files navigation

feathers-ecosystem/batch-loader

Reduce requests to backend services by batching calls and caching records.

Installation

npm install @feathers-plus/batch-loader --save

Documentation

Please refer to the batch-loader documentation for more details.

Basic Example

Use the loaderFactory static method to create a basic batch-loader. This is simply syntatic sugar for manually creating a batch-loader. This "Basic Example" and "Complete Example" create the same batch-loader.

const BatchLoader = require("@feathers-plus/batch-loader");

const usersBatchLoader = BatchLoader.loaderFactory(
  app.service("users"),
  "id",
  false
);

app
  .service("comments")
  .find()
  .then((comments) =>
    Promise.all(
      comments.map((comment) => {
        // Attach user record
        return usersBatchLoader
          .load(comment.userId)
          .then((user) => (comment.userRecord = user));
      })
    )
  );

Complete Example

Use the BatchLoader class to create more complex loaders. These loaders can call other services, call DB's directly, or even call third party services. This example manually implements the same loader created with the loaderFactory above.

const BatchLoader = require("@feathers-plus/batch-loader");
const { getResultsByKey, getUniqueKeys } = BatchLoader;

const usersBatchLoader = new BatchLoader((keys) =>
  app
    .service("users")
    .find({ query: { id: { $in: getUniqueKeys(keys) } } })
    .then((result) => getResultsByKey(keys, result, (user) => user.id, "!"))
);

app
  .service("comments")
  .find()
  .then((comments) =>
    Promise.all(
      comments.map((comment) => {
        // Attach user record
        return usersBatchLoader
          .load(comment.userId)
          .then((user) => (comment.userRecord = user));
      })
    )
  );

License

Copyright (c) 2017 John J. Szwaronek

Licensed under the MIT license.

About

Reduce requests to backend services by batching calls and caching records.

Resources

License

Stars

Watchers

Forks

Packages

No packages published