Skip to content

Commit

Permalink
write about repos
Browse files Browse the repository at this point in the history
  • Loading branch information
Metauriel committed Jan 25, 2024
1 parent 03a0122 commit da66d16
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/schulcloud-server/Coding-Guidelines/repositories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Repositories

The repository is responsible to provide domain objects for the domain layer. Typically, it does so by accessing a database.

Since the domain layer should be isolated (not have knowledge of the outer layers), the domain layer should have an interface definition for each repository it wants to access. This naturally means that the interface can only mention domain objects, without any knowledge about database entities.

```Typescript
// somewhere in the domain layer...
export interface SchoolRepo {
getSchoolById(schoolId: EntityId): Promise<School>;
}
```

The repository itself can now implement this interface, in this example using MikroOrm to get data from a database

```Typescript
@Injectable()
export class SchoolMikroOrmRepo implements SchoolRepo {
constructor(private readonly em: EntityManager) {}

public async getSchoolById(schoolId: EntityId): Promise<School> {
const entity = await this.em.findOneOrFail(
SchoolEntity,
{ id: schoolId },
);

const school = SchoolEntityMapper.mapToDo(entity);

return school;
}
}
```

Note that the internal entity manager uses a different datatype, the SchoolEntity, to represent schooldata. This type includes information that is specific to mikroorm, and should not be mixed into the domain object definition. A mapper is used to explicitly map the entity to the domain object, and vice versa.

0 comments on commit da66d16

Please sign in to comment.