hey. Is there a way to validate that Mongodb datab...
# mongodb
d
hey. Is there a way to validate that Mongodb database contents matches the schema? I really need a way to make sure we don’t run into P1012 errors in production due to some bad data…
d
The easy answer here is to write a separate script that only reads all data, maybe paged. If you run into errors, it doesn't match... but I guess you're looking for another method?
d
Yeah, that would be kind of expensive to do that, I was hoping for a slightly more efficient approach
p
but how can it be possible without reading all the data? due to schema-less data storage in mongo the only way to check the structure is to read it I believe
d
dunno. E.g. Mongo Atlas/Realm has a built in schema validator that’s pretty efficient, was hoping for a similar kind of tool
d
Schema validator is a good thought. We definitely want to leverage JSON schema in the future, but it's tricky to get right. Additionally, JSON schema only applies to data inserted after the schema was created, requiring you to re-insert all your data anyway during the migration if you want to have safety. But I agree, direct support from Prisma for JSON schema would be great regardless. Sorry that I can't offer anything better at the moment.
d
I think Atlas has an instrument which allows you to validate db contents and shows the failing records. At least they have it for Realm. Gives confidence!
IMO prisma shouldn't be throwing on read validation failures, only on writes, or at least have it configurable, like Realm, especially not until there's tooling available to validate it
j
E.g. Mongo Atlas/Realm has a built in schema validator
Do you have a link to learn more about that?
d
my apologies @janpio, missed your reply. E.g this https://www.mongodb.com/basics/json-schema-examples
j
Is this what you meant with the validator?
d
yeah
could be better lol, but at least something
j
Absolutely. Here is an issue requesting more native support for this: https://github.com/prisma/prisma/issues/8135 And there is a community generator to turn your Prisma schema into a JSON schema: https://www.npmjs.com/package/prisma-json-schema-generator A combination of these two things could be quite handy...
A usual: +1 on the original description in the feature request issue and comment with your specific use case and needs very welcome
d
@janpio that community generator does v7 JSON schema which is not compatibly with Mongo. I’ve forked it and added some scripts to automate applying of the generated schema to collections. works like a charm https://github.com/joindeed/prisma-mongo-json-schema-generator
prisma rainbow 1
j
Then definitely mention that in the issue.
d
I will. Just published it today, need to polish the readme etc
👍 1
j
Shared it internally already, made people happy 😄
fast parrot 1
(Feel free to replace mentions of "Prisma 2" with just "Prisma ORM".)
d
I will! I know how it feels when people misname your product lol
j
Haha no worries, this is even still semi correct right now as Prisma 1 is still alive etc. But with Prisma version of course now being 4.0.0 etc this is a mess we are slowly trying to start fixing 😄