DEV Community

Discussion on: Repository Pattern with Typescript and Node.js

Collapse
 
joaolss profile image
João Lucas Silva

IMHO, knex and typescript don't go together, prisma has much better typescript support and they have good solutions for nested queries, inclusions and selections, you should check it out!

Collapse
 
highasthedn profile image
highasthedn

Prisma looks quite awesome but knex has three features we need in our software which I could not do with Prisma.

  1. Run migrations programmatically without cli
  2. Run different sets of migrations (we have to different database schemas)
  3. Select the database connection dynamically on each api call

Those features are running rock solid with knex, in combination with Objection I have a good typescript support. Knex might look not as fancy as Prisma and feels more old fashioned, but I would not switch to Prisma in their current stage of development

Collapse
 
joaolss profile image
João Lucas Silva
  1. We use exec to run migrations programmatically using the CLI, not perfect but works just fine
  2. You are able to have multiple schema files and manage multiple migrations
  3. You are able to select the database connection dynamically when initializing prisma client

Of course knex is not garbage, and it's been one of the most used query builders for ages, but in my company we are making the transition from knex to prisma because as i said in another comment, the advantages are greater than the drawbacks

Collapse
 
nicolasdanelon profile image
Nicolás Danelón

prisma is not production ready at all :(
already tested

Collapse
 
joaolss profile image
João Lucas Silva

We use prisma in production, there is two main drwabacks: no migration rollback support and no long running transaction support, everything else works just fine, and so far the benefits are far greater than the drawbacks

Collapse
 
fyapy profile image
aabdullin • Edited

Never worked with Prisma.

But i see one problems:

  1. Migrations support only SQL files. Yeah, in most cases like rename/add column it is enough. But we have cases when we need select data, and cardinaly change database schema and transform data, in this situations we need features like loops, primitive operations with strings/numbers. SQL support this too, but i find write JavaScript code much simpler and maintainable.

And the points because of which I do not consider Knex a bad choice:

  1. Goal of using of Knex query builder it is get predictable and readable query generator in low level implementation of repository, and get at the exit high level Data Layer with simple API for our needs.
  2. Yeah, Knex not have awesome TypeScript support, but it good enough. Especially because we write types for Repository methods arguments and outputs.

Repository pattern not a panacea, it all depends on your tasks 😉

Collapse
 
joaolss profile image
João Lucas Silva

The first argument is bad just because it is not a good choice to use webdev tools to do data engineer stuff, if you need big data manipulations in the samedatabase that you use to keep ordinary webapp data, something is structurally wrong

Thread Thread
 
petoru profile image
Petr Vnenk

That's a more than good argument, if we're talking about the same thing (the SQL-only migrations). In ideal world that wouldn't happen, but in real world I definitely came into scenario where yes, something was wrong with the database schema, but if you get to that point, having Prisma with its current toolset will screw you over and try then explaining that to the client. Having some other ORM with programatic migrations solves a huge pain point there because you are able to run any logical operations you wish over the database.

I am also not sure I completely follow your point though. Database migrations should definitely be handled by a single tool which ORM's are usually for.