import type { Result } from '@expo/results'; import type { IEntityClass } from './Entity.ts'; import type { EntityConstructionUtils } from './EntityConstructionUtils.ts'; import type { EntityPrivacyPolicy } from './EntityPrivacyPolicy.ts'; import type { EntityQueryContext } from './EntityQueryContext.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; import { mapMap } from './utils/collections/maps.ts'; /** * An interface that knows how to load many objects from a cache by load params and invalidate * those same load params. */ export interface ISecondaryEntityCache, TLoadParams> { /** * Read-through cache function. * @param loadParamsArray - array of loadParams to load from the cache * @param fetcher - closure used to provide underlying data source objects for loadParams * @returns map from loadParams to the entity field object */ loadManyThroughAsync( loadParamsArray: readonly Readonly[], fetcher: ( fetcherLoadParamsArray: readonly Readonly[], ) => Promise, Readonly | null>>, ): Promise, Readonly | null>>; /** * Invalidate the cache for objects cached by loadParams. * * @param loadParamsArray - array of load params objects to invalidate */ invalidateManyAsync(loadParamsArray: readonly Readonly[]): Promise; } /** * A secondary cache loader allows for arbitrary cache keying for load params, which are a set of params used to load * a single entity field object. * * Note that this cache cannot be automatically invalidated like other entity caches so it must be manually invalidated * when the underlying data of a cache key could be stale. * * This is most commonly used to further optimize hot paths that cannot make use of normal entity cache loading * due to use of a non-unique-field-based EntityLoader method like `loadManyByFieldEqualityConjunctionAsync`. */ export abstract class EntitySecondaryCacheLoader< TLoadParams, TFields extends Record, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TPrivacyPolicy extends EntityPrivacyPolicy< TFields, TIDField, TViewerContext, TEntity, TSelectedFields >, TSelectedFields extends keyof TFields = keyof TFields, > { constructor( private readonly secondaryEntityCache: ISecondaryEntityCache, private readonly constructionUtils: EntityConstructionUtils< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, ) {} /** * Load many by load params objects * * @param loadParamsArray - array of loadParams to load through the cache */ public async loadManyAsync( loadParamsArray: readonly Readonly[], ): Promise, Result | null>> { const loadParamsToFieldObjects = await this.secondaryEntityCache.loadManyThroughAsync( loadParamsArray, this.fetchObjectsFromDatabaseAsync.bind(this), ); // convert value to and from array to reuse complex code const entitiesMap = await this.constructionUtils.constructAndAuthorizeEntitiesAsync( mapMap(loadParamsToFieldObjects, (fieldObject) => (fieldObject ? [fieldObject] : [])), ); return mapMap(entitiesMap, (fieldObjects) => fieldObjects[0] ?? null); } /** * Invalidate the cache for objects cached by loadParams. * * @param loadParamsArray - array of load params objects to invalidate */ public async invalidateManyAsync( loadParamsArray: readonly Readonly[], ): Promise { await this.secondaryEntityCache.invalidateManyAsync(loadParamsArray); } /** * Load through method called to fetch objects when load params objects are not in the cache * * @param loadParamsArray - array of load params objects to load */ protected abstract fetchObjectsFromDatabaseAsync( loadParamsArray: readonly Readonly[], ): Promise>, Readonly | null>>; /** * Helper to get construction utils for instantiating a EntitySecondaryCacheLoader. * * @param entityClass - the entity class for which to get construction utils for * @param viewerContext - the viewer context to use for construction utils * @param queryContext - query context to use for construction utils * @returns construction utils for the given entity class and viewer context */ public static getConstructionUtilsForEntityClass< TFields extends Record, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TPrivacyPolicy extends EntityPrivacyPolicy< TFields, TIDField, TViewerContext, TEntity, TSelectedFields >, TSelectedFields extends keyof TFields = keyof TFields, >( entityClass: IEntityClass< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, viewerContext: TViewerContext, queryContext: EntityQueryContext = viewerContext .getViewerScopedEntityCompanionForClass(entityClass) .getQueryContextProvider() .getQueryContext(), ): EntityConstructionUtils< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields > { return viewerContext .getViewerScopedEntityCompanionForClass(entityClass) .getLoaderFactory() .constructionUtils(queryContext, { previousValue: null, cascadingDeleteCause: null }); } }