3 ms·
> I don't see this as a given and I don't accept it in my own applications. If you need data, go to the data access layer. If it doesn't provide you what you ne
by lmm 3y ago
> I don't see this as a given and I don't accept it in my own applications. If you need data, go to the data access layer. If it doesn't provide you what you need, build a new repository / provider / whatever your pattern is.
Whichever layer you put it in, you need a way to compose two query fragments. Otherwise you have to write N*M queries manually instead of N+M query fragments.
Either you use an ORM to help you with this or you don't, with all the usual tradeoffs about using a library or not. But you still have to solve the problem. (Or you do a lot of tedious copy-paste work - with all the usual tradeoffs of that)
- jayd16 3y agoWait, why does every query fragment need to compose with every other? What are the N and Ms here?
- Spivak 3y agoSay you create a nice little utility function that makes a call to the db func1 and you write func2 that also makes a db query. If you have a func3 that needs func1 and func2 conceptually your options are: * Accept that this will just be two trips to the DB. * Write a new function func12 that writes a query to return all the needed data in one query and use that instead. * Have your tool be able to automatically compose the queries. If you do with the second option you have to do that with every combination of functions that end up being used together which is multiplicative in general.
- jayd16 3y agoWell sure but that not necessary for every combination of every call. Most likely those types of combinations are rare.
- preseinger 3y agousually, every interaction with your database is its own unique code path and query it's pretty rare for queries to be dynamically composed from arbitrary sub-queries if this is a problem you need to solve then ORMs certainly make more sense, but even in this case I find query builders to be more effective the interface between the application and the DB is actually a string! it's not an abstract data type, it doesn't benefit from being modeled by types
- Daishiman 3y agoYou never built admin interfaces, faceted search or dynamic query filters?
- preseinger 3y agosure, sometimes, rarely -- these are exceptions, not rules in general, it should not be possible for user input to produce arbitrarily complex queries against your database each input element in an HTML form should map to a well-defined parameter of a SQL query builder, like, you shouldn't be dynamically composing sub-queries based on the value of a text field, the value should add a where or join or whatever other clause to the single well-defined query sometimes this isn't possible but these should be super rare exceptions
- Daishiman 3y agoI prefer using something like Rails or Django to build 10 fully working CRUD interfaces with well-defined yet dynamic filters in a day instead of spending two weeks needlessly writing the equivalent code by hand.
- preseinger 3y agowhy would it take you two weeks to write 10 SQL simple queries?
- Daishiman 3y ago10 simple CRUDs you mean? With dynamic filters, admin UI, auth, tables, and so on? Because these frameworks allow you to do that in a single day.
- preseinger 3y agoi'm running out of ways to say that a CRUD endpoint should not have dynamism in the sense that you mean /users/:id should map to 1 endpoint that's parameterized on userid /search?userid=:userid&tag=:tag should map to 1 endpoint that's parameterized on userid and tag(s) endpoints should be simple to write
- x-shadowban 3y agoThis is a problem, seems like ctes and views are a possible ormless solution here
- lmm 3y agoCTEs and views are both a bit bigger than the parts that you would normally want to share and reuse, and they're also not very well standardised. Plus no-one really agrees on how dynamic tables should be, so you end up with the Excel maintainability problem of no real separation between code and data.