pull down to refresh
Haha sounds like a lot of fun!
Okay this answers my question, it's a pain with SQL. I'm guessing no easy way of writing raw SQL.
What I do like about these libraries is the data modelling and relationships/graph support.
reply
pull down to refresh
Haha sounds like a lot of fun!
Okay this answers my question, it's a pain with SQL. I'm guessing no easy way of writing raw SQL.
What I do like about these libraries is the data modelling and relationships/graph support.
I am going to use responses from other people since I can't explain it better than they already did:
-- https://news.ycombinator.com/item?id=36500185
-- https://news.ycombinator.com/item?id=36500429
For example, I am not sure if this code from the article even works:
const postRepository = connection.getRepository(Post); await postRepository .createQueryBuilder() .update(Post) .set({ status: 'archived' }) .where("authorId IN (SELECT id FROM author WHERE company = :company)", { company: 'Hooli' }) .execute();According to the docs, subqueries should be created like this:
const postRepository = connection.getRepository(Post); await postRepository .createQueryBuilder() .update(Post) .set({ status: 'archived' }) .where(qb => const subQuery = qb .subQuery() .select("author.id") .from(Author, "author") .where("author.company = :company") .getQuery() return "post.authorId IN " + subQuery ) .setParameter("company", "Hooli") .execute();And even then, TypeORM would find a way to complain. For example, TypeORM was not consistent in using quotation marks (at least when I used it). So it could be that this query above still not works, because
authorIdis not quoted. But this should finally work (maybe):- return "post.authorId IN " + subQuery + return 'post."authorId" IN ' + subQueryAnd if it doesn't complain, it will create a really weird SQL query since it doesn't understand enough about your schema so it can't assume some things so it builds a query which is not exactly what you expect. But the query may return correct results first. Then you hit production and you get weird results.
So at the end, I had to know anyway how my SQL query should look like so it does exactly what I want, in 100% of the cases. And then I had to fight with TypeORM to generate this query. And then some more to get some acceptable performance.
At the end, I ditched it for
knexand a few months later another team also ditched it and we never looked back.There was also a hilarious issue in their Github where someone complained that he lost all rows in a table because a parameter he used to select rows was undefined and thus his
.where("id", id)was silently dropped. The maintainer said this was not a bug but expected behavior. Wasn't able to find it between all these issues.You see, I had a lot of fun with TypeORM, lol