Converting Mongoose schema to Prisma Mysql schema
1
vote
0
answers
583
views
I am trying to convert the following Mongoose schema to a Prisma MySql Schema. Not sure if I am doing it right. Here is the Mongoose Schema:
import mongoose from 'mongoose';
const { ObjectId } = mongoose.Schema;
const CartSchema = new mongoose.Schema(
{
products: [
{
product: {
type: ObjectId,
ref: 'Product',
},
count: Number,
color: String,
price: Number,
},
],
cartTotal: Number,
totalAfterDiscount: Number,
orderedBy: { type: ObjectId, ref: 'User' },
},
{ timestamps: true }
);
export mongoose.models.Cart || mongoose.model('Cart', CartSchema);
const CategorySchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: 'Name is required',
minlength: [2, 'Too short'],
maxlength: [32, 'Too long'],
},
slug: {
type: String,
unique: true,
lowercase: true,
index: true,
},
},
{ timestamps: true }
);
export mongoose.models.Category ||
mongoose.model('Category', CategorySchema);
const CouponSchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
unique: true,
uppercase: true,
required: 'Name is required',
minlength: [6, 'Too short'],
maxlength: [12, 'Too long'],
},
expiry: {
type: Date,
required: true,
},
discount: {
type: Number,
requred: true,
},
},
{ timestamps: true }
);
export mongoose.models.Coupon || mongoose.model('Coupon', CouponSchema);
const OrderSchema = new mongoose.Schema(
{
products: [
{
product: {
type: ObjectId,
ref: 'Product',
},
count: Number,
color: String,
},
],
paymentIntent: {},
orderStatus: {
type: String,
default: 'Not Processed',
enum: [
'Not Processed',
'Cash On Delivery',
'processing',
'Dispatched',
'Cancelled',
'Completed',
],
},
orderedBy: { type: ObjectId, ref: 'User' },
},
{ timestamps: true }
);
export mongoose.models.Order || mongoose.model('Order', OrderSchema);
const ProductSchema = new mongoose.Schema(
{
title: {
type: String,
trim: true,
required: true,
maxlength: 32,
text: true,
},
slug: {
type: String,
unique: true,
lowercase: true,
index: true,
},
description: {
type: String,
required: true,
maxlength: 2000,
text: true,
},
price: {
type: Number,
required: true,
trim: true,
maxlength: 32,
},
category: {
type: ObjectId,
ref: 'Category',
},
subcategories: [
{
type: ObjectId,
ref: 'SubCategory',
},
],
quantity: Number,
sold: {
type: Number,
default: 0,
},
images: {
type: Array,
},
shipping: {
type: String,
enum: ['Yes', 'No'],
},
color: {
type: String,
enum: ['Black', 'Brown', 'Silver', 'White', 'Blue'],
},
brand: {
type: String,
enum: ['Apple', 'Samsung', 'Microsoft', 'Lenovo', 'ASUS'],
},
ratings: [
{
star: Number,
postedBy: { type: ObjectId, ref: 'User' },
},
],
},
{
timestamps: true,
// toJSON: { virtuals: true },
// toObject: { virtuals: true },
}
);
export mongoose.models.Product ||
mongoose.model('Product', ProductSchema);
const SubCategorySchema = new mongoose.Schema(
{
name: {
type: String,
trim: true,
required: 'Name is required',
minlength: [2, 'Too short'],
maxlength: [32, 'Too long'],
},
slug: {
type: String,
unique: true,
lowercase: true,
index: true,
},
parent: { type: ObjectId, ref: 'Category', required: true },
},
{ timestamps: true }
);
export mongoose.models.SubCategory ||
mongoose.model('SubCategory', SubCategorySchema);
const UserSchema = new mongoose.Schema(
{
name: String,
email: {
type: String,
required: true,
index: true,
},
role: {
type: String,
default: 'subscriber',
},
cart: {
type: Array,
default: [],
},
address: String,
wishlist: [{ type: ObjectId, ref: 'Product' }],
},
{ timestamps: true }
);
export mongoose.models.User || mongoose.model('User', UserSchema);
Here is what I have got as a result
datasource db {
provider = "mysql"
url = "mysql://user:password@localhost:3306/database_name"
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id @default(cuid())
name String?
email String @unique
emailVerified DateTime?
cart Cart?
address String?
wishlist Product[]
image String?
role Role @default(USER)
ratings Rating[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt()
}
model Cart {
id String @id @default(cuid())
products Product[]
cartTotal Float?
totalAfterDiscount Float?
orderedBy User? @relation(fields: [userId], references: [id])
userId String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Category {
id String @id @default(cuid())
name String
slug String @unique
products Product[]
subcategories SubCategory[]
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
}
model Product {
id String @id @default(cuid())
title String
slug String @unique
description String
price Float
quantity Int
sold Int @default(0)
shipping Shipping
color Color
brand Brand
category Category @relation(fields: [categoryId], references: [id])
categoryId String
subcategories SubCategory[]
carts Cart[]
users User[]
images Image[]
ratings Rating[]
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@index([categoryId])
}
model SubCategory {
id String @id @default(cuid())
name String
slug String @unique
category Category @relation(fields: [categoryId], references: [id])
categoryId String
products Product[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([categoryId])
}
enum Role {
USER
ADMIN
}
model Image {
id String @id @default(uuid())
image String
product Product @relation(fields: [productId], references: [id])
productId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([productId])
}
model Rating {
id String @id @default(cuid())
star Int
postedBy User? @relation(fields: [userId], references: [id])
userId String?
product Product? @relation(fields: [productId], references: [id])
productId String?
created DateTime @default(now())
updated DateTime @updatedAt
@@index([userId])
@@index([productId])
}
enum Shipping {
Yes
No
}
enum Color {
Black
Brown
Silver
White
Blue
}
enum Brand {
Apple
Samsung
Microsoft
Lenovo
ASUS
}
Based on the given Mongoose schemas, the relationships between tables can be inferred as follows:
User and Cart have a one-to-one relationship, where each Cart is associated with a single User. This is indicated by the orderedBy field in the CartSchema, which references the User model.
Category and Product have a one-to-many relationship, where each Category can have multiple Products. This is indicated by the category field in the ProductSchema, which references the Category model.
SubCategory and Product have a many-to-many relationship, where each Product can belong to multiple SubCategorys and each SubCategory can have multiple Products. This is indicated by the subcategories field in the ProductSchema, which is an array of references to the SubCategory model.
User and Product have a many-to-many relationship, where each User can have multiple Products in their wishlist and each Product can be in multiple users' wishlists. This is indicated by the wishlist field in the UserSchema, which is an array of references to the Product model.
Product and Cart have a many-to-many relationship, where each Product can be in multiple Carts and each Cart can have multiple Products. This is indicated by the products field in the CartSchema, which is an array of objects that include a reference to the Product model.
Let me know if it makes sense.
Asked by John John
(111 rep)
Apr 18, 2023, 11:11 PM
Last activity: Apr 19, 2023, 06:07 PM
Last activity: Apr 19, 2023, 06:07 PM