Socket
Book a DemoInstallSign in
Socket

@dalisoft/batch-collect

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dalisoft/batch-collect

Collect calls and make them batching

latest
Source
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

batchCollect

Greenkeeper badge

Collect calls and make them batching

Mostly use this for solving 1+N (N+1) problem

Features

  • Fast
  • Automatic handling
  • No duplication
  • Almost zero-config
  • Flexible
  • On browsers works too
  • Types declaration for IDE/Editor

Import

// ES6
import batchCollect from "@dalisoft/batch-collect";

// or

// CommonJS
const batchCollect = require("@dalisoft/batch-collect");

// or

const batchCollect = window.batchCollect;

Example

Basic example

const batchQuery = batchCollect(async collects => {
  await timeout(500);

  return collects.map(requestToSomeWhere);
});

batchQuery(() => `getUser { id, name }`, user => doSomething(user));

await batchQuery(async () => {
  await timeout(100);
  return `getAsync { result }`;
});

batchQuery(`getProfile {id, avatar}`);

N+1 problem solve example

const batchSQL = batchCollect(async ids => {
  return orm.getByIds(ids);
});

batchSQL(() => id, row => doSomething(row));

// This code is executes from different place than above query
// and both code exetudes once, on DB requests will be called once
batchSQL(id, row => doSomething(row));

For more info see tests.

License

MIT

Keywords

call

FAQs

Package last updated on 11 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts