DEV Community

Repository Pattern with Typescript and Node.js

aabdullin on October 25, 2021

If you working with Node.js you probably interact with the database (MongoDB, PostgreSQL, and etc) via ORM. But sometimes typical ORM does not co...
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
 
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.

Collapse
 
michael_angelobiay_b290c profile image
Michael Angelo Biay

We are currently looking for a US developer. It would be ideal if we utilized your account to handle financial matters, including account growth and bidding, while you handled the development. It would take 2-3 years to raise your account to a Job Success, top-rated level on your own, but thanks to our extensive network of clients, we can improve your account in just two months. We will pay you 15%-20% of the profits. If u have any question, DM me please. thanks

Collapse
 
rindra_rasolofonirina_ac5 profile image
Rindra Rasolofonirina

I don’t get the fact that you use an async executor while you instantiate a new Promise

Collapse
 
fyapy profile image
aabdullin

Indeed, fixed