How to make general purpose queries?
Hello everyone.
I am currently developing a medium-scale server using express and Kysely as a query builder (which I have adored so far).
However, with the growing codebase, I've noticed that many select, update and insert queries are similar between them.
That being said, it would be very useful to have a wrapper to perform those similar queries, which I've tried to build like so:
The problem here is that Kysely doesn't directly expose
ReferenceExpression<DB, ExtractTableAlias<DB, TB>>
, which is the required type from the where()
method.
Then I also have a problem with the filterBy
param, which at the moment always accepts Generated<number> | number | null | Generated<Date> | Date | string | [Generated<number> | number | null | Generated<Date> | Date | string]
.
Any help would be very helpful!Solution:Jump to solution
Hey 👋
Don't. You're building your own ORM, might as well use an ORM - at least for, as you say, plenty of your access patterns, and use Kysely only when you get value from it being specific or from its composition capabilities.
Some ORMs already work with Kysely (e.g. Prisma) - so you get a type-safe low-level escape hatch, some will in the future (e.g. Mikro, Zenstack, etc.).
...
3 Replies
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Thanks for reaching out, and yeah maybe I’ll just stick to your solution to keep the code as concise as possible.
However having a query wrapper which can handle also the where clause would be really really helpful.
Solution
Hey 👋
Don't. You're building your own ORM, might as well use an ORM - at least for, as you say, plenty of your access patterns, and use Kysely only when you get value from it being specific or from its composition capabilities.
Some ORMs already work with Kysely (e.g. Prisma) - so you get a type-safe low-level escape hatch, some will in the future (e.g. Mikro, Zenstack, etc.).
Or, do something else. e.g. write common helpers and use them with
$call
to narrow down and reduce complexity.