How to create prisma schema for user,cart and order models
To model a User
with Orders
and Carts
in Prisma, you would typically use relational fields to link these models together. Here's a step-by-step guide to creating the schema for this relationship:
Step 1: Define the Schema
In your Prisma schema, define the User
, Order
, and Cart
models, and establish the relationships between them.
// schema.prisma
model User {
id Int @id @default(autoincrement())
name String
email String @unique
orders Order[] // One-to-many relation with Order
carts Cart[] // One-to-many relation with Cart
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Order {
id Int @id @default(autoincrement())
userId Int // Foreign key to User
user User @relation(fields: [userId], references: [id])
items Json // Assuming Order contains a JSON field for items
total Float
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Cart {
id Int @id @default(autoincrement())
userId Int // Foreign key to User
user User @relation(fields: [userId], references: [id])
items Json // Assuming Cart contains a JSON field for items
total Float
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Step 2: Run Migrations
After defining your schema, you need to run the migrations to update your database:
npx prisma migrate dev --name add_user_orders_carts
Step 3: Using the Relationships in Code
You can now interact with the User
, Order
, and Cart
models in your application code.
3.1. Creating a User with Orders and Carts
Here's how you can create a user and add orders and carts:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
const user = await prisma.user.create({
data: {
name: 'John Doe',
email: '[email protected]',
orders: {
create: [
{
items: [
{ name: 'Item 1', price: 100, quantity: 2 },
{ name: 'Item 2', price: 200, quantity: 1 }
],
total: 400,
}
]
},
carts: {
create: [
{
items: [
{ name: 'Item A', price: 50, quantity: 3 },
{ name: 'Item B', price: 150, quantity: 1 }
],
total: 300,
}
]
}
}
});
console.log(user);
}
main()
.catch(e => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
3.2. Querying a User with Orders and Carts
You can also query a user and include their orders and carts:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
const user = await prisma.user.findUnique({
where: { email: '[email protected]' },
include: {
orders: true,
carts: true
}
});
console.log(user);
}
main()
.catch(e => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
3.3. Updating Orders and Carts
To update an order or cart, you can use the following code:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
const updatedOrder = await prisma.order.update({
where: { id: 1 },
data: {
items: [
{ name: 'Item 1 Updated', price: 120, quantity: 2 },
{ name: 'Item 2 Updated', price: 220, quantity: 1 }
],
total: 460,
}
});
const updatedCart = await prisma.cart.update({
where: { id: 1 },
data: {
items: [
{ name: 'Item A Updated', price: 55, quantity: 3 },
{ name: 'Item B Updated', price: 160, quantity: 1 }
],
total: 325,
}
});
console.log(updatedOrder);
console.log(updatedCart);
}
main()
.catch(e => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
Published on: Aug 02, 2024, 02:25 AM