Class Entity<TFields, TID, TViewerContext, TSelectedFields>Abstract

Entity is a privacy-first data model.

A instance of an entity represents a single "row" of persisted data in a database that a viewer, represented by the corresponding ViewerContext, has permission to read.

Create, read, update, and delete permissions for an entity are declaratively defined using an EntityPrivacyPolicy.

Entites are loaded through an EntityLoader, which is responsible for orchestrating fetching, caching, and authorization of reading "rows".

Entities are mutated and deleted through an EntityMutator, which is responsible for orchestrating database writes, cache invalidation, and authorization of writing "rows".

All concrete entity implementations should extend this class and provide their own EntityCompanionDefinition.

Type Parameters

  • TFields extends object

  • TID extends NonNullable<TFields[TSelectedFields]>

  • TViewerContext extends ViewerContext

  • TSelectedFields extends keyof TFields = keyof TFields

Hierarchy

Constructors

  • Internal

    Constructs an instance of an Entity.

    Type Parameters

    • TFields extends object

    • TID extends {}

    • TViewerContext extends ViewerContext<TViewerContext>

    • TSelectedFields extends string | number | symbol = keyof TFields

    Parameters

    • constructorParam: {
          databaseFields: Readonly<TFields>;
          id: TID;
          selectedFields: Readonly<Pick<TFields, TSelectedFields>>;
          viewerContext: TViewerContext;
      }

      data needed to construct an instance of an entity viewerContext - the ViewerContext reading this entity id - the ID of this entity databaseFields - all underlying fields for this entity's data selectedFields - selected fields for this entity from TSelectedFields type

      This should only be overridden in cases where additional data validation is needed. The params should not be modified when calling super during constructions.

      • databaseFields: Readonly<TFields>
      • id: TID
      • selectedFields: Readonly<Pick<TFields, TSelectedFields>>
      • viewerContext: TViewerContext

    Returns Entity<TFields, TID, TViewerContext, TSelectedFields>

Methods

  • Get a underlying field from this entity's data

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • fieldName: K

      the field to get

    Returns Pick<TFields, TSelectedFields>[K]

    the value of the field or undefined if not loaded with that field

  • Check whether an entity loaded by a viewer can be deleted by that same viewer.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • existingEntity: TMEntity

      entity loaded by viewer

    • queryContext: EntityQueryContext = ...

      query context in which to perform the check

    Returns Promise<boolean>

    Remarks

    See remarks for canViewerUpdate.

  • Check whether an entity loaded by a viewer can be updated by that same viewer.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • existingEntity: TMEntity

      entity loaded by viewer

    • queryContext: EntityQueryContext = ...

      query context in which to perform the check

    Returns Promise<boolean>

    Remarks

    This may be useful in situations relying upon the thrown privacy policy thrown authorization error is insufficient for the task at hand. When dealing with purely a sequence of mutations it is easy to roll back all mutations given a single authorization error by wrapping them in a single transaction. When certain portions of a mutation cannot be rolled back transactionally (third pary calls, legacy code, etc), using this method can help decide whether the sequence of mutations will fail before attempting them. Note that if any privacy policy rules use a piece of data being updated in the mutations the result of this method and the update mutation itself may differ.

  • Vend mutator for creating a new entity in given query context.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMViewerContext2 extends ViewerContext<TMViewerContext2>

    • TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • viewerContext: TMViewerContext2

      viewer context of creating user

    • queryContext: EntityQueryContext = ...

      query context in which to perform the create

    Returns CreateMutator<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>

    mutator for creating an entity

  • Delete an existing entity in given query context.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • existingEntity: TMEntity

      entity to delete

    • queryContext: EntityQueryContext = ...

      query context in which to perform the delete

    Returns Promise<Result<void>>

  • Delete an existing entity in given query context, throwing if deletion is unsuccessful.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • existingEntity: TMEntity

      entity to delete

    • queryContext: EntityQueryContext = ...

      query context in which to perform the delete

    Returns Promise<void>

  • Vend loader for loading an entity in a given query context.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMViewerContext2 extends ViewerContext<TMViewerContext2>

    • TMEntity extends ReadonlyEntity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • viewerContext: TMViewerContext2

      viewer context of loading user

    • queryContext: EntityQueryContext = ...

      query context in which to perform the load

    Returns EntityLoader<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>

  • Vend mutator for updating an existing entity in given query context.

    Type Parameters

    • TMFields extends object

    • TMID extends {}

    • TMViewerContext extends ViewerContext<TMViewerContext>

    • TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields, TMEntity>

    • TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields, TMPrivacyPolicy>

    • TMSelectedFields extends string | number | symbol = keyof TMFields

    Parameters

    • this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>
    • existingEntity: TMEntity

      entity to update

    • queryContext: EntityQueryContext = ...

      query context in which to perform the update

    Returns UpdateMutator<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields>

    mutator for updating existingEntity

Generated using TypeDoc