Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend WhereUnique for non unique filters #3281

Merged
merged 22 commits into from
Oct 17, 2022
Merged

Conversation

Weakky
Copy link
Member

@Weakky Weakky commented Oct 12, 2022

Overview

  • Adds a preview feature flag extendedWhereUnique
  • Adds non unique fields and boolean operators to WhereUniqueInputs
    • It is still mandatory to specify at least one unique field outside of any boolean operators.
  • Adds WhereInputs to nested delete/disconnect/update for to-one relation
  • Adds DMMF property to specify which fields constraints are applied on

Schema changes

Considering the following datamodel,

model User {
  id Int @id
  email String @unique
  age Int
  name String

  toOne ToOne?
}

model ToOne {
  id Int @id

  userId Int @unique
  user User? @relation(fields: [userId], references: [id])
}

Here are the additions:

input UserWhereUniqueInput {
  id: Int
  email: Int
  age: Int // ++
  name: String // ++
  AND: [UserWhereInput] // ++
  OR: [UserWhereInput] // ++
  NOT: [UserWhereInput] // ++
}

This new WhereUniqueInput affect all reads and writes that uses it, such as:

  • findUnique
  • deleteOne
  • updateOne
  • upsertOne
  • and most nested mutations

Furthermore, on one-to-one relation, it adds the ability to add additional filters via a WhereInput (since the record is pinned by the QE). All examples below aren't possible without the extendedWhereUnique feature flag:

await prisma.user.update({ where: {}, data: {
  to_one: {
    disconnect: { /* ToOneWhereInput */ },
    delete: { /* ToOneWhereInput */ },
    update: { where: { /*ToOneWhereInput*/ }, data: {} },
    upsert: { where: { /*ToOneWhereInput*/ }, create: {}, update: {} }
  },
}})

@Weakky Weakky force-pushed the feat/extended-where-unique branch 2 times, most recently from f544312 to a60059b Compare October 13, 2022 15:48
@Weakky Weakky added this to the 4.5.0 milestone Oct 13, 2022
Copy link
Contributor

@garrensmith garrensmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small feedback. But this is looking really good.
Once the tests are passing I think we good to go.

@@ -11,7 +11,7 @@ mod where_unique {
&runner,
"query { findUniqueUser(where: {}){ id }}",
2009,
"Expected exactly one field to be present, got 0."
"Expected a minimum of 1 fields of (id, email, first_name_last_name) to be present, got 0."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you rename this test file to extended_where_unique

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "extended where" is a temporary name that we use to qualify the feature we're building. But those tests are for a where unique. Once we remove the feature flag, we'll refer to this as where unique again. Why do you think we should rename the file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of extended where because these sets of tests are around that concept. yes its just normal where but if I came to the code base and looked at a where_unique test I would think its a little strange to be there. But if we have the extended_where_unique it gives me context. There is something related to a new feature that was added later to the where_unique functionality

Copy link
Member Author

@Weakky Weakky Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if I came to the code base and looked at a where_unique test I would think its a little strange to be there

I'm sorry but I don't understand why you'd find that strange. This where_unique.rs test file already existed. All I did was add a couple more tests to it. In 2 months, we'll GA and remove this feature flag and extended_where_unique won't make any sense anymore. It won't be easier either from a visibility/searchability perspective because we'll have where unique tests spread in two different files for no obvious reasons because extendedWhereUnique won't be a concept anymore. It does make sense right now but think about onboarding developers 2 years from now without any of this context.

query-engine/core/src/query_ast/write.rs Outdated Show resolved Hide resolved
query-engine/core/src/query_document/error.rs Outdated Show resolved Hide resolved
query-engine/core/src/query_document/parse_ast.rs Outdated Show resolved Hide resolved
@Weakky Weakky marked this pull request as ready for review October 17, 2022 13:17
@Weakky Weakky requested a review from a team as a code owner October 17, 2022 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants