import type { Result } from '@expo/results'; import type { Entity, IEntityClass } from './Entity.ts'; import type { EntityCompanionProvider } from './EntityCompanionProvider.ts'; import type { EntityConfiguration } from './EntityConfiguration.ts'; import type { EntityDatabaseAdapter } from './EntityDatabaseAdapter.ts'; import type { EntityLoaderFactory } from './EntityLoaderFactory.ts'; import type { EntityCascadingDeletionInfo, EntityTriggerMutationInfo, EntityValidatorMutationInfo } from './EntityMutationInfo.ts'; import type { EntityMutationTrigger, EntityMutationTriggerConfiguration, EntityNonTransactionalMutationTrigger } from './EntityMutationTriggerConfiguration.ts'; import type { EntityMutationValidator, EntityMutationValidatorConfiguration } from './EntityMutationValidatorConfiguration.ts'; import type { EntityPrivacyPolicy } from './EntityPrivacyPolicy.ts'; import type { EntityQueryContext, EntityTransactionalQueryContext } from './EntityQueryContext.ts'; import type { ViewerContext } from './ViewerContext.ts'; import type { IEntityMetricsAdapter } from './metrics/IEntityMetricsAdapter.ts'; /** * Base class for entity mutators. Mutators are builder-like class instances that are * responsible for creating, updating, and deleting entities, and for calling out to * the loader at appropriate times to invalidate the cache(s). The loader is responsible * for deciding which cache entries to invalidate for the entity being mutated. * * ## Notes on invalidation * * The primary goal of invalidation is to ensure that at any point in time, a load * for an entity through a cache or layers of caches will return the most up-to-date * value for that entity according to the source of truth stored in the database, and thus * the read-through cache must be kept consistent (only current values are stored, others are invalidated). * * This is done by invalidating the cache for the entity being mutated at the end of the transaction * in which the mutation is performed. This ensures that the cache is invalidated as close as possible * to when the source-of-truth is updated in the database, as to reduce the likelihood of * collisions with loads done at the same time on other machines. * *
* The easiest way to demonstrate this reasoning is via some counter-examples. * For sake of demonstration, let's say we did invalidation immediately after the mutation instead of * at the end of the transaction. * * Example 1: * - t=0. A transaction is started on machine A and within this transaction a new entity is created. * The cache for the entity is invalidated. * - t=1. Machine B tries to load the same entity outside of a transaction. It does not yet exist * so it negatively caches the entity. * - t=2. Machine A commits the transaction. * - t=3. Machine C tries to load the same entity outside of a transaction. It is negatively cached * so it returns null, even though it exists in the database. * * One can see that it's strictly better to invalidate the transaction at t=2 as it would remove the * negative cache entry for the entity, thus leaving the cache consistent with the database. * * Example 2: * - t=0. Entity A is created and read into the cache (everthing is consistent at this point in time). * - t=1. Machine A starts a transaction, reads entity A, updates it, and invalidates the cache. * - t=2. Machine B reads entity A outside of a transaction. Since the transaction from the step above * has not yet been committed, the changes within that transaction are not yet visible. It stores * the entity in the cache. * - t=3. Machine A commits the transaction. * - t=4. Machine C reads entity A outside of a transaction. It returns the entity from the cache which * is now inconsistent with the database. * * Again, one can see that it's strictly better to invalidate the transaction at t=3 as it would remove the * stale cache entry for the entity, thus leaving the cache consistent with the database. * * For deletions, one can imagine a similar series of events occurring. *
* * #### Invalidation as it pertains to transactions and nested transactions * * Invalidation becomes slightly more complex when nested transactions are considered. The general * guiding principle here is that over-invalidation is strictly better than under-invalidation * as far as consistency goes. This is because the database is the source of truth. * * For the visible-to-the-outside-world caches (cache adapters), the invalidations are done at the * end of the outermost transaction (as discussed above), plus at the end of each nested transaction. * While only the outermost transaction is strictly necessary for these cache adapter invalidations, * the mental model of doing it at the end of each transaction, nested or otherwise, is easier to reason about. * * For the dataloader caches (per-transaction local caches), the invalidation is done multiple times * (over-invalidation) to better ensure that the caches are always consistent with the database as read within * the transaction or nested transaction. * 1. Immediately after the mutation is performed (but before the transaction or nested transaction is committed). * 2. At the end of the transaction (or nested transaction) itself. * 3. At the end of the outermost transaction (if this is a nested transaction) and all of that transactions's nested transactions recursively. * * This over-invalidation is done because transaction isolation semantics are not consistent across all * databases (some databases don't even have true nested transactions at all), meaning that whether * a change made in a nested transaction is visible to the parent transaction(s) is not necessarily known. * This means that the only way to ensure that the dataloader caches are consistent * with the database is to invalidate them often, thus delegating consistency to the database. Invalidation * of local caches is synchronous and immediate, so the performance impact of over-invalidation is negligible. * * #### Invalidation pitfalls * * One may have noticed that the above invalidation strategy still isn't perfect. Cache invalidation is hard. * There still exists a very short moment in time between when invalidation occurs and when the transaction is committed, * so dirty cache writes are still possible, especially in systems reading an object frequently and writing to the same object. * For now, the entity framework does not attempt to provide a further solution to this problem since it is likely * solutions will be case-specific. Some fun reads on the topic: * - https://engineering.fb.com/2013/06/25/core-infra/tao-the-power-of-the-graph/ * - https://hazelcast.com/blog/a-hitchhikers-guide-to-caching-patterns/ */ export declare abstract class AuthorizationResultBasedBaseMutator, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends Entity, TPrivacyPolicy extends EntityPrivacyPolicy, TSelectedFields extends keyof TFields> { protected readonly companionProvider: EntityCompanionProvider; protected readonly viewerContext: TViewerContext; protected readonly queryContext: EntityQueryContext; protected readonly entityConfiguration: EntityConfiguration; protected readonly entityClass: IEntityClass; protected readonly privacyPolicy: TPrivacyPolicy; protected readonly mutationValidators: EntityMutationValidatorConfiguration; protected readonly mutationTriggers: EntityMutationTriggerConfiguration; protected readonly entityLoaderFactory: EntityLoaderFactory; protected readonly databaseAdapter: EntityDatabaseAdapter; protected readonly metricsAdapter: IEntityMetricsAdapter; constructor(companionProvider: EntityCompanionProvider, viewerContext: TViewerContext, queryContext: EntityQueryContext, entityConfiguration: EntityConfiguration, entityClass: IEntityClass, privacyPolicy: TPrivacyPolicy, mutationValidators: EntityMutationValidatorConfiguration, mutationTriggers: EntityMutationTriggerConfiguration, entityLoaderFactory: EntityLoaderFactory, databaseAdapter: EntityDatabaseAdapter, metricsAdapter: IEntityMetricsAdapter); protected validateFields(fields: Partial): void; protected executeMutationValidatorsAsync(validators: EntityMutationValidator[] | undefined, queryContext: EntityTransactionalQueryContext, entity: TEntity, mutationInfo: EntityValidatorMutationInfo): Promise; protected executeMutationTriggersAsync(triggers: EntityMutationTrigger[] | undefined, queryContext: EntityTransactionalQueryContext, entity: TEntity, mutationInfo: EntityTriggerMutationInfo): Promise; protected executeNonTransactionalMutationTriggersAsync(triggers: EntityNonTransactionalMutationTrigger[] | undefined, entity: TEntity, mutationInfo: EntityTriggerMutationInfo): Promise; } /** * Mutator for creating a new entity. */ export declare class AuthorizationResultBasedCreateMutator, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends Entity, TPrivacyPolicy extends EntityPrivacyPolicy, TSelectedFields extends keyof TFields> extends AuthorizationResultBasedBaseMutator { private readonly fieldsForEntity; /** * Set the value for entity field. * @param fieldName - entity field being set * @param value - value for entity field */ setField>(fieldName: K, value: TFields[K]): this; /** * Commit the new entity after authorizing against creation privacy rules. Invalidates all caches for * queries that would return new entity. * @returns authorized, cached, newly-created entity result, where result error can be UnauthorizedError */ createAsync(): Promise>; private createInTransactionAsync; private createInternalAsync; } /** * Mutator for updating an existing entity. */ export declare class AuthorizationResultBasedUpdateMutator, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends Entity, TPrivacyPolicy extends EntityPrivacyPolicy, TSelectedFields extends keyof TFields> extends AuthorizationResultBasedBaseMutator { private readonly cascadingDeleteCause; private readonly originalEntity; private readonly fieldsForEntity; private readonly updatedFields; constructor(companionProvider: EntityCompanionProvider, viewerContext: TViewerContext, queryContext: EntityQueryContext, entityConfiguration: EntityConfiguration, entityClass: IEntityClass, privacyPolicy: TPrivacyPolicy, mutationValidators: EntityMutationValidatorConfiguration, mutationTriggers: EntityMutationTriggerConfiguration, entityLoaderFactory: EntityLoaderFactory, databaseAdapter: EntityDatabaseAdapter, metricsAdapter: IEntityMetricsAdapter, originalEntity: TEntity, cascadingDeleteCause: EntityCascadingDeletionInfo | null); /** * Set the value for entity field. * @param fieldName - entity field being updated * @param value - value for entity field */ setField>(fieldName: K, value: TFields[K]): this; /** * Commit the changes to the entity after authorizing against update privacy rules. * Invalidates all caches for pre-update entity. * @returns authorized updated entity result, where result error can be UnauthorizedError */ updateAsync(): Promise>; private updateInTransactionAsync; private updateInternalAsync; private ensureStableIDField; } /** * Mutator for deleting an existing entity. */ export declare class AuthorizationResultBasedDeleteMutator, TIDField extends keyof NonNullable>, TViewerContext extends ViewerContext, TEntity extends Entity, TPrivacyPolicy extends EntityPrivacyPolicy, TSelectedFields extends keyof TFields> extends AuthorizationResultBasedBaseMutator { private readonly entity; private readonly cascadingDeleteCause; constructor(companionProvider: EntityCompanionProvider, viewerContext: TViewerContext, queryContext: EntityQueryContext, entityConfiguration: EntityConfiguration, entityClass: IEntityClass, privacyPolicy: TPrivacyPolicy, mutationValidators: EntityMutationValidatorConfiguration, mutationTriggers: EntityMutationTriggerConfiguration, entityLoaderFactory: EntityLoaderFactory, databaseAdapter: EntityDatabaseAdapter, metricsAdapter: IEntityMetricsAdapter, entity: TEntity, cascadingDeleteCause: EntityCascadingDeletionInfo | null); /** * Delete the entity after authorizing against delete privacy rules. The entity is invalidated in all caches. * @returns void result, where result error can be UnauthorizedError */ deleteAsync(): Promise>; private deleteInTransactionAsync; private deleteInternalAsync; /** * Finds all entities referencing the specified entity and either deletes them, nullifies * their references to the specified entity, or invalidates the cache depending on the * OnDeleteBehavior of the field referencing the specified entity. * * @remarks * This works by doing reverse fan-out queries: * 1. Load all entity configurations of entity types that reference this type of entity * 2. For each entity configuration, find all fields that contain edges to this type of entity * 3. For each edge field, load all entities with an edge from target entity to this entity via that field * 4. Perform desired OnDeleteBehavior for entities * * @param entity - entity to find all references to */ private processEntityDeletionForInboundEdgesAsync; }