Home > Back-end >  Can I use prisma and node-postgres tougether
Can I use prisma and node-postgres tougether

Time:09-22

I started using prisma especially for handling database migrations. It handles that well. But there are many open issues for things that it does not handle well related to queries (biggest are related to queryRaw not always working as expected and with no straight forward way to use postgreSQL Row Level Security). Everything I've found to be a problem related to queries in prsima can easily be done in node-postgres.

I realize from the docs that some of these issues are not a problem in knexjs but prisma has a more feature rich migration setup (automatic and can be customized).

Can I safely use prisma and node-postgres together? If so, how? For example use prisma for schema design and database migrations and use node-postgres for all my query logic?

CodePudding user response:

Yes, you can safely use prisma and node-postgres together.

Your workflow will look like the following:

  1. Create a Prisma Schema to define your models and underlying database tables.
  2. Use the Prisma CLI (the prisma npm library) to run migrations to your database. This will generate the client in node_modules/.prisma/client directory, which you could optionally delete as you won't be using it.
  3. Instead of using generated Prisma Client inside your node application, use the node-postgres library to run queries against the database in your application.

Since you're only using Prisma to make any kind of migration or changes to the database structure, there's no risk of your prisma schema not being synced with your database. On the off chance this does happen, you can always get it back in sync using prisma db pull.

Furthermore, since your application is not going to use Prisma client to connect to the database for running queries, node-postgres will handle the connection pool.

  • Related