Skip to content

Introduction to Prisma

Filip Najdovski edited this page Jul 7, 2023 · 5 revisions

Introduction

As we mentioned before, we need to start our data somewhere. To do this, we have created a Supabase project where our PostgresSQL database will live.

We will be using Prisma, an Object Relational Mapper (ORM), as the layer that connects our Object Oriented program with the Postgres relational database.

Getting Started With Prisma

To get started, we need to install Prisma using

npm install prisma

Next we need to set up our Prisma project using

npx prisma init

This command does two things:

  • creates a new directory called prisma that contains a file called schema.prisma, which contains the Prisma schema with your database connection variables and schema models
  • creates a `.env. file in your root directory, which is used for defining environment variables
    • such as our database connection string

Your .env file should now contain a variable which will hold the connection string to our Postgres database.

DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"

This variable is used by the schema.prisma file when defining the datasource:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

To find your connection string, head over to your project in Supabase.
From here Navigate to Project Settings -> Database and scroll down until you find a section labelled "Connection String:. image Copy and paste this string into your DATABASE_URL variable in your .env file.

DATABASE_URL="postgresql://postgres:[YOUR-PASSWORD]@db.moqluuxezmzfmrpaiosr.supabase.co:5432/postgres"

Remember to replace [YOUR-PASSWORD] with the password you set for your project in the initial setup. Told you it was important to remember it.

We will also need the Prisma Client for us to communicate with the Prisma API. To install, run

npm install @prisma/client

Once completed, created a file called prisma.ts in the /app that looks like so

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export default prisma;
Clone this wiki locally