Skip to content

Relationships Repositories

Yossi Kolesnicov edited this page Aug 21, 2018 · 1 revision

Relationship repositories are special repositories, which allow data retrieval of models, that are related to a specified other model.

The basic syntax for defining a ReadonlyRepository is as follows:

@EntityRelationship({
	sourceEntity: TodoList,
	dataEntity: Todo,
	foreignKey: 'todoListId'
})
export class TodoListItemsRelationship
	implements EntityRelationshipRepositoryType<TodoList, Todo> {}

A RelationshipRepository has two generic parameters:

  • TSource - the first param is the type of the source, for which data is retrieved.
  • TData - the second param is the type of data to return from queries to the RelationshipRepository.

In the example above, TodoList is the TSource and Todo is the TData. Meaning, after setting a TodoList as the source of the RelationshipRepository, it'll be possible to query TodoListItemsRelationship for the Todo items related to that TodoList:

const todoListItemsRepo: RelationshipRepository<TodoList, Todo> = paris.getRelationshipRepository(TodoListItemsRelationship);
todoListItemsRepo.sourceItem = myTodoList;

// The following will get todo items from:
// (the Todo endpoint)?todoListId=(myTodoList.id)
todoListItemsRepo.queryForItem().subscribe((listItems:DataSet<Todo>) => {
	// listItems contains the Todo items related to `myTodoList`.
})

Configuration

Property Type Description
sourceEntity T extends ModelBase The type of model to act as source. Should be the same type as the TSource param.
dataEntity T extends ModelBase The type of model that will be returned from queries. Should be the same type as the TData param.
[foreignKey] string A query param to add to URLs when querying the RelationshipRepository.
[getRelationshipData] (item?:TSource) => object A function that accepts the source item and returns an object representing query params to add to URLs when querying the RelationshipRepository.
[allowedTypes] Array<RelationshipType> A relationship doesn't always make sense for single item queries, or for multiples. allowedTypes may be used to specify which types of queries are possible for the RelationshipRepository.
Clone this wiki locally