This is the first article in a series dedicated to showing you how to use Prisma to manage your databases in your Nuxt 3 app.
Get notified when we release new tutorials, lessons, and other expert Nuxt content.
Trying to manage database schemas alongside your Nuxt app types can be a challenge.
But with Prisma, most of these problems go away.
It handles all of the boilerplate and coordination, so you just write one single schema that’s used in your database and in your TypeScript app.
This is the first article in a series dedicated to showing you how to use Prisma in your Nuxt 3 app. Here is where we’re going:
In this article, we’ll lay the groundwork for working with Prisma and Supabase. We’ll also install and configure the Prisma VS Code extension, because it makes developing with Prisma so much nicer.
Let’s get started!
The details for setting up Prisma and Supabase together have changed a bit recently, so please go here to find the most up-to-date instructions if this article isn’t working for you. I’ll try to keep this article updated though.
Here are the steps you’ll need:
DATABASE_URL
environment variable to your .env
file. Set the value to be the connection string with the password. It’ll look something like this:DATABASE_URL="postgresql://postgres:H1ztIK5fZu1JiTll@db.slwsodtadobbvhetphzl.supabase.co:5432/postgres"
If you’re running your app in a serverless environment like Netlify or Vercel, you’ll need to enable connection pooling.
Every time a serverless function is run, it will create an entirely new connection to your Supabase database. This is not great, because it creates a lot of unnecessary overhead between your app and database.
Instead, we can enable connection pooling with PgBouncer, which will pool all of these connections into a single connection to our database. In order to enable this, we need to append ?pgbouncer=true
to our connection URL.
But, when we’re updating our database schema during migrations we need to have a direct connection.
To accomplish this, we’ll use two environment variables — DATABASE_URL
and DIRECT_DATABASE_URL
.
We’ll copy DATABASE_URL
to DIRECT_DATABASE_URL
which doesn’t use connection pooling. Then, we’ll create the DATABASE_URL
by appending ?pgbouncer=true
to enable connection pooling:
DIRECT_DATABASE_URL="postgresql://postgres:H1ztIK5fZu1JiTll@db.slwsodtadobbvhetphzl.supabase.co:5432/postgres"
DATABASE_URL="postgresql://postgres:H1ztIK5fZu1JiTll@db.slwsodtadobbvhetphzl.supabase.co:5432/postgres?pgbouncer=true"
Now that we have all of our environment variables set up, we need to make our development experience nicer by installing the official Prisma extension for VS Code.
This gives us a few really nice features:
///
and get intellisense wherever we use these types in our codeOf course, there are many other features, but these are the most useful in my opinion.
We have a tiny bit of config to do here to make these two extensions play nicely.
In one of your settings.json
files (user or workspace), add the following:
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
},
You may also want to enable formatOnSave
as well:
"editor.formatOnSave": true
Now we have all the boring configuration set up, and we’re ready to start actually doing something with Prisma.
We’ll get to that in the next part of this series, which is creating our Prisma schema. This schema is used for both our database in Supabase as well as our TypeScript code in our Nuxt app, keeping things all in one place!
Next Article: Creating the Prisma Schema