Skip to main content

@baeta/extension-cache

Classes

CacheRef<Result, Root, Args>

Cache reference for a type field or query

Type Parameters

Type Parameter

Result

Root

Args

Constructors

new CacheRef()

new CacheRef<Result, Root, Args>(type, field, hash, revision): CacheRef<Result, Root, Args>

Parameters
ParameterTypeDefault value

type

string

undefined

field

string

undefined

hash

string

undefined

revision

number

1

Returns

CacheRef<Result, Root, Args>

Methods

getHash()

getHash(): string

Returns

string

getRevision()

getRevision(): number

Returns

number

setRevision()

setRevision(revision): void

Parameters
ParameterType

revision

number

Returns

void

toString()

toString(): string

Returns

string


abstract Store

Base class for cache storage implementations

Constructors

new Store()

new Store(): Store

Returns

Store

Methods

createStoreAdapter()

abstract createStoreAdapter<T>(options, type, hash): StoreAdapter<T>

Creates a new store adapter for a specific type

Type Parameters
Type Parameter

T

Parameters
ParameterTypeDescription

options

StoreOptions<T>

Store configuration options

type

string

Type name for the cached items

hash

string

Unique hash for the type

Returns

StoreAdapter<T>


abstract StoreAdapter<Item>

Type Parameters

Type Parameter

Item

Constructors

new StoreAdapter()

new StoreAdapter<Item>(options, type, hash): StoreAdapter<Item>

Parameters
ParameterType

options

StoreOptions<Item>

type

string

hash

string

Returns

StoreAdapter<Item>

Properties

PropertyModifierType

deleteMany

abstract

(refs: ItemRef[], evictQueries?: boolean) => Promise<void>

deleteQueriesByRef

abstract

<Result, Root, Args>(queryRef?: string, parentRef?: ParentRef, args?: Record<string, unknown>) => Promise<void>

getPartialMany

abstract

(refs: ItemRef[]) => Promise<null | (null | Item)[]>

hash

protected

string

loader

protected

DataLoader<ItemRef, null | Item, ItemRef>

loadQueryMetadata

abstract

(queryRef: string, parentRef?: ParentRef, args?: Record<string, unknown>) => Promise<null | string[]>

options

protected

StoreOptions<Item>

saveMany

abstract

(items: Item[]) => Promise<void>

saveQueryMetadata

abstract

(queryRef: string, meta: string[], parentRef?: ParentRef, args?: Record<string, unknown>) => Promise<void>

type

protected

string

Methods

createKey()

protected createKey(ref): string

Parameters
ParameterType

ref

ItemRef

Returns

string

createKeyByItem()

protected createKeyByItem(item): string

Parameters
ParameterType

item

Item

Returns

string

createKeyByQuery()

protected createKeyByQuery(queryRef, parentRef?, args?): string

Parameters
ParameterType

queryRef

string

parentRef?

ParentRef

args?

Record<string, unknown>

Returns

string

createMiddleware()

createMiddleware<Result, Root, Args>(queryRef, ...args): Middleware<Result, Root, unknown, Args>

Type Parameters
Type Parameter

Result

Root

Args

Parameters
ParameterType

queryRef

CacheRef<Result, Root, Args>

...args

Root extends RefCompatibleRoot ? [CacheMiddlewareOptions<Root<Root>>] : [RequiredCacheMiddlewareOptions<Root>]

Returns

Middleware<Result, Root, unknown, Args>

createQueryKeyGlobMatcher()

protected createQueryKeyGlobMatcher(queryRef, parentRef, args): string

Parameters
ParameterType

queryRef

string

parentRef

NonNullable<ParentRef>

args

Record<string, unknown>

Returns

string

createQueryKeyHeader()

protected createQueryKeyHeader(parentRef, args?): string

Parameters
ParameterType

parentRef

ParentRef

args?

Record<string, unknown>

Returns

string

createQueryKeyNamespace()

protected createQueryKeyNamespace(queryRef): string

Parameters
ParameterType

queryRef

string

Returns

string

createQueryKeyRegExpMatcher()

protected createQueryKeyRegExpMatcher(queryRef, parentRef, args): RegExp

Parameters
ParameterType

queryRef

string

parentRef

NonNullable<ParentRef>

args

Record<string, unknown>

Returns

RegExp

decodeQueryItemRef()

protected decodeQueryItemRef(encodedRef): null | string

Parameters
ParameterType

encodedRef

string

Returns

null | string

delete()

delete(ref, evictQueries?): Promise<void>

Parameters
ParameterType

ref

ItemRef

evictQueries?

boolean

Returns

Promise<void>

deleteQueries()

deleteQueries<Result, Root, Args>(queryRef?, matcher?): Promise<void>

Type Parameters
Type Parameter

Result

Root

Args

Parameters
ParameterType

queryRef?

CacheRef<Result, Root, Args>

matcher?

CacheQueryMatching<Args>

Returns

Promise<void>

encodePrimitive()

protected encodePrimitive(value, catchAll?): null | string

Parameters
ParameterType

value

unknown

catchAll?

string

Returns

null | string

encodeProperty()

protected encodeProperty(value): string

Parameters
ParameterType

value

string

Returns

string

encodeQueryArgs()

protected encodeQueryArgs(args, catchAll?): string

Parameters
ParameterType

args

Record<string, unknown>

catchAll?

string

Returns

string

encodeQueryItemRef()

protected encodeQueryItemRef(item): string

Parameters
ParameterType

item

null | Item

Returns

string

get()

get(ref): Promise<null | Item>

Parameters
ParameterType

ref

ItemRef

Returns

Promise<null | Item>

getMany()

getMany(refs): Promise<null | Item[]>

Parameters
ParameterType

refs

ItemRef[]

Returns

Promise<null | Item[]>

getQueryResult()

getQueryResult<Result, Root, Args>(queryRef, matcher?): Promise<null | { query: Result; }>

Type Parameters
Type Parameter

Result

Root

Args

Parameters
ParameterType

queryRef

CacheRef<Result, Root, Args>

matcher?

CacheQueryMatching<Args>

Returns

Promise<null | { query: Result; }>

getRef()

protected getRef(root): ItemRef

Parameters
ParameterType

root

Item

Returns

ItemRef

getRefFallback()

protected getRefFallback(root): undefined | string

Parameters
ParameterType

root

unknown

Returns

undefined | string

getRevision()

protected getRevision(): string

Returns

string

loaderFn()

protected loaderFn(refs): Promise<any[]>

Parameters
ParameterType

refs

readonly ItemRef[]

Returns

Promise<any[]>

refillNullQueryItems()

protected refillNullQueryItems(nullableRefs, items): (null | Item)[]

Parameters
ParameterType

nullableRefs

(null | ItemRef)[]

items

Item[]

Returns

(null | Item)[]

save()

save(item): Promise<void>

Parameters
ParameterType

item

Item

Returns

Promise<void>

saveQueryResult()

saveQueryResult<Result, Root, Args>(queryRef, data, matcher?): Promise<void>

Type Parameters
Type Parameter

Result

Root

Args

Parameters
ParameterType

queryRef

CacheRef<Result, Root, Args>

data

Result

matcher?

CacheQueryMatching<Args>

Returns

Promise<void>

shouldEncode()

protected shouldEncode(value): boolean

Parameters
ParameterType

value

string

Returns

boolean

validateRefType()

protected validateRefType(ref): asserts ref is string | number | bigint

Parameters
ParameterType

ref

unknown

Returns

asserts ref is string | number | bigint

Interfaces

CacheMiddlewareOptions<Root>

Options for cache middleware

Extended by

Type Parameters

Type Parameter

Root

Properties

PropertyTypeDescription

getRootRef?

(root: Root) => ItemRef

Function to extract object reference id


DefaultStoreOptions

Default options for cache stores

Extended by

Properties

PropertyTypeDefault valueDescription

ttl?

number

3600;

Time-to-live in seconds


RequiredCacheMiddlewareOptions<Root>

Required options for cache middleware

Extends

Type Parameters

Type Parameter

Root

Properties

PropertyTypeDescriptionOverrides

getRootRef

(root: Root) => ItemRef

Function to extract object reference id

CacheMiddlewareOptions.getRootRef


RequiredStoreOptions<Root>

Required configuration options for cache stores

Extends

Type Parameters

Type Parameter

Root

Properties

PropertyTypeDefault valueDescriptionOverridesInherited from

getRef

(root: Root) => ItemRef

undefined

Function to extract object reference id

StoreOptions.getRef

revision?

number

undefined

Manual cache version for invalidation

StoreOptions.revision

ttl?

number

3600;

Time-to-live in seconds

StoreOptions.ttl


StoreOptions<Root>

Configuration options for cache stores

Extends

Extended by

Type Parameters

Type Parameter

Root

Properties

PropertyTypeDefault valueDescriptionInherited from

getRef?

(root: Root) => ItemRef

undefined

Function to extract object reference id

revision?

number

undefined

Manual cache version for invalidation

ttl?

number

3600;

Time-to-live in seconds

DefaultStoreOptions.ttl

Type Aliases

CacheArgs<T>

CacheArgs<T>: { [P in keyof T]?: T[P] extends object ? CacheArgs<T[P]> : T[P] }

Optional query arguments

Type Parameters

Type Parameter

T


CacheQueryMatching<Args>

CacheQueryMatching<Args>: object

Type Parameters

Type Parameter

Args

Type declaration

NameType

args?

CacheArgs<Args>

parentRef?

ParentRef


ItemRef

ItemRef: string | number | bigint

Reference type for cached items


ParentRef

ParentRef: ItemRef | null | undefined

Reference type for query parent


RefCompatibleRoot

RefCompatibleRoot: { id: string | number | bigint; } | {}

Type constraint for objects that are compatible with default cache ref

Functions

cacheExtension()

cacheExtension(store, options?): () => Extension

Creates a cache extension

Parameters

ParameterTypeDescription

store

Store

Storage adapter implementation

options?

DefaultStoreOptions

Default caching options

Returns

Function

Extension factory function

Returns

Extension

Example

import { cacheExtension } from "@baeta/extension-cache";
import { RedisStore } from "@baeta/extension-cache-redis";
import Redis from "ioredis";

const redis = new Redis("redis://localhost:6379");
const redisStore = new RedisStore(redis);

export const cacheExt = cacheExtension(redisStore, {
ttl: 3600, // TTL in seconds (defaults to 1 hour)
});