Course

Connecting Our Database to Prisma

So we've initialized Prisma. And we've created our Database on Supabase. How do we connect them together so they can communicate?

First we'll open up our file in the directory.

At the top, we'll find two sections already added to our

  • generator client
  • datasource db

These are for the of our connection and prisma client.

Generator client

schema.prisma
generator client {
  provider = "prisma-client-js"
}

These lines tell prisma to build the whenever we run . This client is what we'll import into our files to interact with the database. Prisma is great, because it will read our database schema and create a we can use- meaning we get all the advantages of typescript. We'll get autocomplete, type detection, and early alerts for any errors we may make when writing queries.

When building applications with javascript and Prisma, you'll never really need to remove or change this. You can, however, add more generators to this file in the future. Generators exist to interpret our schema and some code from it. You can have generators make diagrams for your schema, or even generate Zod types for forms that interact with your database. This is a little beyond an intro to databases, though. You can read more about generators here. For now- just know that this line is what causes our client with all of our types to be created by Prisma.

Datasource db

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

These lines tell prisma how to connect to our database. Remember the URLs we added to our in the previous lesson? This is where we use them.

Let's add the to this block of code so that Prisma can connect to our database without error.

schema.prisma
datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

The is what Prisma will use to connect to the database for queries like inserts, updates, and deletes. The is what Prisma will use to connect to the database when it needs to update the schema like adding or removing tables or rules.

In the next lesson, we'll create a quick demo table and show how to push this table to our database and view the information inside our database from Prisma.

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Introduction To Prisma Models