Building Type-Safe APIs with Nuxt 3 and Prisma
Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.
Master Pinia with real-world patterns: store composition, async actions, optimistic updates, and when you don't need a store at all.
Mbeah Essilfie
June 23, 2026 at 06:03 AM
Not every piece of state belongs in Pinia. Use stores for:
For local component state, ref() is enough.
// stores/posts.ts
export const usePostsStore = defineStore('posts', () => {
const posts = ref<Post[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
const publishedPosts = computed(() =>
posts.value.filter(p => p.status === 'published')
)
async function fetchPosts() {
loading.value = true
error.value = null
try {
const { data } = await $fetch('/api/posts')
posts.value = data
} catch (e) {
error.value = (e as Error).message
} finally {
loading.value = false
}
}
async function deletePost(id: string) {
// Optimistic update
const previousPosts = [...posts.value]
posts.value = posts.value.filter(p => p.id !== id)
try {
await $fetch(`/api/posts/${id}`, { method: 'DELETE' })
} catch (e) {
// Rollback on failure
posts.value = previousPosts
throw e
}
}
return { posts, loading, error, publishedPosts, fetchPosts, deletePost }
})
Stores can use other stores:
export const useDashboardStore = defineStore('dashboard', () => {
const postsStore = usePostsStore()
const authStore = useAuthStore()
const myPosts = computed(() =>
postsStore.posts.filter(p => p.authorId === authStore.user?.id)
)
return { myPosts }
})
Pinia handles SSR hydration automatically in Nuxt. But watch out for:
// ❌ This runs on every request in SSR — shared state between users!
const globalState = ref([])
// ✅ State inside defineStore is per-request in SSR
export const useStore = defineStore('safe', () => {
const state = ref([]) // Fresh per request
return { state }
})
useFetch / useAsyncData with cachingreactive()Pinia is powerful, but the simplest solution that works is always the right one.
Fullstack Software Developer
Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.
Go beyond basic utility classes and learn how to build cohesive, maintainable design systems with Tailwind CSS.
Understand the power of Vue 3's Composition API through practical, real-world composable patterns that you can use today.
