Open In App

How to Connect Prisma with any Database?

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Prisma simplifies database access by providing a type-safe and intuitive ORM (Object-Relational Mapping) layer. It seamlessly connects with various databases, including PostgreSQL, MySQL, SQLite, and SQL Server.

With Prisma, developers can efficiently manage database interactions, write concise queries, and focus on building robust applications without worrying about low-level database operations.

Why Do We Use Prisma?

Prisma is a really helpful tool for managing databases. Normally, when you move your web app’s data to a new database, it’s a big hassle. You have to redo all the instructions for how the database works, which takes a lot of time and needs you to know a bunch of different ways to talk to different databases. But Prisma changes that. With Prisma, you only have to write those instructions once, in a language that Prisma understands. Then, no matter what kind of database you’re using, Prisma helps you talk to it in the same way.

This means that whether one is working with PostgreSQL, MySQL, or another database, they can seamlessly interact with their data using the same query language, streamlining development and ensuring consistency across different database systems. It saves time and makes things simpler for developers, so they can focus on making great apps instead of getting stuck in database problems.

What is Prisma?

​Prisma is an advanced Object-Relational Mapping (ORM) tool that simplifies database access and manipulation in software development. It allows developers to interact with databases using a type-safe and auto-generated query builder, streamlining the process of handling database operations. Prisma supports multiple database systems and provides a modern and efficient approach to database management in various programming languages, such as TypeScript and JavaScript. Its features include automatic schema migrations, strong typing, and a clean API, contributing to improved developer productivity and code maintainability.

Note: Here I am using Supabase to show you how to connect with the database. You can use any database management system that you like; just change the database URL, and you are ready to go.

What is Supabase?

Supabase is a relational database designed for simplicity and scalability, providing an open-source alternative with a focus on real-time features. It leverages the power of PostgreSQL and offers an intuitive API for seamless integration with applications, making database operations straightforward for developers.​

Let’s see the step-by-step procedure for connecting the prisma with ​superbase.

*Do these commands in your terminal in project directory.

Step 1: Install Prisma on the website. To install Prisma, open the terminal and go to the directory of your folder where your website’s main folder is located. Just copy the below command, paste it on the terminal, and press enter.

npm install prisma --save-dev

Step 2: Prisma init. To initialize Prisma, open the terminal and go to the directory of your folder where your website’s main folder is located. Just copy the below command, paste it on the terminal, and press enter.

npx prisma init

Now you can see one file is in the directory.

prisma-schema

schema.prisma file

Step 3: Go to the database site and register/log in. After login go to the dashboard and click on new project.

supabase

New Project

Step 4: After creating the project click on the storage and copy the API url from supabase. Then just go into the .env file in your root directory open it change the DATABASE_URL, and use your The finaldatabase project name and password.

# Connect to Supabase via connection pooling with Supavisor.

DATABASE_URL=”postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:6543/postgres?pgbouncer=true”

# Direct connection to the database. Used for migrations.

DIRECT_URL=”postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:5432/postgres”

Step 5: Now edit the above details. Set the provider according to your database. If you don’t know which provider is for your database system, then just google it and you will get to know.

supabase-prisma

Schema Prisma

Step 6: The final step is to push the database schema of your project to the database server. It creates a connection between your application and database system and updates the schema on the server.

npx prisma db push

you have successfully connected your project with database now you can create, update, delete, and fetch the data from the superbase.

Conclusion

Prisma streamlines database management, seamlessly connecting with diverse systems like Supabase. This integration simplifies development, boosts productivity, and allows developers to focus on creating robust applications without the hassle of intricate database operations.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads