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

rdb-dataloader

v1.0.1

Published

[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][down

Downloads

13

Readme

rdb-dataloader

NPM version build status Test coverage David deps Known Vulnerabilities npm download

This module targets at relational database such as MySQL, SQL Server. Heavily inspired by Facebook DataLoader.

Install

$ npm i rdb-dataloader

Why?

First i'd like to say DataLoader is a great module that, like it's said, aims to provide per-request cache to save network IO and extremly useful for data service implemented by GraphQL which intergreted with my project recently. I use MySQL as data storage layer which is a common case in real world i believe. However, I found something inconvenient when playing with MySQL:

Unnecessary duplicate cache

It is common to have one or more unique key(s) in a table. For example, an User table with these fields: id, name, mobile can have id as the Primary Key and mobile as a Unique Key. So the data service has to response to fetchByIds and fetchByMobiles calls. Then it have to initiate two instances when using DataLoader:

  const DataLoader = require('dataloader');
  const idLoader = new DataLoader(fetchByIds);
  const mobileLoader = new DataLoader(fetchByMobiles);

  idLoader.load(1);
  mobileLoader.load('+86123456');

  function fetchByIds(ids) {
    return db.Users.getByIds(ids);
  }

  function fetchByMobiles(mobiles) {
    return db.Users.getByMobiles(mobiles);
  }

This can work but it's not good enough and the logic is far more complex in real world. Two instances meaning two separate cache, you can imagine that there would be many duplecate records cached in both instance cache just because they are fetched by a different key(id or mobile) and Network Still happens for both sides. Also there would be more instances to initiate as the number of unique keys increases.

Duplicate keys

There is an old issue and DataLoader fixes now.

What does this module address?

This module aims to solve the inconvenience metioned above when playing with relational database. It is recommended that one loader per table. It'd use a single cache and whenever a record is fetched by PK or UK, it'd contribute to the cache. Accordingly, it'd support load by PK or UK to take full advantage of cache.

Example

  const uniqueKeyMap = new Map();
  uniqueKeyMap.set('name', db.fetchByNames); // UK
  uniqueKeyMap.set('email', db.fetchByEmails);  // UK
  const loader = new DataLoader(db.fetchByIds, { uniqueKeyMap });
  loader.load('luckydrq', 'name') // the second argument `name` is to tell loader to fetchByNames
    .then(record => {
      assert(record.id === 1);
      assert(loader._promiseCache.size === 1);
      done();
    }).catch(done);

More examples.

API

Since this module inherits from DataLoader, it is compatible with the apis of DataLoader. But as you can see from former example, the api is extended. Extended apis are the following:

contructor(options)

The constructor accepts all the options that DataLoader accepts. options.cache = true and options.batch = true is set by default and there is no way to override it because it'd be useless without these two features disabled. Alse, there are additional options as below:

  • options.primaryKey(optional): String Specify PK name, default is "id";

  • options.uniqueKeyMap(optional): ES6 Map

    • key: String UK name
    • value: Function Batch function for UK Use it when you want to fetch by UK.

load(key[, uniqueKey])

If you want to load by UK, the second argument should be specified, omitted meaning load by PK.

loadMany(keys[, uniqueKey])

If you want to load by UK, the second argument should be specified, omitted meaning load by PK.

Lisence

MIT