Add support for many to many relationships #1456
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses various issues regarding implicit many to many relationships, as reported in #1237 for instance.
I added a new entity
Commententity related toPostlike:and added relevant coverage for both create and update cases.
The
RelationshipStoreclassThe current mock client was not building intermediate cross tables for implicit relationships, so I did implement an helper for that, that is initialised in
createPrismock.I derived that an implicit relationship appears twice in the relationship list, both times with empty
relationFromFieldsandrelationToFields.A check has been injected in
create.ts,update.tsandfind.tslike:This populates the
wherewith the ids stored with the connect operations. Before it was just passing{ undefined: undefined }, leading to the retrieval of all available records and not just the connected ones.Symmetrical implicit relationships
i.e. a friendship relation over the
Usertable. Although an explicit relationship allows for metadata (date of addition, id of the user who initiated the request, etc.), there is also the implicit case.In the test I updated the schema as:
(I chose
connectionsoverfriendsbecause the field was already existing). The other hand side of the connection (symmetricalConnections) is needed just to satisfy the Prisma constraint, as suggested here.Some adjustments in the connected ids were needed in the relationship store, see the
extractSymmetricalValuesmethod.Usage
The
RelationshipStoresingleton is exported fromclient.tsfor cleanup and debugging purposes in your actual application tests:To clean the relationship cache between test runs:
To debug the current relationship state:
You'll get the following log:
Open problems
It would be great not to expose the relationship store singleton, but to add methods to directly the Prismock client. I was facing some circular reference issue, I'll see if I can solve the issue.
While I managed to support some
none/somecase, there are still uncovered scenarios.Hope this helps!