4 ms·
It's micro-service like, or some other crap? I had situation where was implementing something quite simple - an URL shortener. I didn't use PostgREST, but I de
by takeda 7y ago
It's micro-service like, or some other crap?
I had situation where was implementing something quite simple - an URL shortener. I didn't use PostgREST, but I decided to use ORM, because it was simple CRUD operation. It had an option to either use generated url or allow user to specify a custom one. And it worked as expected.
But then once completed I decided to add extra functionality, for example extra statistics, like what IPs were accessing it and how much. Adding expiration times etc.
I realized that ORM encouraged me to implement all of my logic in the application even when I actually would put less load on the database and made things simpler if I would let the database do many things for me and use types and functionality provided to me. I am not taking here about using stored procedures, I could do all operations as 1 at most 2 SQL statements. While ORM had to send multiple. In the end I dropped SQLAlchemy (this was python code) and just use psycopg2 directly didn't even bother with wrappers, just used built-in pools. It was also easier for me to make my code use two endpoints for reading and writing, so I can scale my code better.
I realized that ORM did not save me much code at all, it was the same amount of code with or without it, and without ORM I had greater control of what I wanted to do.
I previously believed that ORM was standing in a way when your application gets bigger, but my belief was ORM was good for small projects. Here I realized that it doesn't bring much benefits even for simple projects.
I think REST interface like this is doubling down on what ORM tries to do. Maybe it could be beneficial in places that don't have libraries to communicate with a database and only can make http requests?
- ruslan_talpa 7y agoIt's not doubling on the ORM part, it's not a orm replacement, it's a replacement for the entire api layer (that uses orm internally). I agree with the other point, this is meant to be used when your client can not ahve direct access to the db (browsers, 3rd parties). If you can connect to the db and use SQL, do that.
- FreelanceX 7y agoYou should try SqlAlchemy Core. It does not have the drawbacks of an ORM but still saves you the trouble of writing sql queries as raw strings.
- takeda 7y agoIt's better but still not great, because it tries to make your SQL independent of the database. So some things are harder to express that way. You have to understand your database, and then you need to understand how to express in SQLAlchemy to get your desired SQL statement. PyCharm actually has a database support, you can configure it so it connects to your database, then it will fetch database schema. After that it will recognize your SQL statements in the string, offer auto complete and even take these into account when refactoring your code. IMO this is how impedance mismatch should have been handled from the beginning.