Direct Database Access from Server Components

Because Server Components execute on the server, they can call database libraries directly. This can be more efficient than calling your own HTTP API from the server.

For example, a dashboard page can call getOrders() which uses Prisma, Mongoose, Drizzle, or another database abstraction. The browser receives rendered results rather than database credentials or internal query logic.

However, database access should be placed behind a clean data-access layer so components do not become collections of raw queries.

Data Access Layer
import 'server-only';

export async function getOrders(userId: string) {
  return db.order.findMany({
    where: { userId },
    orderBy: { createdAt: 'desc' }
  });
}
💡 A dedicated server-only data-access function keeps database details away from UI components.
Key Takeaways
  • Server Components can access databases directly.
  • Keep database logic in a dedicated service or repository layer.
  • Never expose database credentials to the browser.