Open blaix opened 8 years ago
When I say sql alchemy, I mean specifically the core: http://docs.sqlalchemy.org/en/latest/core/ - not the ORM
Consider rom-rb for inspiration: https://www.youtube.com/watch?v=9TBvWRgll64
A better source of inspiration is Ecto: https://hexdocs.pm/ecto/Ecto.html
I like the repo/schema/changeset/query breakdown.
Use the Entity/Repository pattern. An entity is any python object that can be persisted. A repository handles the persistence details for the entity.
Use SQL Alchemy to power repositories?
Repositories should be configured with an engine. I would like to make this explicit rather than depend on configuration. This will make it more flexible for situations where you want different dbs in different environments, or for different entities. For example:
An entity can be any python object, but woma should provide a convenience mixin for the usual style. For example:
It should be that simple. I want the freedom to manage my entity's attributes however I want (e.g. custom getters/setters) without worrying about stepping on the toes of the database mappings.
Each entity should have a repository that maps fields to the database:
Note: If the default
entity
isdict
, we can create repos for pure data.Repository
should provide standard methods for create, update, delete and getting all records or one record by primary key. For more complex queries, aquery
function could be provided that accepts a repository. It should not be exposed as part of the public interface of aRepository
. I want to encourage defining explicit methods for repo queries. For example:Making
query
a function and not a method onRepository
allows running ad-hoc queries in places where that might be needed like the shell and your tests, without requiring adding a method or exposing a huge query interface on every repo. (maybe?)Associations could be handled like this:
That could run into problems if we want
Comment
to a have an actualpost
field instead of just apost_id
(would require circular imports) but maybe we can provide a module path to the repo as a string instead of the actual class.