Active Record vs Data Mapper
What is the Active Record pattern?
In TypeORM you can use both the Active Record and the Data Mapper patterns.
Using the Active Record approach, you define all your query methods inside the model itself, and you save, remove, and load objects using model methods.
Simply said, the Active Record pattern is an approach to access your database within your models. You can read more about the Active Record pattern on Wikipedia.
Example:
All active-record entities must extend the BaseEntity
class, which provides methods to work with the entity. Example of how to work with such entity:
BaseEntity
has most of the methods of the standard Repository
. Most of the time you don't need to use Repository
or EntityManager
with active record entities.
Now let's say we want to create a function that returns users by first and last name. We can create such functions as a static method in a User
class:
And use it just like other methods:
What is the Data Mapper pattern?
In TypeORM you can use both the Active Record and Data Mapper patterns.
Using the Data Mapper approach, you define all your query methods in separate classes called "repositories", and you save, remove, and load objects using repositories. In data mapper your entities are very dumb - they just define their properties and may have some "dummy" methods.
Simply said, data mapper is an approach to access your database within repositories instead of models. You can read more about data mapper on Wikipedia.
Example:
Example of how to work with such entity:
In order to extend standard repository with custom methods, use custom repository pattern.
Which one should I choose?
The decision is up to you. Both strategies have their own cons and pros.
One thing we should always keep in mind with software development is how we are going to maintain our applications. The Data Mapper
approach helps with maintainability, which is more effective in larger apps. The Active Record
approach helps keep things simple which works well in smaller apps. And simplicity is always a key to better maintainability.
Last updated