-
Notifications
You must be signed in to change notification settings - Fork 244
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(psl, qe): Add relation mode prismaSkipIntegrity for MongoDB #4116
base: main
Are you sure you want to change the base?
feat(psl, qe): Add relation mode prismaSkipIntegrity for MongoDB #4116
Conversation
CodSpeed Performance ReportMerging #4116 will not alter performanceComparing Summary
|
This PR is a nice surprise, thanks @ItzSiL3Nce. Until your first PR is merged, we will need to allow the GitHub Actions to run by manually clicking - feel free to ping me in Prisma Slack (obvious name) if you need it to happen sooner. You are kinda "forcing" us to figure out our internal position on this now - but in a good way 😆. We started to dig into our design documents of both |
…de 'prismaSkipIntegrity'
…de 'prismaSkipIntegrity'
…tegrity' with non-MongoDB connectors.
af68f4a
to
2fa2a53
Compare
(I had to rebase and do some git evil tricks to change my author name and email in the commits, to sign the CLA. Sorry for the chaos) |
… mode with the new 'prismaSkipIntegrity' mode
…existence of the new 'prismaSkipIntegrity' relation mode
I think this existing issue captures the feature request here: prisma/prisma#15759 |
@Jolg42 Yes, I didn't notice that feature request, but yes. My PR allows setting the relationMode |
As "weak relations" we usually refer to a relation that is not backed by a foreign key in relational databases. So there are no constraints that enforce the values you can set, and also no referential actions and no guarantee of referential integrity. Not that different what is the default in MongoDB honestly. Regarding I wonder how Prisma now deals with data that is invalid, so a "relation scalar field" that points to another entry that does not exist for example. Or one side of a required relation, that does not actually have a counterpart on the other side. I think we have a quite extensive test suite in |
@janpio I've already built a custom engine and used it, on my machine, with my NodeJS PrismaClient, it works as I expected it to, don't know if the community would expect something else, though. I don't really know how to submit a public test for it though cause there are a lot of automatic dependencies being downloaded while running the prisma CLI and generating the correct binaries, I just manually plug my custom built binary after everything. What I did is I tested a delete query, with the 'query' log enabled. There was nothing in the logs other than the delete itself, worked well. It could also be possible to do like mongoose's populate function does when a referenced entity doesn't exist anymore, which is returning it as null, but that may be more complex to implement. It could also be possible to enforce all relations to be optional in the schema while using this relation mode, for better type-safety. |
Yes, we would take care of that by creating an "integration version" which then gets its own PR and branch at
Ok, I expect these exceptions and error messages are pretty terrible right now? Can you maybe share a few? That might indeed be one of those areas we want to polish. I could imagine we get a known error that is MongoDB specific that explains the situation a bit.
Can you explain that a bit more? For me https://mongoosejs.com/docs/populate.html is really what Prisma does automatically when you |
This is the error I get when attempting to prisma:error
Invalid `x.record.findUnique()` invocation in
/home/silence/Desktop/dev/redacted/packages/tests/dist/mongo.js:42:40
39 where: { id: relation.id }
40 });
41 console.log('Relation deleted, now trying to include the relation when finding the record');
→ 42 const record = await x.record.findUnique(
Inconsistent query result: Field relation is required to return data, got `null` instead.
PrismaClientUnknownRequestError:
Invalid `x.record.findUnique()` invocation in
/home/silence/Desktop/dev/redacted/packages/tests/dist/mongo.js:42:40
39 where: { id: relation.id }
40 });
41 console.log('Relation deleted, now trying to include the relation when finding the record');
→ 42 const record = await x.record.findUnique(
Inconsistent query result: Field relation is required to return data, got `null` instead.
at Hr.handleRequestError (/home/silence/Desktop/dev/redacted/packages/mongodb/dist/prisma-client/runtime/library.js:122:7171)
at Hr.handleAndLogRequestError (/home/silence/Desktop/dev/redacted/packages/mongodb/dist/prisma-client/runtime/library.js:122:6388)
at Hr.request (/home/silence/Desktop/dev/redacted/packages/mongodb/dist/prisma-client/runtime/library.js:122:6108)
at async l (/home/silence/Desktop/dev/redacted/packages/mongodb/dist/prisma-client/runtime/library.js:126:10298)
at async main (/home/silence/Desktop/dev/redacted/packages/tests/dist/mongo.js:42:25) {
clientVersion: '5.1.0'
} @janpio GOOD NEWS!
Yes I specifically meant the https://mongoosejs.com/docs/populate.html#doc-not-found part. |
The error message is not even that bad, I am surprised 👍
That is an interesting and logical observation. It could indeed be a way to help users do the right thing. On the other hand, I bet there are also users that want it to be a required relation, and there data is currently just too "messy" for it - so they want to be able to work with what they have but explicitly get errors. Does that thought make sense to you?
Does Mongoose also do something about the "foreign key" field or only for the "relation" (which is set to |
Yeah of course, it should be explained very well in order to not cause confusion or unexpected behaviours.
The foreign key part (the related entity's ObjectId) remains there. |
Note: I recreated this PR in our repo at #4123 @ItzSiL3Nce if you want to try it out pin Prisma packages to this version |
Thanks a lot, I'm going to use it and I'll also make some of my co-workers use it in our test environments, I'll give feedback in case of problems. |
@ItzSiL3Nce Note that I messed up the version in my previous post and I updated it now 🙈 |
I've been using, first in test environments and then in production ones, the custom pinned version for almost a year now (since it was published on NPM last August). I didn't have any problems with it and it has been super helpful. I noticed that a recent release of Prisma fixed and optimized a lot of stuff regarding MongoDB but I didn't see anything regarding this. It would be very nice if this issue/feature was more looked upon/implemented. |
@ItzSiL3Nce @janpio any news ? |
I'm waiting for it to be merged |
hi, any news on this? it seems very useful! |
This PR adds a new
relationMode
for Prisma calledprismaSkipIntegrity
.Related to the discussion prisma/prisma#20544
Closes prisma/prisma#15759
This PR, solves a common problem and also
drastically increases the performance of updates and deletes in MongoDB.
This new mode works only for the MongoDB
connector
and is needed to skip any (emulated) integrity check.This is needed in cases like mine where, for example, deleting a document from the database must not check for its references and leave them as they are, so they can be compared with backups and improve (by a lot) the performance of write operations, as they do not have to perform any emulation of relations.
This mode also makes the MongoDB
connector
work just like the official MongoDB client would (not caring about leaving "pending" relations).Of course, users using this
relationMode
should be careful and be aware of the potential problems arising during write operations, at the PrismaClient level. This can be done easily though, either by checking before running the query or with try-catch patterns (or similar).