Kex: an ORM-less for Knex

My recent project had short deadlines. I wanted to go with a safe approach and use tools that I know. One of them didn’t fit. It was an ORM, with which I’ve been working on previous projects.

The problems started to occur quite quickly. The way how I had to make queries was pulling me down. I had to spend much time going through the docs and StackOverflow to find the answers. This tool was supposed to be easy to use, and I found myself duplicating code and googling for answers. It was no good.

Shortly, I decided to replace it with something different. At this point, I knew that I want to use something based on Knex. Knex offers an excellent migration tool, and the query builder is friendly, robust, and easy to use. There’s only one problem - it’s just a query builder. For me it felt almost like writing raw SQL queries. So I started looking for an ORM based on Knex. This choice was my second mistake.

There are pretty decent ORMs based on Knex. However, all of them come with a price - complexity. I didn’t want to lose more time on setting them up and migrating the existing application to a new ORM.

So a good mentor suggested sticking to Knex. At first, I was reluctant. After some considerations, I decided to give it a try. And this was the right decision. The project has ended, and I started considering using just Knex in my next project.

There are things I like in ORMs like Eloquent, or Lucid. They offer ways to extend the query builder and add some useful functionalities. I started to wonder, is it possible to “make” Knex to be something similar to them?

Turns out, yes! The last two weeks I’ve spent implementing this idea. It resulted in a new package - @baethon/kex.

Kex borrows the concept of a “Model” but restricts it only to query builder. You can define scopes, use relations, and even things like timestamps or soft deletes. It’s not an ORM, just an extended query builder for Knex. Give it a try, and let me know what you think!