top | item 17891129

(no title)

saas_co_de | 7 years ago

> const selectBy = (table, column, value) => db.query(`SELECT * FROM :table WHERE :column = :value`, {table, column, value})

> const selectOneBy = (table, column, value) => { const res = selectBy(table, column, value); return res && res.shift() }

Two or three lines gives you about 80% of the value in any ORM and avoids the -20% value found in the other 100k LOC.

discuss

order

idoubtit|7 years ago

> Two or three lines gives you about 80% of the value in any ORM and avoids the -20% value found in the other 100k LOC.

I have a hard time believing this when the two lines above are obviously wrong. You can't use :column to escape the name of a column in SQL. So your code has to protect it, which is not easy if you wan't to be portable. MySQL will generally use `column` but may be configured to use the standard "column".

Anyway, selectAllBy() and selectOneBy() are not enough to be confortable. I appreciate the static completion in Something.find({id: 1}).complete, the relative queries like Post.find({id: 1}).author, and many other things that help against typos, help code faster and make the code more readable.

I agree with the OP that fallthroughs are much needed, because writing custom SQL is sometimes simpler, and sometimes much more performant. Usually, its mostly about writing SomeModel.findAllBySql() which most ActiveRecord implementations provide.

saas_co_de|7 years ago

[deleted]