Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ReadonlyRepository<TEntity, TRawData>

A Repository is a service through which all of an Entity's data is fetched, cached and saved back to the backend.

ReadonlyRepository is the base class for all Repositories, and the class used for Repositories that are readonly.

Type parameters

Hierarchy

Implements

Index

Constructors

constructor

Properties

entityConstructor

entityConstructor: DataEntityType<TEntity, TRawData>

error$

error$: Observable<EntityErrorEvent>

Accessors

allItems$

  • get allItems$(): Observable<Array<TEntity>>
  • An Observable for all the items of this entity. If the Entity has already loaded all possible items (if loadAll is set to true, for example), those items are returned. Otherwise, a query with no DataQuery will be performed to the backend and the data will be fetched.

    Returns Observable<Array<TEntity>>

entity

modelConfig

values

  • get values(): Array<TEntity>

Methods

addCustomHeaders

  • addCustomHeaders(data: any): Record<string, string>

clearAllValues

  • clearAllValues(): void

clearCache

  • clearCache(): void

createItem

  • Creates a full model of this Repository's Entity. Any sub-models that need to be fetched from backend will be fetched (if options.availability === DataAvailability.deep). This method is used internally when modeling entities and value objects, but may be used externally as well, in case an item should be created programmatically from raw data.

    Parameters

    • rawData: TRawData

      The raw data for the entity, as it arrives from backend

    • Default value options: DataOptions = { allowCache: true, availability: DataAvailability.available }
    • Optional query: DataQuery

    Returns Observable<TEntity>

createNewItem

  • createNewItem(): TEntity
  • Creates a new instance of the Repository's entity. All fields will be undefined, except those that have defaultValue or those that are arrays, which will have an empty array as value.

    Returns TEntity

getEndpointName

getEndpointUrl

getItemById

getQueryHttpOptions

  • Returns the HttpOptions for the specified DataQuery. Uses the entity config's parseDataQuery configuration, if available, or otherwise the queryToHttpOptions function. If the entity config has fixedData set, the fixed data will be added to the HttpOptions params.

    Parameters

    Returns HttpOptions

query

  • Gets multiple items from backend. The backend may add paging information, such as count, page, etc, so a DataSet object is returned rather than just an Array.

    example

    Get all Todo items

    repository.query()
            .subscribe((todoItems:DataSet<TodoItem>) => console.log('Current items: ', todoItems.items));
    
    example

    Get all Todo items, sorted by name

    repository.query({ sortBy: { field: 'name' }})
            .subscribe((todoItems:DataSet<TodoItem>) => console.log('Items by name: ', todoItems.items));
    

    Parameters

    Returns Observable<DataSet<TEntity>>

queryItem

  • Same as {@link ReadonlyRepository#query|query}, but returns a single item rather than a {DataSet}. Useful for when we require to fetch a single model from backend, but it's either a ValueObject (so we can't refer to it by ID) or it's fetched by a more complex data query.

    Parameters

    Returns Observable<TEntity>

serializeItem

  • serializeItem(item: Partial<TEntity>, serializationData?: any): TRawData
  • Creates a JSON object that can be saved to server, with the reverse logic of getItemModelData

    Parameters

    • item: Partial<TEntity>
    • Optional serializationData: any

    Returns TRawData

Static validateItem

  • validateItem<TEntity, TRawData>(item: __type, entity: ModelConfig<TEntity, TRawData>): boolean
  • Validates that the specified item is valid, according to the requirements of the entity (or value object) it belongs to. Meaning, that it can be used as data for creating an instance of T.

    Type parameters

    Parameters

    Returns boolean

Generated using TypeDoc