Prisma is a wonderful ORM and it is very easy to setup. You can use Prisma with any database. Prisma is a type-safe database client. It helps you to write queries in your language and it converts it to SQL queries.
Basic Project Setup
If you look at my previous article on Setting up Express, I'm using a specific folder structure. I'll be using the same folder structure here.
You can checkout my previous article Setting up Express to setup a project with javascript or typescript.
Prisma Setup
First, you need to install Prisma globally.
Then, you need to initialize Prisma in your project.
This will create a prisma
folder in your project. Inside the prisma
folder, you will find a schema.prisma
file. This is where you define your database schema.
Database Connection
Overall you have two choices either you can use a SQL based database or a NoSQL based database. Here is the official doc I'll show you how to work with MongoDB and Postgres, which are the two popular choices.
In the prisma/schema.prisma
file, you can define your database connection.
The file looks like this
The provider is the type of database you are using. In this case, it is "postgresql". You can change it to "mongodb" if you are using MongoDB.
The url comes from the environment variable. You can set the environment variable in a .env
file.
Now define a model
You can define your model in the schema.prisma
file. Here is an example of a user model.
Query database with Prisma Client
Prisma Client is a type-safe database client. It helps you to write queries in your language and it converts it to SQL queries.
Install the client
You will have to generate the client. You can do this by running the following command.
This will prepare the client for you. You can now use the client to query the database.
Now setup the client object as per the documentation. I like to do it inside db/prisma-client.ts
Now you can import the client and directly use it like this :
SQL vs NoSQL and Docker Images
There is a slight difference in the connection setup for SQL and NoSQL databases.
SQL or relational databases require a table structure and needs to be migrated before you can use it. You can use the following command to migrate the database.
This will create the tables in the database or update the existing table. The step is not required for non relational databases like MongoDB.
MongoDB
If you don't have MongoDB installed, you can use the docker image. You can run the following command to start a MongoDB instance.
If you are using MongoDB, you can define the connection like this.
Postgres
If you don't have Postgres installed, you can use the docker image. You can run the following command to start a Postgres instance.
If you are using Postgres, you can define the connection like this.
Prisma is a wonderful ORM and it is very easy to setup. You can use Prisma with many databases, it makes querying databases easier and is widely used. Enjoy using Prisma!