In Next.js, you can connect to your database directly inside Server Components or Server Actions. You do not need to create separate API routes for internal data fetching.
Best Practices
- Server-only: Ensure your database logic runs only on the server.
- Caching: Database queries in Server Components are not automatically cached (unlike fetch), so consider using
unstable_cacheor theuse cachedirective (Next.js 16) if needed.
Common ORMs include Prisma and Drizzle.