Repository
When aggregate persistence support is enabled, the SDK provides access to the database through a repository. Using this, the connection to the underlying database is automatically established. As Aggregates are considered as a persistable unit, one repository per root entity is created which allows to read items from the database. Each of the returned instances offer a persist
and delete
method which can be used for saving and deleting data in the database.
Access the repository
The repository is provided within the scope of command, service or agent implementations. The root entities are grouped within namespaces.
// access repository this.repo.ns.RootEntityIdentifier;
Find
The find
method offered by the repository allows to load one or many aggregates from the database. It takes a RepositoryFindConfiguration
as first input parameter and a filter expression as a second parameter. Using the parameters, the returned result can be influenced.
Example:
// load all instances of a certain root entity type from the database const rootEntityInstances = await this.repo.ns.RootEntityIdentifier.find(); // loop over instances for (const inst of rootEntityInstances) { // do something // ... }
Example using find configuration and filter expression:
// create a special find configuration const findConfiguration = { // include child instances - default is false includeSubentities: true, // restrict the received result to a maximum amount - format is "<offset>,<amount>" limit: "0,20", // overwrites the sort order of the received instances - format is "<localIdentifier>,<ASC|DESC>" sortBy: "customerID,DESC" }; // create a filter expression that only returns items with a special name const filterExpression = `name == "${name}"`; // load instances from database const rootEntityInstances = await this.repo.ns.RootEntityIdentifier.find(findConfiguration, filterExpression);
A more detailed description of filter expressions is available at filter expressions
Find by id
The findById
method offered by the repository allows to load one exact aggregate from the database by using the provided id. The id is an auto generated uuid which is e.g. used when a root entity is referenced in another entity. In case that an instance with the provided id cannot be found in the database, an AggregateNotFoundError
will be thrown.
Example:
// load instance from database const rootEntityInstance = await this.repo.ns.RootEntityIdentifier.findById(`EntityId`); // save instance in database await rootEntityInstance.persist(); // delete instance from database await rootEntityInstance.delete();
Factory Commands
The repository also provides access to the factory commands of a root entity. To find out more about this, please check Trigger Commands.