vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.62k stars 26.25k forks source link

example: update server action + mongo example #67263

Open bysxx opened 3 days ago

bysxx commented 3 days ago

For Contributors

Adding or Updating Examples

What?

This PR adds a new example demonstrating the integration of Next.js 14 Server Actions with MongoDB. The example showcases how to perform CRUD operations using Server Actions and store data in a MongoDB database.

Why?

Server Actions are a powerful new feature in Next.js 14, and many developers are looking for guidance on how to integrate them with popular databases like MongoDB. This example provides a clear, practical demonstration of this integration, helping developers understand how to leverage Server Actions for database operations.

How?

The example includes:

  1. Setup of a MongoDB connection using mongoose
  2. Implementation of Server Actions for creating, reading, updating, and deleting data
  3. A simple UI demonstrating how to trigger these actions from the client-side
  4. Error handling and data validation
  5. TypeScript support for better type safety

The example follows Next.js best practices and demonstrates efficient use of Server Actions in conjunction with MongoDB operations.