r/node • u/gajus0 • Feb 12 '19
Stop using Knex.js (and get $30)
https://medium.com/@gajus/stop-using-knex-js-and-earn-30-bf410349856c14
u/chmod777 Feb 12 '19
lets ignore that knex is fully promisified and can use callbacks as well as async/await.
then lets make a case where using knexRaw solves our problem, and disallow it.
14
u/cinnapear Feb 12 '19
I love the example SQL included that he challenges anyone to duplicate in knex.js.
Also I love the argument that because knex.js allows you to build dynamic queries based on javascript code that means you're allowing the user to dictate the queries. Maybe don't let the user have http access to your server because you're allowing them access to your complete filesystem? Oh wait, you aren't.
Basically this is an ad for his knex.js alternative.
7
u/dahlesreb Feb 12 '19
Using SQL query builder is an anti-pattern
No, it's not. SQL is not the natural syntax in Javascript, Javascript is. Knex really just gives you the ability to generate SQL using Javascript syntax. It's just building SQL strings, there's no behind-the-scenes magic going on with Knex like there is with an ORM. Building the strings yourself doesn't give you any particular advantage. If you do need to, it's always a toString() away in Knex.
4
u/Something_Sexy Feb 12 '19
Instead of bashing knex.js maybe he should work on keeping his website up.
1
u/coolaj86 Oct 26 '23
This is still one of the best articles out there on the topic.
TL;DR You have to learn SQL either way, so how about better tools focused on SQL - less abstraction, easier to understand, even better tooling (and fewer false promises).
We need more of this.
2
u/gajus0 Oct 27 '23
I only regret the title. Never did I think this is going to be the most quoted article I've written haha
1
3
u/myockey Feb 12 '19
This isn't a use case for Knex.js. Your application probably does not, as a general rule, produce a wide variety of queries this complicated unless you're doing star schema type things (and if you are then Knex is the wrong tool). If it does then you have bigger problems than the decision to include Knex in your project. Queries like this are the kind of specialized tasks best suited to keeping in a stored procedure on the database server and referring to them by name in your application code. The hundreds of other simpler queries are going to look just fine in Knex.
5
u/sneffer Feb 12 '19
* reads the post *
Wow, almost nothing in this is post is good. I really hope the sub doesn't buy into this.
* sees OP is getting shit on in the comments *
Faith restored.
9
u/tuxedo25 Feb 12 '19
> SQL has been around since 1974 and it is a standardised... language.
hahahahahahahhahaha
7
u/jeffreyhamby Feb 12 '19
ISO/IEC 9075, better known as SQL, is in fact a standardized database language.
2
u/hopfield Feb 12 '19
Yeah but every DB implements in differently.
5
u/jeffreyhamby Feb 12 '19
Not every.
4
u/EvanCarroll Feb 12 '19 edited Feb 12 '19
Every. His literal example of something Knex.JS can't do. Directly from the Pg docs,
This command conforms to the SQL standard, except that the FROM and RETURNING clauses are PostgreSQL extensions, as is the ability to use
WITHwithUPDATE.The SQL spec is really, really rough.
1
u/javitury Feb 12 '19 edited Feb 12 '19
I clearly see the advantages of slonik for building queries. It's more flexible. But what about the abstraction capabilities of knex? How does slonik compete? I think that knex abstraction capabilities are the foundation of an ecosystem that includes the bookshelf and objection ORMs.
As a side note, the use of tagged template literals reminds me of another query builder, sqorn. At first I was impressed and wondered why other sql libraries were not using it.
The reason I was into tagged template literals is because I'm writing a patch(pull requrest) that adds support for binded parameters in sequelize. I considered using tagged template literals, but I faced a problem: they are hard to compose. You cannot do this natively:
const taggedTemplateLiteral = sql`foo = ${bar}`;
sql`SELECT id FROM ${table} WHERE ${taggedTemplateLiteral} LIMIT ${limit}`You have to use objects instead. On top of that, the binded parameter losses context. This makes it difficult for sql libraries that target multiple DB vendors. I see this as another abstraction problem.
-2
u/gajus0 Feb 12 '19
Hi! I am glancing through Sqorm and it looks really good. Do you know if it is possible to generate just a part of the query? e.g. just the WHERE block as opposed to the entire query?
If yes, I would promote use of Sqorm with
Slonik#raw. Good synergy.0
u/javitury Feb 12 '19 edited Feb 16 '19
Yes, sqorn implements query composability.
// EXTEND QUERIES sq.extend( sq.from`book`, sq.return`distinct author`, sq.where({ genre: 'Fantasy' }), sq.where({ language: 'French' }) ) // EMBED Queries sq.return`now() today, (${sq.return`now() + '1 day'`}) tomorrow` // select now() today, (select now() + '1 day') tomorrowSqorn uses helper functions to detect and combine tagged templates into a single statement.
EDIT: Sqorn identifies template literals because they are string Arrays with the special raw property. This raw property cannot be create on regular arrays using JSON or XML. https://github.com/sqorn/sqorn/issues/57
ORIGINAL: Perhaps I didn't understand the sqorn code well enough, but I don't trust the implementation. This is the helper function that tells apart parameters to be binded from template literals to be composed.
const isTaggedTemplate = ([strings]) => Array.isArray(strings) && strings.rawThis only checks the structure of the argument. Imagine this is how your API finds users.
sq.where`id IN ${ctx.body.users}`Suppose that a malicious user submits a value for
ctx.body.usersthat has the same structure as a tagged template, for instancectx.body.users = [['(1234) ; DROP ALL TABLES; --', ''], 'dummy']. How does sqorn deal with this malicious argument? I never found out. Personally, I favor using objects for composing query fragments. Malicious users cannot spoof an object class in json data or XML.0
u/gajus0 Feb 12 '19
Cannot comment on Sqorn implemnetation, but in case of Slonik,
[['(1234) ; DROP ALL TABLES; --', ''], 'dummy']would never be interpolated into the query. Instead, the query would get positional parameters representing the input; query text with placeholders and actual values would be sent in separate packages.
2
u/AxelTerizaki Feb 12 '19
I haven't looked at the article, but from my personal experience, I found Knex pretty counter intuitive and was going way faster writing my own SQL queries. There's something about Knex's syntax that made me scratch my head.
I guess it just depends on developer preference in the end, I just feel mroe at ease writing my queries myself.
2
u/caguiclajmg Feb 12 '19
Wrap the whole bullshit he wrote in a knex.raw() call, where's my 30 bucks?
-5
u/gajus0 Feb 12 '19
Note: Part of the article was removed. It was challenging reader to implement this query (and earlier a query with WITH block) using Knex.js without using Knex.js raw methods in return for USD 30 reward. While it is possible to write the latter query using Knex.js, it is admittedly not the best use case for Knex.js.
32
u/brtt3000 Feb 12 '19
Don't tell me what to do, author of an alternative.