CacheDBProviderclass
new CacheDBProvider<I, T>(source: DBProvider<I, T>, cache: MemoryDBProvider<I, T> = new MemoryDBProvider<I, T>())
| Param | Type | |
|---|---|---|
source | DBProvider<I, T> | The provider to fetch from and write to. required |
cache | MemoryDBProvider<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. |
| Property | Type | |
|---|---|---|
.source | DBProvider<I, T> | The wrapped source provider that data is fetched from and written to. required readonly |
.memory | MemoryDBProvider<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
sourceprovider and mirrors every read and write into an in-memoryMemoryDBProvider, so subsequent reads can be served synchronously and live subscriptions stay seeded. - Reads fetch from
source, then refresh the cache; writes hitsource, 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
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 mirrorDBCache 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.