[ $davids.sh ] — david shekunts blog

🐱 **Node.js + TypeScript + SQL = disappointment... but there's hope

# [ $davids.sh ] · message #155

🐱 **Node.js + TypeScript + SQL = disappointment... but there's hope

**I've been trying to find an optimal solution for working with SQL databases on Node.js + TypeScript for a long time

As I've written before, I don't like ORMs, but what about SQL Builder and Typed SQL?

The first thing that comes to mind is Knex, but:

Firstly, although it has TypeScript support, it doesn't do much with it (it can't handle aliases, select only through a cumbersome construction db.ref(...).withSchema(...) and often without typing; where + join = pain)

Secondly, and this is, in my opinion, incredibly wild, you won't find almost any content on the topic of working with Knex + TypeScript, even on StackOverflow (reminder: 17k stars on Github)

The projects that interest me and that I will be implementing in the near future are:

  • kysely – Knex, but written in TS from the start and able to do what Knex can't
  • pgtyped – write SQL, run the pgtyped client, it checks that it's correct SQL for your database and exports types
  • drizzle – although it's an ORM, it works like a SQL Builder
  • slqc – an amazing library for Go, which is currently being ported to TypeScript (so it's not ready yet, but worth keeping an eye on)

The problem with these technologies is that they're a bit raw and only just starting to be discussed in the media (1, 2), but this is definitely the direction to look

**Okay, what if you already have Knex, an ORM, or even pure SQL and can't switch?

**At the moment, my solution is integration tests.

I've set up a system that allows me to write an integration test in a couple of minutes and check that all queries work correctly. And that's not counting all the other benefits of integration testing.

I'll definitely share all my settings, because I've already transferred them to 4 large projects and they work perfectly everywhere.

What do you use?

  • @ Taras 🫧 · # 295

    What do you think about objection.js? Should it be used?

  • @ [ $davids.sh ] · # 296

    I wrote a whole post about it and will post it tomorrow)

    By the way, feel free to use "you" informally.

  • @ Ivan ITK 🚫 · # 300

    Several projects are in production using it, and there have only been minor issues, but everything is solvable.
    Overall, I would call it one of the most well-developed ORMs, even among modern ones that are updated daily.
    As for the downsides, contributing to it is genuinely painful. For example, to extend a dialect updated in Knex, you need to write abstractions on top of it in Objection.

  • @ Taras 🫧 · # 301

    I personally really liked the join syntax, even though it's a dead ORM, but I definitely used it better than Sequelize 😅

  • @ Ivan ITK 🚫 · # 303

    Graph queries are pretty cool, and you can also use different methods to choose between JOIN or SELECT IN.