Skip to content

Commit 1be667f

Browse files
committed
wip repository docs
1 parent d2e92f1 commit 1be667f

File tree

8 files changed

+141
-50
lines changed

8 files changed

+141
-50
lines changed

17/umbraco-cms/.gitbook/assets/data-flow.svg

Lines changed: 0 additions & 19 deletions
This file was deleted.

17/umbraco-cms/customizing/foundation/fetching-data/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ After fetching data, the next step is to execute the request. You can use the `t
5555
### [Custom Generated Client](custom-generated-client.md)
5656

5757
For advanced scenarios, you can generate a custom client for your API using tools like [@hey-api/openapi-ts](https://github.com/hey-api/openapi-ts). This approach is ideal when working with custom API controllers or when you need type-safe, reusable client code.
58+
59+
### [Repositories](../repositories/README.md)
60+
Repositories provide a structured way to manage data operations in the Backoffice. They abstract the data access layer, allowing for easier maintenance and scalability.

17/umbraco-cms/customizing/foundation/repositories.md

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Repositories
2+
Repositories provide a structured way to manage data operations in the Backoffice. They abstract the data access layer, allowing for easier reuse and scalability.
3+
4+
Repositories create separation between domain logic and data access. By providing a known interface for data requests, we can reuse UI components across different domains. For example, we have a generic UX flow for deleting an entity. By supplying this flow with a repository that has a known interface for deletion, we can use the same UX flow to delete any entity.
5+
6+
Additionally repositories can utilize different data sources depending on the application's state. These sources may include:
7+
8+
* A REST API
9+
* Offline storage
10+
* A local cache
11+
* Etc.
12+
13+
This abstraction ensures that consumers don’t need to worry about how to access data. The repository serves as the Backoffice’s entry point for requesting new data. As a result, we achieve a loosely coupled connection between consumers and data storage procedures, effectively hiding complex implementations.
14+
15+
**Repository:** defines what data operations are available (get, add, update, delete).
16+
**Data Source:** defines how data is actually fetched or stored.
17+
18+
### Data flow with a repository <a href="#data-flow-with-a-repository" id="data-flow-with-a-repository"></a>
19+
20+
A repository must be instantiated where it is used. It should take an [UmbController](../../umbraco-controller/README.md) as part of the constructor. This ensures that any contexts consumed in the repository, like notifications or modals, are rendered in the correct context.
21+
22+
A repository can be initialized directly from an element, but will often be instantiated in a [context](../../context-api/README.md), like the Workspace Context.
23+
24+
```text
25+
Element → (Context) → Repository → Data Source(s)
26+
```
27+
28+
### Using an existing Repository <a href="#using-a-repository" id="using-a-repository"></a>
29+
30+
Often, you will find that data is already available and observable in a [context](./contexts/README.md), such as the Workspace Context. In that case, subscribing to the context [state](./states.md) will be the right approach to take. This way, you will receive all runtime updates that occur to the data throughout the session.
31+
32+
When a context with the appropriate data state is not available, reaching for a repository will ensure access to the needed information no matter the current application state.
33+
34+
```typescript
35+
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
36+
import { LitElement} from '@umbraco-cms/backoffice/external/lit';
37+
import {UmbDocumentDetailRepository} from '@umbraco-cms/backoffice/document';
38+
39+
class MyElement extends UmbElementMixin(LitElement) {
40+
...
41+
#documentRepository = new UmbDocumentDetailRepository(this);
42+
43+
firstUpdated() {
44+
const { data, error } = await this.#documentRepository.requestByUnique('some-unique-key');
45+
console.log('The Document Data:', data);
46+
}
47+
...
48+
}
49+
50+
const documentRepository = new UmbDocumentDetailRepository(this);
51+
52+
```
53+
54+
### Register a custom Repository <a href="#register-a-custom-repository" id="register-a-custom-repository"></a>
55+
56+
By registering your repository in the [Extension Registry](../../extension-registry/README.md) you make it available to use in different extension kinds that requires a repository alias.
57+
58+
Some of the common repository interfaces are:
59+
* [UmbDetailRepository](./repository-types/detail-repository.md) - for detail views of a single entity.
60+
* [UmbCollectionRepository](./repository-types/collection-repository.md) - for collection views of multiple entities.
61+
* [UmbTreeRepository](./repository-types/tree-repository.md) - for tree structures of entities.
62+
* [UmbItemRepository](./repository-types/item-repository.md) - for item requests.
63+
64+
See the example below of how to register a custom repository:
65+
66+
```typescript
67+
import { umbExtensionsRegistry } from "@umbraco-cms/backoffice/extension-registry";
68+
69+
interface MyEntityDetailModel {
70+
unique: string;
71+
entityType: string;
72+
}
73+
74+
class MyEntityDetailRepository implements UmbDetailRepository<MyEntityDetailModel> {
75+
// Implement repository methods here
76+
}
77+
78+
const repositoryManifest = {
79+
type: "repository",
80+
alias: "My.Repository.EntityDetail",
81+
name: "My Entity Detail Repository",
82+
api: MyRepository,
83+
};
84+
85+
umbExtensionsRegistry.register(repositoryManifest);
86+
```
87+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Collection Repository
2+
3+
A collection repository is a specific type of repository designed to handle operations related to collections of items. It provides methods to request collections of data in a filtered and paginated manner.
4+
5+
The interface below is simplified for clarity and omits return types and arguments. See full interfaces in the [UI API Documentation](https://apidocs.umbraco.com/v17/ui-api/interfaces/packages_core_collection.UmbCollectionRepository.html)
6+
7+
```typescript
8+
interface UmbCollectionRepository {
9+
requestCollection();
10+
}
11+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Detail Repository
2+
3+
A detail repository is a specific type of repository designed to handle operations related to individual entities. It provides methods to create, retrieve, update, and delete single entities.
4+
5+
The interface below is simplified for clarity and omits return types and arguments. See full interfaces in the [UI API Documentation](https://apidocs.umbraco.com/v17/ui-api/interfaces/packages_core_repository.UmbDetailRepository.html)
6+
7+
```typescript
8+
interface UmbDetailRepository {
9+
createScaffold();
10+
create();
11+
requestByUnique();
12+
save();
13+
delete();
14+
}
15+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Item Repository
2+
3+
An item repository is a specific type of repository designed to handle operations related to multiple individual items. It provides methods to request multiple items based on unique identifiers.
4+
5+
The interface below is simplified for clarity and omits return types and arguments. See full interfaces in the [UI API Documentation](https://apidocs.umbraco.com/v17/ui-api/interfaces/packages_core_repository.UmbItemRepository.html)
6+
7+
```typescript
8+
interface UmbItemRepository {
9+
requestItems();
10+
}
11+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tree Repository
2+
3+
A tree repository is a specific type of repository designed to handle operations related to tree data structures. It provides methods to request tree roots, tree items, and their ancestors.
4+
5+
The interface below is simplified for clarity and omits return types and arguments. See full interfaces in the [UI API Documentation](https://apidocs.umbraco.com/v17/ui-api/interfaces/packages_core_tree.UmbTreeRepository.html)
6+
7+
```typescript
8+
interface UmbTreeRepository {
9+
requestTreeRoot();
10+
requestTreeRootItems();
11+
requestTreeItemsOf();
12+
requestTreeItemAncestors();
13+
}
14+
```

0 commit comments

Comments
 (0)