import type { EntityConfiguration } from './EntityConfiguration.ts'; import type { EntityTransactionalQueryContext } from './EntityQueryContext.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; import type { EntityDataManager } from './internal/EntityDataManager.ts'; import type { LoadPair } from './internal/EntityLoadInterfaces.ts'; import { SingleFieldHolder, SingleFieldValueHolder } from './internal/SingleFieldHolder.ts'; import type { IEntityMetricsAdapter } from './metrics/IEntityMetricsAdapter.ts'; /** * Entity invalidation utilities. * Methods are exposed publicly since in rare cases they may need to be called manually. */ export class EntityInvalidationUtils< TFields extends Record, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends ReadonlyEntity, TSelectedFields extends keyof TFields, > { constructor( private readonly entityConfiguration: EntityConfiguration, private readonly dataManager: EntityDataManager, protected readonly metricsAdapter: IEntityMetricsAdapter, ) {} private getKeyValuePairsFromObjectFields( objectFields: Readonly, ): readonly LoadPair[] { const keys = Object.keys(objectFields) as (keyof TFields)[]; const singleFieldKeyValues: LoadPair[] = []; for (const fieldName of keys) { const value = objectFields[fieldName]; if (value !== undefined && value !== null) { singleFieldKeyValues.push([ new SingleFieldHolder(fieldName), new SingleFieldValueHolder(value), ]); } } const compositeFieldKeyValues: LoadPair[] = []; for (const compositeFieldHolder of this.entityConfiguration.compositeFieldInfo.getAllCompositeFieldHolders()) { const compositeFieldValueHolder = compositeFieldHolder.extractCompositeFieldValueHolderFromObjectFields(objectFields); if (compositeFieldValueHolder) { compositeFieldKeyValues.push([compositeFieldHolder, compositeFieldValueHolder]); } } return [...singleFieldKeyValues, ...compositeFieldKeyValues]; } /** * Invalidate all caches and local dataloaders for an entity's fields. Exposed primarily for internal use by EntityMutator. * @param objectFields - entity data object to be invalidated */ public async invalidateFieldsAsync(objectFields: Readonly): Promise { await this.dataManager.invalidateKeyValuePairsAsync( this.getKeyValuePairsFromObjectFields(objectFields), ); } /** * Invalidate all local dataloaders specific to a transaction for an entity's fields. Exposed primarily for internal use by EntityMutator. * @param objectFields - entity data object to be invalidated */ public invalidateFieldsForTransaction( queryContext: EntityTransactionalQueryContext, objectFields: Readonly, ): void { this.dataManager.invalidateKeyValuePairsForTransaction( queryContext, this.getKeyValuePairsFromObjectFields(objectFields), ); } /** * Invalidate all caches and local dataloaders for an entity. One potential use case would be to keep the entity * framework in sync with changes made to data outside of the framework. * @param entity - entity to be invalidated */ public async invalidateEntityAsync(entity: TEntity): Promise { await this.invalidateFieldsAsync(entity.getAllDatabaseFields()); } /** * Invalidate all local dataloaders specific to a transaction for an entity. One potential use case would be to keep the entity * framework in sync with changes made to data outside of the framework. * @param entity - entity to be invalidated */ public invalidateEntityForTransaction( queryContext: EntityTransactionalQueryContext, entity: TEntity, ): void { this.invalidateFieldsForTransaction(queryContext, entity.getAllDatabaseFields()); } }