Re:Ask HN: Is web programming a series of hacks on hacks?
by tracker1 on Monday September 12, 2016 @02:39PM
(#12482439)
honestly, in JS/Node, I find it easiest to use a SQL adapter that can handle template strings as parameterized queries...
const results = await sql.query`
SELECT ...
WHERE foo = ${bar}
`;
if (!results && results.length) return;
await myQueue.add(results);
Which works unbelievably well... There's not nearly as much need for boilerplate/translation layers in what is already a dynamic environment. I wrote a wrapper for ms-sql when migrating data, it took 2-3 days to get it done, but writing queries as above was so easy to work with it was incredibly nice. I'd rather work with a db that has a friendlier API to work with or abstract around... but writing a little template driven sql is often better than layers of boilerplate like an ORM.. and I still don't really get mongoose.
Re:Ask HN: Is web programming a series of hacks on hacks?
by tonyarkles on Monday September 12, 2016 @05:44PM
(#12483948)
Maybe I'm missing something, but that looks like SQL Injection ready to happen.
Re:Ask HN: Is web programming a series of hacks on hacks?
by tracker1 on Monday September 12, 2016 @07:30PM
(#12484524)
sql.query is a function that will receive two arrays, one is the strings part, the other is the injected values... the template processor takes those arrays and turns it into a parameterized query to the database.
Re:Ask HN: Is web programming a series of hacks on hacks?
by tonyarkles on Monday September 12, 2016 @10:12PM
(#12485180)
Very cool! In that example though... where's the array? And don't backticks do string interpolation?
Re:Ask HN: Is web programming a series of hacks on hacks?
by sime2009 on Tuesday September 13, 2016 @03:37AM
(#12486236)
Backticks by themselves will do string interpolation. But backticks with a function name in front will do something a bit different. In this case 'sql.query' is a function and the JS will pass it an array which represents the contents of the backtick string. There the function can do what it likes and return a result. 'sql.query' builds a proper (and safe!) SQL query and executes it.
The backtick feature in ES2015 is really cool and allows for some great DSL type features.
Re:Ask HN: Is web programming a series of hacks on hacks?
by alxndr on Tuesday September 13, 2016 @11:47AM
(#12489331)
This feature is apparently called "tagged template strings"; more on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re:Ask HN: Is web programming a series of hacks on hacks?
by tonyarkles on Friday September 16, 2016 @08:42PM
(#12518470)
Neat! Thanks for the awesome explanation!
Re:Ask HN: Is web programming a series of hacks on hacks?
by bschwindHN on Monday September 12, 2016 @10:20PM
(#12485212)
That looks fantastic, I'll take that any day over an ORM in JS.
I'm gonna stop you right there. Node is a helpful tool and I enjoy using it, but Sequelize was nothing but pure pain when I used it. You're much better off running raw SQL, or using a query builder like KnexJS.
I would never use Sequelize for more than a 1-table read or update.