Closed vladfaust closed 6 years ago
Maybe introduce some kind of queries container, which would store and validate custom queries on compilation/launch?
GET_USER = Queries.add(User, "SELECT * FROM users WHERE id = ?")
user = repo.query(GET_USER)
GET_POSTS = Queries.add([Post], <<-SQL
SELECT posts.*, '' AS _author, author.*
FROM posts
JOIN users AS author ON author.id = posts.author_id
ORDER BY created_at
SQL)
posts = repo.query(GET_POSTS)
How about leaving all as-is, but trying to get rid of Model
class and introducing some kind of baking mechanism?
baked_query = Query(User).where(id: 0).bake # Stores "SELECT * FROM user WHERE id = ?"
user = repo.query_one(baked_query, 42) # Super-performant, right?
And it's already here:
class Foo
QUERY = Query(User).where(id: 0).to_s # Results in plain SQL
def foo
user = repo.query_one(User, QUERY, 1)
end
end
Thanks to dedf31756cca72f07477e49cffdaa243f7351739, repo.query(User.where(id: 42))
is now possible, which is good enough IMO :smirk:
If a developer does migrations on his own, which requires SQL knowledge, why using handy
Query
then? Let 'em write raw SQL queries!Pros:
Query(T)
allows to get rid ofModel
class, turning model into a set of modulesBetter performance - store SQL queries as constantsAlready possibleAbility to validate SQL on launch/compilationAlready possibleCan use custom Query builderAlready possibleCons:
JOIN
sturns into
and
turns into
rfc 🤔