prisma / docs

📚 Prisma Documentation
https://www.prisma.io/docs
Apache License 2.0
983 stars 779 forks source link

Allow use of `findUnique()` with MongoDB composite types #3453

Open andrew-walford-prisma opened 2 years ago

andrew-walford-prisma commented 2 years ago

Update 05 Sep 2022:

I accidentally merged this, so I reverted it, then made a new PR. The new PR is here: https://github.com/prisma/docs/pull/3722 This docs work is done, but we need to wait until the feature actually foes out. We will need to update the version numbers in the text to [whatever version this goes into]

SME: Cristian, Daniel

Cristian and Dan have reviewed it; however, we can't merge this, because there are issues with it. Also note that we still mention 4.3, and this didn't go out with that version, so we'll need to update it.

This is NOT a preview feature

Notes:

Our docs here currently giv an example of composite findUniques as they work now:

prisma
  .user
  .findUnique({
    where: {
      firstName_lastName: {
      fullName: {
        firstName: "Paul",
        lastName: "Panther"
    }
  })

Incidentally, we should ensure we link to these examples from our main findUnique entry (if we don't already), because we don't go into this amount of detail there.

MongoDB has embedded documents. We currently don't support findUniques with them. With this new feature, composite findUniques work with MongoDB embedded types.

With this feature, you can findUnique on embedded documents, as follows:

In the schema:

@@unique([name, location.address])
@@unique([cost, location.address])

So if we want to find records with a unique attribute that is a combination of name, location.address and cost (a bit of a wacky example - we might be able to come up with a better one for the doc), our app code might be as follows:


findUniqueA(where: {
  name_location_address: {
    name: "",
    location: {
      address: ""
    }
  },
  cost_location_address: {
   cost: 1,
   location: {
     address: ""
    }
  }
})
wlans commented 8 months ago

Is this PR going to go in? Considering Prima client for a project.