MongoDB and Prisma

Table of Contents

Attention: Make sure your server is running and connected to your MongoDB Atlas cluster before interacting with the frontend.
follow the instructions in the lesson 01 to start the server.

Lesson 01: Setting up MongoDB and Prisma

We will be using MongoDB Atlas, a cloud-based database service.

Links

Procedures

MongoDB & Prisma setup

  1. Go to the MongoDB website and create/login to your account.
  2. Create a new cluster in MongoDB Atlas.
  3. Set your cluster configuration (e.g., cloud provider, region, cluster tier).
  4. Wait for the cluster to be deployed.
  5. Create the user and password for your database.
  6.   //The database will be created with Prisma through schema.prisma
  7. Go to the Prisma documentation and follow the MongoDB Quickstart guide.
  8.   //Right now Prisma v7 doesn't support MongoDB, we will use Prisma v6.19
  9. install required dependencies
  10. Creating prisma schema
  11. In the schema.prisma file:
  12. In VS Code, install the Prisma extensions:
  13. Use the command: To generate the Prisma Client based on your schema.
  14. Set the default version of prisma to 6.19.0 by using the command CTRL+SHIFT+P and selecting Pin the current workspace to Prisma 6
  15. If the VS Code Still doesn't recognize the Prisma Client, use the command CTRL+SHIFT+P and selecting Reload Window
  16. In the server.js add the lines to import and initialize the Prisma Client:
  17. On MongoDB Atlas, create a new database and collection.
  18. In your database, click in Connect and get the connection string.
  19. Modify the .env file with your MongoDB connection string.
    Then before the querry string '?=', after the '/', change to /Users?
  20.   //the /Users? set the default database to Users

Lesson 02: Defining Data Models

In this lesson, we will define our data models using the Prisma schema file. We will create a simple User model with fields for id, email, name, and age.

Procedures

  1. Open the schema.prisma file in the prisma folder.
  2. Define a new model called User with the following fields:
  3. After defining the model, run the command: This command will push your Prisma schema changes to your MongoDB database, creating the necessary collections and indexes based on your model definitions.

Model parameters

Model typing

In Prisma, you can define the types of your model fields using the following data types:

Lesson 03: CRUD Operations with Prisma

CRUD (Create, Read, Update, Delete) operations using the Prisma Client.

Procedures

  1. Create: To create a new user, we can use the prisma.user.create() method. For example:
    const newUser = await prisma.user.create({
        data: {
            email: 'example@example.com',
            name: 'John Doe',
            age: 30
        }
    });
    
  2. Read: To read users, we can use the prisma.user.findMany() method. For example:
    const users = await prisma.user.findMany();
    
  3. Update: To update a user, we can use the prisma.user.update() method. For example:
    const updatedUser = await prisma.user.update({
        where: { id: 'userId' },
        data: { name: 'Jane Doe' }
    });
    
  4. Delete: To delete a user, we can use the prisma.user.delete() method. For example:
    const deletedUser = await prisma.user.delete({
        where: { id: 'userId' }
    });