@pokutuna/envelop-response-cache-firestore
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@pokutuna/envelop-response-cache-firestore

npm version License: MIT

Firestore cache implementation for @envelop/response-cache plugin.

Provides response caching that works well with serverless environments on Google Cloud.

Check out the GraphQL Response Cache Guide and Envelop for more information

Getting Started

yarn add @envelop/response-cache
yarn add @pokutuna/envelop-response-cache-firestore
import {envelop} from '@envelop/core';
import {useResponseCache} from '@envelop/response-cache';
import {createFirestoreCache} from '@pokutuna/envelop-response-cache-firestore';
import {Firestore} from '@google-cloud/firestore';

const firestore = new Firestore({projectId: 'YOUR_PROJECT_ID'});
const cache = createFirestoreCache({firestore});

const getEnveloped = envelop({
  plugins: [
    // ... other plugins ...
    useResponseCache({cache}),
  ],
});

Or, for use with GraphQL Yoga, see this doc.

Options

const cache = createFirestoreCache({
  // Firestore instance to store cache entries (required)
  firestore: new Firestore(),

  // Firestore collection path to store cache entries (default: "responseCache")
  // You can use subcollection (e.g. "_internals_/cache/entries")
  collectionPath: 'responseCache',

  // Customize entity id string conversion for invalidation (usually not required to use)
  buildEntityId: (typename: string, id: number | string) => `${typename}#${id}`,
})

Invalidate Cache

await cache.invalidate([
  // invalidate specific entities
  {typename: 'User', id: '1'},
  {typename: 'User', id: '2'},

  // invalidate all Comment entity
  {typename: 'Comment'},
]);

Delete expired cache entry

Expired cache entries are not automatically deleted. Recommend to run the following periodically.

await cache.deleteExpiredCacheEntry();

Or use TTL policies in Firestore (preview).

  • Collection group: responseCache (default)
  • Timestamp field: expireAt

See Manage data retention with TTL policies.

Notice

  • If you need performance, I recommend to use the Redis version officially provided.
    • The package is aimed at ease of setup with serverless environments and low cost.
  • The envelop implementation does not wait for the write to complete in order to return a response faster.
  • When a highly referenced cache expires, the same document will be updated in a short period and which may affect performance.

Readme

Keywords

none

Package Sidebar

Install

npm i @pokutuna/envelop-response-cache-firestore

Weekly Downloads

4

Version

0.2.0

License

MIT

Unpacked Size

16.7 kB

Total Files

8

Last publish

Collaborators

  • pokutuna