Data repositories give a common interface to store the models in databases.
The different repositories share the next operations:
- Add an
Entityobject or list of
Entityobjects to the repository, if it already exist, it updates the stored attributes. If you want to merge the entities before adding them to the repository, use the
- Remove an
Entityobject form the repository.
- Obtain an
Entityfrom the repository by it's ID. Optionally you can pass the
attributeargument if you want to filter by other than the
id_, remember that only one attribute can be returned, so the chosen attribute should uniquely identify the entity.
- Persist the changes into the repository.
- Get all the entities of a type or types from the repository. If no argument is given, it will return all entities.
- Get the entities whose attributes match a condition or regular expression.
- Get the first entity of a type or types of the repository. If no argument is given, it will return the first of any type of entity.
- Get the last entity of a type or types of the repository. If no argument is given, it will return the first of any type of entity.
- Run the migrations of the repository schema.
- Close the connection to the database.
- Property that tells if the connection to the database is closed.
- Remove all the entities from the repository.
Changes in the repository aren't persisted until you run
(with the exception of
To change the repository you only need to change the url passed to
load_repository. We have the next repositories:
- FakeRepository: is the simplest implementation of the repository pattern, meant to be used for the tests and early phases of development.
- TinyDBRepository: is the implementation of the repository pattern for the local NoSQL TinyDB database. You can use it in the early stages of the project where the data schema is yet unstable and you don't have enough entities to have performance issues.
- PypikaRepository: is the implementation of the repository pattern for the relational databases. It's meant for the stages of the project where the schema is more stable and you need the improved performance of these types of databases.