Giter Club home page Giter Club logo

rdb-dataloader's Introduction

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

rdb-dataloader's People

Contributors

luckydrq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.