n
r
@Neo Lambada πŸ‘‹ Is this schema final or you can make changes to this? Because in Prisma you can directly link Users to the Products that they have saved like this:
Copy code
model User {
  id            Int       @id @default(autoincrement())
  name          String
  avatar        String?
  savedProducts Product[]
}

model Product {
  id    Int    @id @default(autoincrement())
  name  String
  price Int
  users User[]
}
This would then take just a single Prisma call to fetch the users who have saved/bookmarked this product.
n
no. the schema is not final.
the underline tables are correct right. i just have to change the schema right
im sorry, im just learning prisma
r
Yes, you can simply use the above schema and then query for the bookmarked products by the user as follows:
Copy code
let userId = 1
  let products = await prisma.product.findMany({
    where: { stock: { gte: 1 } },
    orderBy: { id: 'desc' },
    include: { users: { where: { id: userId } } },
  })
If the user exists in the product
include
, it means that it is bookmarked and if the user doesn’t exist, it means that it is not bookmarked
πŸ‘ 1
This will be made easier once we have
exists
implemented, so it would be great if you could add a πŸ‘ to this request πŸ™‚
n
done. πŸ™‚
πŸ™Œ 1
Copy code
model User {
  id        Int    @id @default(autoincrement())
  nidNumber Int?
  name      String @db.VarChar(255)

  createdAt     DateTime  @default(now()) @db.DateTime(0)
  updatedAt     DateTime  @default(now()) @db.DateTime(0)
  //relationships
  savedProducts Product[]
}

model Product {
  id        Int      @id @default(autoincrement())
  code      String   @db.VarChar(255)
  name      String   @db.VarChar(255)
  createdAt DateTime @default(now()) @db.DateTime(0)
  updatedAt DateTime @default(now()) @db.DateTime(0)
  //relations
  users     User[]
}
i can understand this will create implicit many to many relationship with users and products. but what if i also want find out users who like that certain product?
i guess this will be the correct one