CacheDBProviderclass

new CacheDBProvider<I, T>(source: DBProvider<I, T>, cache: MemoryDBProvider<I, T> = new MemoryDBProvider<I, T>())
ParamType
sourceDBProvider<I, T>
The provider to fetch from and write to. required
cacheMemoryDBProvider<I, T>
In-memory provider to use as the cache (a fresh MemoryDBProvider by default). Defaults to new MemoryDBProvider<I, T>()
Return
CacheDBProvider<I, T>
Database provider that keeps a copy of asynchronous remote data in a local synchronous cache.
PropertyType
.sourceDBProvider<I, T>
The wrapped source provider that data is fetched from and written to. required readonly
.memoryMemoryDBProvider<I, T>
The in-memory provider holding the local synchronous cache of source data. required readonly

Database provider that keeps a copy of asynchronous remote data in a local synchronous cache.

  • Wraps a source provider and mirrors every read and write into an in-memory MemoryDBProvider, so subsequent reads can be served synchronously and live subscriptions stay seeded.
  • Reads fetch from source, then refresh the cache; writes hit source, then mirror the change into the cache.
  • Discover the cache from a wrapping layer with getSource(CacheDBProvider, provider) to seed stores from .memory.

A wrapping provider that keeps an in-memory mirror in sync with a remote source. CacheDBProvider holds a MemoryDBProvider and populates it as data is read, so subsequent reads are synchronous — the basis of synchronous first renders in the React integration.

The constructor takes the source provider and an optional MemoryDBProvider to use as the mirror (one is created by default).

Usage

ts
import { CacheDBProvider, ValidationDBProvider, MemoryDBProvider } from "shelving/db";

const provider = new CacheDBProvider(
  new ValidationDBProvider(new MemoryDBProvider())
);

await provider.getItem(POSTS, "abc"); // fetches from source, populates the mirror
await provider.getItem(POSTS, "abc"); // served synchronously from the mirror

DBCache finds the CacheDBProvider in a chain automatically and reuses its mirror to seed reactive stores.

Examples

const provider = new CacheDBProvider(new FirestoreProvider());
 await provider.getItem(users, 123); // Fetches from source, then caches in memory.