|
| 1 | +--- |
| 2 | +title: Plasmic |
| 3 | +description: Build websites and apps visually with Prisma and Plasmic |
| 4 | +url: /guides/integrations/plasmic |
| 5 | +metaTitle: How to use Prisma without code in Plasmic |
| 6 | +metaDescription: Set up the Plasmic Prisma integration, configure queries from Plasmic Studio, and connect it to your UI |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +[Plasmic](https://www.plasmic.app/) is a visual builder for React websites and applications. In this guide, you will connect Plasmic to Prisma using the [starter blog template](https://github.com/plasmicapp/plasmic-prisma-starter) and learn how to connect your queries to the UI created in Plasmic Studio. |
| 12 | + |
| 13 | +Your Prisma connection credentials remain in the application environment and are not sent to Plasmic. All queries safely go through the Next.js server functions hosted on your end. You can configure queries using either a server function or a client component, which will be supplied with the Prisma models, operations, filters, and all options required to build the queries without looking up the schema in Prisma Studio too often. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- [Node.js 20 or later](https://nodejs.org/en/download/) |
| 18 | +- [pnpm](https://pnpm.io/installation) |
| 19 | +- [Prisma account](https://console.prisma.io/) |
| 20 | +- [Plasmic account](https://studio.plasmic.app/) |
| 21 | +- [Git](https://git-scm.com/downloads) |
| 22 | + |
| 23 | +## 1. Clone the starter |
| 24 | + |
| 25 | +Clone the starter so you have the registered Plasmic data query, Prisma schema, seed data, and example Next.js routes used in this guide. |
| 26 | + |
| 27 | +```bash title="Terminal" |
| 28 | +git clone https://github.com/plasmicapp/plasmic-prisma-starter.git |
| 29 | +cd plasmic-prisma-starter |
| 30 | +pnpm install |
| 31 | +``` |
| 32 | + |
| 33 | +The project includes `prisma/schema.prisma`, `functions/prismaQuery.tsx`, and the Plasmic registration in `plasmic-init.ts`. |
| 34 | + |
| 35 | +## 2. Create a Prisma database |
| 36 | + |
| 37 | +Provision a Prisma project for the starter. The command signs you in to the Prisma Console, asks for a region and project name, and prints a connection string. |
| 38 | + |
| 39 | +```bash title="Terminal" |
| 40 | +pnpm exec prisma init --db |
| 41 | +``` |
| 42 | + |
| 43 | +Copy the `postgres://...` connection string from the command output. You will add it to the starter in the next step. |
| 44 | + |
| 45 | +## 3. Configure the environment and seed the database |
| 46 | + |
| 47 | +Create the local environment file from the example so the application can connect to Prisma and initialize Auth.js. |
| 48 | + |
| 49 | +```bash title="Terminal" |
| 50 | +cp .env.example .env |
| 51 | +pnpm dlx auth secret |
| 52 | +``` |
| 53 | + |
| 54 | +Open `.env`, replace the placeholder `DATABASE_URL` with the connection string from the previous step, and confirm that `AUTH_SECRET` contains the generated value: |
| 55 | + |
| 56 | +```text title=".env" |
| 57 | +DATABASE_URL="postgres://USER:PASSWORD@HOST:5432/postgres?sslmode=require" |
| 58 | +AUTH_SECRET="YOUR_GENERATED_SECRET" |
| 59 | +``` |
| 60 | + |
| 61 | +Apply the included schema, generate Prisma Client, and load the sample roles, users, and posts: |
| 62 | + |
| 63 | +```bash title="Terminal" |
| 64 | +pnpm exec prisma migrate dev --name init |
| 65 | +pnpm exec prisma generate |
| 66 | +pnpm exec prisma db seed |
| 67 | +``` |
| 68 | + |
| 69 | +The final command should print: |
| 70 | + |
| 71 | +```text no-copy title="Expected output" |
| 72 | +Seeding completed. |
| 73 | +``` |
| 74 | + |
| 75 | +If `prisma migrate dev` reports error `P3005` because the database schema is not empty, follow the [baselining workflow](/orm/prisma-migrate/workflows/baselining) before applying migrations. |
| 76 | + |
| 77 | +## 4. Connect your Plasmic project |
| 78 | + |
| 79 | +Create an empty project in [Plasmic Studio](https://studio.plasmic.app/). Open the **Code** panel to find the project ID and public project token, then add both values to `.env`: |
| 80 | + |
| 81 | +```text title=".env" |
| 82 | +DATABASE_URL="postgres://USER:PASSWORD@HOST:5432/postgres?sslmode=require" |
| 83 | +AUTH_SECRET="YOUR_GENERATED_SECRET" |
| 84 | +NEXT_PUBLIC_PLASMIC_PROJECT_ID="YOUR_PROJECT_ID" |
| 85 | +NEXT_PUBLIC_PLASMIC_PROJECT_TOKEN="YOUR_PUBLIC_PROJECT_TOKEN" |
| 86 | +``` |
| 87 | + |
| 88 | +The starter reads these variables in `plasmic-init.ts`. Its loader has `preview: true` so Studio can render unpublished changes during development. Set `preview` to `false` before using the application in production. |
| 89 | + |
| 90 | +## 5. Run the app and set the app host |
| 91 | + |
| 92 | +Start the Next.js development server so Plasmic Studio can load the registered data query from your application. |
| 93 | + |
| 94 | +```bash title="Terminal" |
| 95 | +pnpm dev |
| 96 | +``` |
| 97 | + |
| 98 | +In browser, verify that the Plasmic host page is available by visiting the following url: |
| 99 | + |
| 100 | +```bash title="Terminal" |
| 101 | +http://localhost:3000/plasmic-host |
| 102 | +``` |
| 103 | + |
| 104 | +Follow Plasmic's [app host setup guide](https://docs.plasmic.app/learn/app-hosting/#3-set-your-plasmic-project-to-use-your-app-host) and set your project's app host to: |
| 105 | + |
| 106 | +```text no-copy title="App host URL" |
| 107 | +http://localhost:3000/plasmic-host |
| 108 | +``` |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +Studio reloads after you save the host. The **Prisma Query** function is now available in the page data panel in "Data Queries". |
| 113 | + |
| 114 | +## 6. Query published posts in Plasmic Studio |
| 115 | + |
| 116 | +Create or open a page that will display the seeded posts. Add a page data query named `Get Recent Posts` with these settings: |
| 117 | + |
| 118 | +- **Function:** `Prisma Query` (`prismaQuery`) |
| 119 | +- **Table:** `Post` |
| 120 | +- **Operation:** `findMany` |
| 121 | +- **Where:** `published` equals `true` |
| 122 | +- **Include Relations:** `author` |
| 123 | +- **Order By Field:** `createdAt` |
| 124 | +- **Order Direction:** `desc` |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +Hit "Execute" to preview the query in Studio. The result under `$q.getRecentPosts?.data` should be an array of published posts with their authors. |
| 129 | + |
| 130 | +Add a container to the page and repeat it over `$q.getRecentPosts?.data`. Inside the repeated item, bind text elements to the current post's `title`, `content`, and `author.name` fields. |
| 131 | + |
| 132 | +:::note |
| 133 | + |
| 134 | +The starter checks every operation in `functions/prismaQuery.tsx` against the current user's role before it reaches Prisma Client. The seeded `guest` role can read data, so this `findMany` query works without signing in. Keep authorization checks in application code when you adapt the starter. |
| 135 | + |
| 136 | +::: |
| 137 | + |
| 138 | +## Verify the integration |
| 139 | + |
| 140 | +Preview the page in Plasmic Studio. You should see the published sample posts created by `prisma db seed`, ordered with the newest post first. Change a post in [Prisma Studio](/studio) and execute `Get Recent Posts` again to confirm that the page reads from your Prisma database. |
| 141 | + |
| 142 | +## Next steps |
| 143 | + |
| 144 | +- Read the detailed [Using Prisma with Plasmic](https://www.plasmic.app/blog/prisma-plasmic-integration) article for dynamic routes, write operations, authentication, and permissions. |
| 145 | +- Review the complete [Plasmic Prisma starter](https://github.com/plasmicapp/plasmic-prisma-starter) implementation. |
| 146 | +- Learn how to [filter and sort with Prisma Client](/orm/prisma-client/queries/filtering-and-sorting). |
0 commit comments