What is Prisma?
Prisma is a next-generation, open-source database toolkit and Object-Relational Mapping (ORM) tool for building and managing modern web applications. It provides a set of tools and libraries that make it easier to work with databases, allowing developers to interact with databases using a more intuitive and efficient API
it can be configured with any database e.g MYSQL, MongoDB, PostgreSQL, Sqlite3
it also provides a powerful Prisma Studio, with the help of a tabular interface you can check your data from local databases or app is working and connected or not
you can also take advantage of studio features-
- performing CRUD operations eg. adding, modifying and deleting the data from table records
- viewing your data by filtering, sorting or paginating options
How to install Prisma?
prerequisites -
- you should have installed Nodejs v16.13.0 or higher
- database- PostgreSQL
setting up project
in the first step, we will be creating a directory and navigate to it
open your terminal and go to Desktop
next, install Prisma cli as a development dependency
now define your database provider in our project you can choose any Database as per your choice
the first method is using data source as the database name
for example -
$ prisma init --datasource-provider mysql
in the second method, we will add a database URL eg.
$ prisma init --url mysql://user:password@localhost:3306/mydb
you choose as per your convenience we are going with 1st method using data source provider
given command creates a prisma folder which contains the file name as schema.prisma also creates a .env file and sets your env variable in it with the name DATABASE_URL
if you are not seeing the .env file then copy it from below
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
update your Database name, port, username and password from.env
some times people forgot their password so please make sure that your connection URL is working properly
modeling your schema
add the following models in prisma/schem.prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
At this moment you have schema but not database let's make it through migrate command
$ npx prisma migrate dev --name init
you will get this below output
above migration command creates migrations
folder inside of the Prisma folder
now you have successfully created databases and tables as per the mentioned schema let's verify this through Prisma studio
$ npx prisma studio
the command will redirect you to this URL : http://localhost:5555/
if you are seeing the below interface then Big Congratulations you have made it