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.
We will be using MongoDB Atlas, a cloud-based database service.
npm install express
If you don't have Express installed, run this command to install it.
npm install prisma@6.19.0 --save-devnpm install @prisma/client@6.19.0npx prisma init --datasource-provider mongodb --output ../generated/prismaprisma.configs.ts because we are not using the v7schema.prisma file:
prisma-client to prisma-client-js.postgresql to mongodb.Prisma extension by PrismaPrisma insider extension by Prismanpx prisma generateCTRL+SHIFT+P and selecting Pin the current workspace to Prisma 6CTRL+SHIFT+P and selecting Reload Windowserver.js add the lines to import and initialize the Prisma Client:
const { PrismaClient } = require('@prisma/client');const prisma = new PrismaClient();.env file with your MongoDB connection string.
'?=', after the '/', change to /Users?/Users? set the default database to UsersIn 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.
schema.prisma file in the prisma folder.User with the following fields:
id: A unique identifier for each user (String, @id, @default(auto()), @map("_id"), @db.ObjectId)email: The user's email address (String, @unique)name: The user's name (String, optional)age: The user's age (Int, optional)npx prisma db push@id: Marks the field as the primary key.@default(auto()): Automatically generates a unique value for the field (useful for IDs).@map("_id"): Maps the field to a different name in the database (in this case, MongoDB uses "_id" for primary keys).@db.ObjectId: Specifies that the field should be treated as a MongoDB ObjectId.@unique: Ensures that all values in this field are unique across the collection.@optional: Marks the field as optional, allowing it to be null.@relation: Defines a relationship between models.In Prisma, you can define the types of your model fields using the following data types:
String: A sequence of characters.Int: A 32-bit integer.Float: A floating-point number.Boolean: A true/false value.DateTime: A date and time value.Json: A JSON object.Bytes: Binary data.Decimal: A decimal number (requires the Prisma Decimal.js library).Enum: A set of predefined values (you can define your own enums in the schema).@relation: Defines a relationship between models. For example, if you have a Post model that has a relation to the User model, you can define it like this:
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
authorId String
author User @relation(fields: [authorId], references: [id])
}
User can have many Posts, you can define it like this:
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
posts Post[]
}
npx prisma db pushnpx prisma studioCRUD (Create, Read, Update, Delete) operations using the Prisma Client.
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
}
});
Read: To read users, we can use the prisma.user.findMany() method. For example:
const users = await prisma.user.findMany();
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' }
});
Delete: To delete a user, we can use the prisma.user.delete() method. For example:
const deletedUser = await prisma.user.delete({
where: { id: 'userId' }
});