import type { AuthorizationResultBasedEntityLoader } from './AuthorizationResultBasedEntityLoader.ts'; import type { EntityCompositeField, EntityCompositeFieldValue } from './EntityConfiguration.ts'; import type { EntityPrivacyPolicy } from './EntityPrivacyPolicy.ts'; import type { ReadonlyEntity } from './ReadonlyEntity.ts'; import type { ViewerContext } from './ViewerContext.ts'; import { CompositeFieldValueHolder } from './internal/CompositeFieldHolder.ts'; import { CompositeFieldValueMap } from './internal/CompositeFieldValueMap.ts'; import { mapMap } from './utils/collections/maps.ts'; /** * Enforcing entity loader. All normal loads are batched, * cached, and authorized against the entity's EntityPrivacyPolicy. All loads * through this loader will throw if the load is not successful. */ export class EnforcingEntityLoader< 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 entityLoader: AuthorizationResultBasedEntityLoader< TFields, TIDField, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields >, ) {} /** * Load many entities where fieldName is one of fieldValues. * @param fieldName - entity field being queried * @param fieldValues - fieldName field values being queried * @returns map from fieldValue to entities that match the query for that fieldValue * @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities */ async loadManyByFieldEqualingManyAsync>( fieldName: N, fieldValues: readonly NonNullable[], ): Promise, readonly TEntity[]>> { const fieldValuesToResults = await this.entityLoader.loadManyByFieldEqualingManyAsync( fieldName, fieldValues, ); return mapMap(fieldValuesToResults, (results) => results.map((result) => result.enforceValue()), ); } /** * Load many entities where compositeField is one of compositeFieldValues. * @param compositeField - composite field being queried * @param compositeFieldValues - compositeField values being queried * @returns map from compositeFieldValue to entities that match the query for that compositeFieldValue * @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities */ async loadManyByCompositeFieldEqualingManyAsync< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValues: readonly EntityCompositeFieldValue, N>[], ): Promise, readonly TEntity[]>> { const compositeFieldValuesToResults = await this.entityLoader.loadManyByCompositeFieldEqualingManyAsync( compositeField, compositeFieldValues, ); return new CompositeFieldValueMap( Array.from(compositeFieldValuesToResults.entries()).map(([compositeFieldValue, results]) => { return [ new CompositeFieldValueHolder(compositeFieldValue), results.map((result) => result.enforceValue()), ]; }), ); } /** * Load many entities where fieldName equals fieldValue. * @param fieldName - entity field being queried * @param fieldValue - fieldName field value being queried * @returns array of entities that match the query for fieldValue * @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities */ async loadManyByFieldEqualingAsync>( fieldName: N, fieldValue: NonNullable, ): Promise { const entityResults = await this.entityLoader.loadManyByFieldEqualingAsync( fieldName, fieldValue, ); return entityResults.map((result) => result.enforceValue()); } /** * Load many entities where compositeField equals compositeFieldValue. * @param compositeField - composite field being queried * @param compositeFieldValue - compositeField value being queried * @returns array of entities that match the query for compositeFieldValue * @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities */ async loadManyByCompositeFieldEqualingAsync< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValue: EntityCompositeFieldValue, N>, ): Promise { const entityResults = await this.entityLoader.loadManyByCompositeFieldEqualingAsync( compositeField, compositeFieldValue, ); return entityResults.map((result) => result.enforceValue()); } /** * Load an entity where fieldName equals fieldValue, or null if no entity exists. * @param uniqueFieldName - entity field being queried * @param fieldValue - uniqueFieldName field value being queried * @returns entity where uniqueFieldName equals fieldValue, or null if no entity matches the condition. * @throws when multiple entities match the condition * @throws EntityNotAuthorizedError when viewer is not authorized to view the returned entity */ async loadByFieldEqualingAsync>( uniqueFieldName: N, fieldValue: NonNullable, ): Promise { const entityResult = await this.entityLoader.loadByFieldEqualingAsync( uniqueFieldName, fieldValue, ); return entityResult ? entityResult.enforceValue() : null; } /** * Load an entity where compositeField equals compositeFieldValue, or null if no entity exists. * @param compositeField - composite field being queried * @param compositeFieldValue - compositeField value being queried * @returns entity where compositeField equals compositeFieldValue, or null if no entity matches the condition. * @throws when multiple entities match the condition * @throws EntityNotAuthorizedError when viewer is not authorized to view the returned entity */ async loadByCompositeFieldEqualingAsync< N extends EntityCompositeField>, >( compositeField: N, compositeFieldValue: EntityCompositeFieldValue, N>, ): Promise { const entityResult = await this.entityLoader.loadByCompositeFieldEqualingAsync( compositeField, compositeFieldValue, ); return entityResult ? entityResult.enforceValue() : null; } /** * Loads an entity by a specified ID. * @param id - ID of the entity * @returns entity matching ID * @throws EntityNotAuthorizedError when viewer is not authorized to view the returned entity * @throws EntityNotFoundError when no entity exists for ID */ async loadByIDAsync(id: TFields[TIDField]): Promise { const entityResult = await this.entityLoader.loadByIDAsync(id); return entityResult.enforceValue(); } /** * Load an entity by a specified ID, or return null if non-existent. * @param id - ID of the entity * @returns entity for matching ID, or null if no entity exists for ID. * @throws EntityNotAuthorizedError when viewer is not authorized to view the returned entity * @throws when multiple entities are found matching the condition */ async loadByIDNullableAsync(id: TFields[TIDField]): Promise { const entityResult = await this.entityLoader.loadByIDNullableAsync(id); return entityResult ? entityResult.enforceValue() : null; } /** * Loads many entities for a list of IDs. * @param ids - IDs of the entities to load * @returns map from ID to corresponding entity result, where result error can be UnauthorizedError or EntityNotFoundError. * @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities * @throws EntityNotFoundError when no entity exists for one or more of the IDs */ async loadManyByIDsAsync( ids: readonly TFields[TIDField][], ): Promise> { const entityResults = await this.entityLoader.loadManyByIDsAsync(ids); return mapMap(entityResults, (result) => result.enforceValue()); } /** * Loads many entities for a list of IDs, returning null for any IDs that are non-existent. * @param ids - IDs of the entities to load * @returns map from ID to nullable corresponding entity * @throws EntityNotAuthorizedError when viewer is not authorized to view one or more of the returned entities */ async loadManyByIDsNullableAsync( ids: readonly TFields[TIDField][], ): Promise> { const entityResults = await this.entityLoader.loadManyByIDsNullableAsync(ids); return mapMap(entityResults, (result) => result?.enforceValue() ?? null); } }