diff --git a/docs/guides/development/integrations/databases/prisma-postgres.mdx b/docs/guides/development/integrations/databases/prisma-postgres.mdx new file mode 100644 index 0000000000..d663119e84 --- /dev/null +++ b/docs/guides/development/integrations/databases/prisma-postgres.mdx @@ -0,0 +1,295 @@ +--- +title: Integrate Prisma Postgres with Clerk +description: Learn how to integrate Clerk into your Prisma Postgres application. +--- + + + +Integrating Prisma Postgres with Clerk gives you the benefits of using a Prisma Postgres database while leveraging Clerk's authentication, prebuilt components, and webhooks. This guide will show you how to create a simple blog application that allows users to create and read posts using Prisma Postgres and Clerk. This guide uses Next.js App Router but the same principles can be applied to other SDKs. + + + ## Install Prisma + + Run the following command to install Prisma: + + ```sh {{ filename: 'terminal' }} + npm install prisma tsx --save-dev + ``` + + ## Initialize Prisma + + Run the following command to initialize Prisma: + + ```sh {{ filename: 'terminal' }} + npx prisma init --output ../app/generated/prisma + ``` + + This will: + + - Create a new `prisma/` directory in your project, with a `schema.prisma` file inside of it. The `schema.prisma` file is where you will define your database models. + - Create a `prisma.config.ts` file in the root of your project. + - Update your `.env` file to include a `DATABASE_URL` environment variable, which is used to store your database connection string for your Prisma Postgres database. + + ## Update the Prisma configuration + + Inside the `prisma.config.ts` file, add `import "dotenv/config";` so that it can load the `DATABASE_URL` environment variable from your `.env` file. + + ```ts {{ filename: 'prisma.config.ts', mark: [1] }} + import 'dotenv/config' + import { defineConfig, env } from 'prisma/config' + + export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + engine: 'classic', + datasource: { + url: env('DATABASE_URL'), + }, + }) + ``` + + ## Update the database schema + + In the `prisma/schema.prisma` file, update the schema to include a new model called `Post` with the columns `id`, `title`, `content`, `published`, and `authorId`. The `id` column will be used as the post's primary key, and the `authorId` column will be used to store the user's Clerk ID. + + ```prisma {{ filename: 'prisma/schema.prisma', mark: [[11, 17]] }} + generator client { + provider = "prisma-client" + output = "../app/generated/prisma" + } + + datasource db { + provider = "postgresql" + url = env("DATABASE_URL") + } + + model Post { + id Int @id @default(autoincrement()) + title String + content String? + published Boolean @default(false) + authorId String + } + ``` + + ## Push the schema to the database + + Run the following command to push the updated schema to the database: + + ```sh {{ filename: 'terminal' }} + npx prisma db push + ``` + + ## Create a reusable Prisma Client instance + + At the root of your project, create a folder called `lib`. Inside of it, create a new file called `prisma.ts` and add the following code to it. This will set up a single Prisma Client instance, which is used to interact with your database, and bind it to the global object so that only one instance of the client is created in your application. This helps resolve issues with hot reloading that can occur when using Prisma with Next.js in development mode. + + ```typescript {{ filename: 'lib/prisma.ts' }} + import { PrismaClient } from '@prisma/client' + + const prisma = new PrismaClient() + + const globalForPrisma = global as unknown as { prisma: typeof prisma } + + if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma + + export default prisma + ``` + + ## Create the UI for the homepage + + Now that all of the set up is complete, it's time to start building out your app. + + Replace the contents of `app/page.tsx` with the following code. This page fetches all posts from your database and displays them on the homepage, showing the title and author ID for each post. It uses the [`prisma.post.findMany()`](https://www.prisma.io/docs/orm/reference/prisma-client-reference?utm_source=docs#findmany) method, which is a Prisma Client method that retrieves all records from the database. + + ```tsx {{ filename: 'app/page.tsx' }} + import Link from 'next/link' + import prisma from '@/lib/prisma' + + export default async function Page() { + const posts = await prisma.post.findMany() // Query the `Post` model for all posts + + return ( +
+

Posts

+ +
+ {posts.map((post) => ( + + {post.title} + by {post.authorId} + + ))} +
+ + + Create New Post + +
+ ) + } + ``` + + ## Create a new post + + Create a new file called `app/posts/create/page.tsx` and add the following code to it. This page allows users to create new posts. It uses Clerk's [`auth`](/docs/reference/backend/types/auth-object) object to get the user's ID. + + - If there is no user ID, the user is not signed in, so a sign in button is displayed. + - If the user is signed in, the "Create New Post" form is displayed. When the form is submitted, the `createPost()` function is called. This function creates a new post in the database using the [`prisma.post.create()`](https://www.prisma.io/docs/orm/reference/prisma-client-reference?utm_source=docs#create) method, which is a Prisma Client method that creates a new record in the database. + + ```tsx {{ filename: 'app/posts/create/page.tsx' }} + import Form from 'next/form' + import prisma from '@/lib/prisma' + import { redirect } from 'next/navigation' + import { SignInButton, useAuth } from '@clerk/nextjs' + import { revalidatePath } from 'next/cache' + import { auth } from '@clerk/nextjs/server' + + export default async function NewPost() { + // The `Auth` object gives you access to properties like `userId` + // Accessing the `Auth` object differs depending on the SDK you're using + // https://clerk.com/docs/reference/backend/types/auth-object#how-to-access-the-auth-object + const { userId } = await auth() + + // Protect this page from unauthenticated users + if (!userId) { + return ( +
+

You must be signed in to create a post.

+ + + +
+ ) + } + + async function createPost(formData: FormData) { + 'use server' + + // Type check + if (!userId) return + + const title = formData.get('title') as string + const content = formData.get('content') as string + + await prisma.post.create({ + data: { + title, + content, + authorId: userId, + }, + }) + + revalidatePath('/') + redirect('/') + } + + return ( +
+

Create New Post

+
+
+ + +
+
+ +