1.0.1 • Published 3 years ago

immutable-dataloader v1.0.1

Weekly downloads
9
License
MIT
Repository
github
Last release
3 years ago

immutable-dataloader

JavaScript Style Guide TypeScript

dataloader, but calls to load() return new copies of the data, every time

Note: This doesn't mean that data is reloaded from the data source, just that each call to load() or loadMany() returns a new copy of the data.

Install

npm i immutable-dataloader
OR
yarn add immutable-dataloader

How to use

This is meant to be a drop in replacement for dataloader, thus usage is identical to dataloader

import DataLoader from 'immutable-dataloader'

const dataloader = new DataLoader(...)

Why

It may be desirable to manipulate data returned from a dataloader ie.

const res = await dataloader.load(1)

res.foo = 'bar'

// or an array
const resArray = await dataloader.load(2) // ['foo']

const lastItem = resArray.pop() // mututes the array

Because the data returned from the dataloader was directly mutated, subsequent calls to load, will return that cached, but same mutated, data:

const resArray = await dataloader.load(2) // [] 'foo' was removed by the last pop() call

const lastItem = resArray.pop() // array is empty, so lastItem is now undefined

Depending on the order or the calls to load, this can cause some funky gotchas, and bugs that are difficult to trace.

With immutable-dataloader, each call to load or loadMany returns a new copy of the data, so this whole class of bugs is removed.

License

MIT