apps/blog/content/blog/introducing-prisma-nuxt/index.mdx
The Prisma Nuxt module is now available, offering a streamlined way to integrate Prisma ORM into Nuxt applications. This module extends Nuxt, a powerful framework for building full-stack Vue.js apps, with functionalities provided by Prisma ORM such as type-safe database interactions, automated migrations, and Prisma Studio for data management.
The Prisma Nuxt module provides:
usePrismaClient() composable.prisma for use in API routes.@prisma/nuxt moduleLet’s create a new Nuxt app and add the @prisma/nuxt module. This module will set up Prisma ORM for us and also launch Prisma Studio, allowing us to manipulate data in our SQLite database directly from the Nuxt Devtools:
Begin with a new Nuxt project:
npx nuxi@latest init test-nuxt-app
Navigate to your project directory and install @prisma/nuxt:
cd test-nuxt-app
npm install @prisma/nuxt
Add @prisma/nuxt to your nuxt.config.ts:
export default defineNuxtConfig({
modules: ["@prisma/nuxt"],
devtools: { enabled: true },
});
Start the development server:
npm run dev
This will:
User and Post) in the Prisma SchemaOpen localhost:3000 in your browser and follow terminal instructions to open Nuxt Devtools and Prisma Studio:
Use Prisma Studio to add a user to the database:
We’ve seen how easy it is to start with Prisma ORM in Nuxt, thanks to the module. Now proceed to the next section to see how to query your data from within the app using the usePrismaClient composable.
usePrismaClient composable to create a server componentNow we’ll create a server component that retrieves the first user from the database using the usePrismaClient composable in a server component:
Configure nuxt.config.ts to automate the setup process, so we aren't prompted to start Prisma Studio every time the development server reloads:
export default defineNuxtConfig({
modules: ["@prisma/nuxt"],
prisma: {
autoSetupPrisma: true,
},
devtools: { enabled: true },
});
Create a new FirstUser.server.vue component in the components folder, and import usePrismaClient in the script:
<script setup>
import { usePrismaClient } from '@prisma/nuxt'
const prisma = usePrismaClient();
const firstUserFound = await prisma.user.findFirst();
</script>
<template>
<div>
<p>{{ firstUserFound ?? "" }}</p>
</div>
</template>
Integrate the server component into app.vue:
<template>
<h1>Welcome to the Nuxt Prisma Demo</h1>
<NuxtIsland name="FirstUser"> </NuxtIsland>
</template>
View the user details queried from the database in your browser:
And that’s how easily you can get started using Prisma ORM in your Nuxt app using the Nuxt Prisma module.
For further details on the Nuxt Prisma module's capabilities, visit our documentation. This is an early release, and we're actively working on improvements based on community feedback. We welcome your contributions and suggestions. If you find this project useful, please star it on GitHub!
We have an active Discord community where you can ask for help and share your feedback, stay updated on our changelog, and follow us on X for the latest news.