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

fielddataloader

v0.0.7

Published

A wrapper around Facebook's DataLoader which supports batching field queries.

Downloads

5

Readme

FieldDataLoader build status Coverage Status

DataLoader is a utility for batching and caching requests to backend services. FieldDataLoader is a lightweight wrapper that adds support for requesting specific fields per item. For example:

import { FieldDataLoader } from 'fielddataloader';

var userLoader = new FieldDataLoader((key, fields) => {
  return db.select(/* ... */);
});

// Then, from different places in your app:
userLoader.load(1, ['first_name']);
userLoader.load(1, ['last_name']);
userLoader.load(2, ['first_name']);

// These will be batched into a single callback per ID, for example:
//   SELECT first_name, last_name FROM users WHERE id = 1;
//   SELECT first_name FROM users WHERE id = 2;

This is especially handy for GraphQL resolvers! Use it with the included getSelection helper to load only the specific fields that were requested in the given GraphQL query from your database or backend. For example:

import { FieldDataLoader, getSelection } from 'fielddataloader';

const resolvers = {
  Query: {
    user(obj, args, context, info) => userLoader.load(args.id, getSelection(info)),
  }
}

Getting Started

First, install FieldDataLoader using npm.

npm install --save fielddataloader

To get started, create a FieldDataLoader. Each DataLoader instance represents a unique cache.

Usage

FieldDataLoader replicates most of DataLoader's API:

const dataLoader = new FieldDataLoader(batchLoadFn)

Create a new FieldDataLoader, given a batch loading function.

  • batchLoadFn: A function which accepts a key and an array of fields, and returns a Promise which resolves to an Array of values.
dataLoader.load(key, fields)

Loads fields for the given key, returning a Promise for the result.

  • key: A key value to load.
  • fields: A list of fields to load for that key.
dataLoader.clear(key)

Clears the value at key from the cache, if it exists. Returns itself for method chaining.

  • key: A key value to clear.
dataLoader.clearAll()

Clears the entire cache. To be used when some event results in unknown invalidations across this particular FieldDataLoader. Returns itself for method chaining.

getFields(info, [returnType], [path])

Given the info argument from a GraphQL resolver, returns an array containing the names of any requested fields. Supports named & inline fragments, and fields referenced by Apollo Federation's @requires directive. You can optionally pass a returnType and path if reading fields nested within a query (rather than at the root).

License

© DoSomething.org. FieldDataLoader is free software, and may be redistributed under the terms specified in the LICENSE file. The name and logo for DoSomething.org are trademarks of Do Something, Inc and may not be used without permission.