@rapiq/cache
Experimental
@rapiq/cache is versioned separately on a 0.x line: its API may change in any 0.x minor release, independently of the 2.x packages. Pin an exact version or a ~ range if you depend on it.
A tag-invalidated result cache for rapiq queries. A cached result depends on every row it holds and on the membership of the sets it selected from, so a stored value carries tags (a collection, a scope, a record), every tag has a version on one logical clock, and a write bumps the tags it owns. A read whose entry carries a bumped tag is a miss.
npm install @rapiq/core @rapiq/cacheThe full walkthrough is the Caching Query Results guide.
What's inside
| Area | Exports | Guide |
|---|---|---|
| Cache | TaggedCache (remember, invalidate, drop), ITaggedCache | Reading through the cache |
| Tags | buildCollectionTag, buildRecordTag, buildScopedTag, isScopedTagValue, normalizeScopedUuid, ScopedTagValueNormalizer | Vocabulary, canonical scoped values |
| Query tags | collectQueryTags, isCacheable, rememberQuery, QueryTagsInput, RememberQueryInput, QueryCaseSensitive | Which tags a query yields |
| Drivers | ICacheDriver, CacheEntry, MemoryCacheDriver, CACHE_MAX_TTL_DEFAULT, isCacheTtlValid, clampCacheTtl, isCacheTagWellFormed | The driver contract |
| Errors | CacheError, isCacheError | Errors |
The cache key is the caller's, and it must encode every input that selects the result: the executed query, fields, and every path parameter (the :id of GET /roles/:id). See the cache key. A string filter value scopes only when caseSensitive carries exactly what you pass to adapter.execute(query, { caseSensitive }).
The TypeORM write side
CacheInvalidationSubscriber bumps the tags of every row TypeORM writes, after the transaction that wrote it committed. It is its own entry, so the root types never import from typeorm:
import { CacheInvalidationSubscriber } from '@rapiq/cache/typeorm';typeorm is an optional peer dependency for the types of that entry alone; the class imports nothing from typeorm at runtime. Push the instance onto dataSource.subscribers after every initialize(). See The TypeORM write side and what reaches a scoped reader.
The same entry exports isTransactionActive(dataSource, manager?), the check a read must pass before it goes through the cache: a read inside an open transaction calls the database directly. See what is not cached.
Key-value tables
A write to a key-value table is an update of the parent row the table serves. Name each table to the subscriber:
new CacheInvalidationSubscriber({
cache,
registry,
resolveSchemaName,
keyValueTables: [{ target: ProviderAttribute, foreignKey: 'providerId' }],
});foreignKey is the column (property or database name) on target that names the parent: the join column of a @ManyToOne, whose related entity is the parent, or a plain column such as @Column() providerId, which @rapiq/adapter-typeorm serves as well. A plain column has no relation to read the parent from, so name it with parent (and parentKey when the column holds something other than the parent's single primary column, as with the adapter's parentKey):
keyValueTables: [{ target: ProviderAttribute, foreignKey: 'providerId', parent: Provider }],An attribute write bumps the parent's collection and record tags, and, when the parent's schema declares indexes, its scoped tags (the parent row is loaded through the write's query runner to read them; a non-primary parentKey loads it too, to learn its id). A table whose parent resolves neither way is a configuration fault: it is reported to onError once, at the first write of that table, or, without onError, thrown from that write's hook (a Repository.save then rolls back), so cached parent reads never go stale silently.
A criteria write names no row: a delete bumps the parent collection only, and an update bumps the new parent only when it sets the foreign key, never the old one.
A key-value relation has no schema of its own, so tag derivation skips it; a write to its side table bumps the parent through the subscriber above. A query whose key-value relation carries a key gate (bound by the parser when the schema declares a fields hook) is not cacheable when it reads the whole map, since that read is gated per actor (see What is not cached); without a gate, or with a fieldset naming its keys, it caches like any other query.
Shared stores
The memory driver serves one process. For a store shared between processes use @rapiq/cache-redis, which implements the same ICacheDriver contract and runs the same contract suite.