Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/fastify-single-user-cache
https://github.com/mcollina/fastify-single-user-cache
Last synced: 12 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/mcollina/fastify-single-user-cache
- Owner: mcollina
- License: mit
- Created: 2019-03-08T17:46:09.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-19T10:11:02.000Z (over 5 years ago)
- Last Synced: 2024-12-20T02:37:56.297Z (20 days ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-single-user-cache
[![Build
Status](https://travis-ci.com/mcollina/fastify-single-user-cache.svg?branch=master)](https://travis-ci.com/mcollina/fastify-single-user-cache)Provide a cache for each request, so multiple fetches of the same data
are not performed. Moreover, it provides an API to batch requests, so
that a bulk query can be done to a database or an external endpoint.The core caching functionality is provided by
[single-user-cache](http://npm.im/single-user-cache).This is similar to Facebook Data Loader pattern.
## Install
```
npm i fastify-single-user-cache
```## Usage
```js
const fastify = require('fastify')const app = fastify()
app.register(require('fastify-single-user-cache'), {
// key is the property that is going to be decorated in the
// app, Request and Reply
key: 'data'
})// We wrap our data definitions and routes in a plugin, so
// the cache plugin can actually load asynchronously.
app.register(async function (app) {// The result of this functions will be caches for the lifetime
// of the request.
app.data.add('fetchSomething', async (queries, { req, reply }) => {
// queries include an array of all the requested ids
reply.log.info({ queries }, 'executing fetchSomething')// It must return an array of length queries.length.
return queries.map((k) => {
return { k }
})
})app.get('/', async (req, reply) => {
const data = await Promise.all([
reply.data.fetchSomething(42),
reply.data.fetchSomething(24)
])return data
})
})app.listen(3000)
```## License
MIT