I have recently started working on Flask and Flask-SQLAlchemy. Coming from Django background I found Flask-SQLAlchmey to be quite complex. I have read that SQLAlchemy implements Data Mapper pattern while Django ORM is based on Active Record Pattern.
Here is a sample code written that implements repository pattern to access the database.
Here is another link of a comment by S.Lott (271k reputation) who says that ORM is the data access layer and it is separate from model.
My questions are these:
- Can you provide a practical use case in the above example or an example of your own where Data mapper pattern is useful? Everywhere I have read is that data mapper pattern is useful in complex situation, but not seen examples.
- Is using repositories pattern as in above case same as using a data mapper pattern?
- Does data mapper advocates write select queries in a different class than the model as done in the example?
- Why is
Question.query.filter_by(text = text).all()not better to use than
db.session.query(Question).filter(Question.text == text).all()?
This is not a duplicate of DataMapper vs ActiveRecord pattern because this just tells the definition, I am more interested in the practical examples.