0

I would appreciate hearing some opinions on the best approach to creating a light-weight data mapping layer between some python objects and a postgres database. I'm using the psycopg2 library, and plan to use pgbouncer. I looked at this: http://codeinthehole.com/writing/domain-model-mapper-a-php-data-mapper-implementation/ for a basic idea, but it isn't clear how they are closing the connection after the queries. The component I'm building is similar to a forum in that it has your familiar user, post, forum tables, etc.

Jaigus
  • 1,422
  • 1
  • 16
  • 31

1 Answers1

3

The best approach is not to create a new layer, but to use an existing one. SQLAlchemy is the most full-featured, but others are also good, such as Storm.

Maybe you could explain why you want to write a new one?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I wanted something more light-weight than sql-alchemy; and I'm convinced that building a light-weight alternative will likely prove to be the better choice later on, if/when the database grows large. – Jaigus Jul 03 '12 at 00:36
  • You might be underestimating the effort involved in building something like this. Also, the size of the package may not correlate with the run-time overhead of the package. In fact, it might be exactly the opposite: a large package may be large because of the optimizations in place. – Ned Batchelder Jul 03 '12 at 00:43
  • Perhaps I am, but I don't mind some effort in the beginning that may save complications later on. I just didn't think learning and using an orm would be worth it if I'd just have to drop down to raw sql queries anyway later on when things need to change because of complexity. – Jaigus Jul 03 '12 at 00:56
  • @RoryBreaker: ok, sounds like you and I have different ideas about what effort might pay off later and what won't. Whatever you do, be very careful about SQL injections. – Ned Batchelder Jul 03 '12 at 01:07
  • Hello again Ned. I was ignorant to the fact that sqlalchemy's DAO component can be used exclusively, without having to to use the ORM, and its also lightweight being that its only the expression language. I want to use an approach like this: http://www.tonymarston.net/php-mysql/infrastructure.html#generic.table.class where the database and models sort of meet each other half way. I was thinking of simply including a table object within each of the corresponding models; does this seem a bit clunky to you? – Jaigus Jul 15 '12 at 21:34