@npmteam2024/nesciunt-tempore-occaecati

1.0.2 • Public • Published

@npmteam2024/nesciunt-tempore-occaecati

A simple cache system for a single user request, built on the same concepts of data loader.

Install

npm i @npmteam2024/nesciunt-tempore-occaecati

Usage

const { Factory } = require('.')
const factory = new Factory()

factory.add('fetchSomething', {
  // cache by default, set to false to just do batching
  cache: true
}, async (queries, context) => {
  console.log(queries)
  // [ 42, 24 ]

  console.log(context)
  // { some: 'data' }

  return queries.map((k) => {
    return { k }
  })
})

async function run () {
  const context = {
    some: 'data'
  }
  const cache = factory.create(context)

  const p1 = cache.fetchSomething(42)
  const p2 = cache.fetchSomething(24)

  const res = await Promise.all([p1, p2])

  console.log(res)
  // [
  //   { k: 42 },
  //   { k: 24 }
  // ]
}

run().catch(console.log)

If the query parameter is an object, its cache key will be generated using safe-stable-stringify.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @npmteam2024/nesciunt-tempore-occaecati

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

14.2 kB

Total Files

5

Last publish

Collaborators

  • sabordi2v