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

Add support for $jsonSchema for mongodb #8135

Open
Tracked by #16311 ...
mgcrea opened this issue Jul 7, 2021 · 7 comments
Open
Tracked by #16311 ...

Add support for $jsonSchema for mongodb #8135

mgcrea opened this issue Jul 7, 2021 · 7 comments
Labels
kind/feature A request for a new feature. status/has-stopgap A stopgap for this functionality has been implemented. team/client Issue for team Client. team/schema Issue for team Schema. topic: database-functionality topic: json-schema topic: mongodb

Comments

@mgcrea
Copy link

mgcrea commented Jul 7, 2021

Problem

Prisma currently does not support a mongodb json schema, is there any plan to add support to it?

Suggested solution

Having $jsonSchema support would be nice to have safe guarantees over the integrity of our data (when using an external tool, or performing raw aggregation operations, etc.).

It would also enable existing databases migration that rely on this feature (currently my case).

Finally it could also enable easy introspection of an existing database and help on other subjects as well, if it makes things simpler/safer, I think it could make sense to have it enabled by default and drop support for schema-less mongodb databases.

Additional context

One major pain point when dealing with jsonSchema was the lack of proper error reporting when an operation fails, would not impact end user that much thanks to the type-safety but would have been harder to develop / test, it has been fixed in SERVER-20547 that landed in 4.9.0.

@dimaip
Copy link

dimaip commented May 10, 2022

IMO this is a crucial feature for Prisma+Mongo combination.
Currently it's very easy to run into runtime validation failures (even on read!) due to invalid data in the database.
The problem comes from the fact that relational databases make it technically impossible to have invalid data not conforming to the schema in the database, and with Mongo we have no such guarantees unless we use $jsonSchema, which makes things super fragile. We've been running into terrible crashes in production, where in staging it all worked fine and then due to some slightly incorrect record everything blew up.

@janpio
Copy link
Member

janpio commented May 12, 2022

What could a workflow for using $jsonSchema in Prisma for its MongoDB connector look like?

@dimaip
Copy link

dimaip commented May 13, 2022

@janpio I think it could work as a generator, in a manner similar to this: https://www.npmjs.com/package/prisma-json-schema-generator
Then there could be a command to apply it to Mongo models.

@dimaip
Copy link

dimaip commented May 13, 2022

Btw I'm currently experimenting with that package, trying to see how much work it would be to convert its format into what $jsonSchema would take.

@janpio
Copy link
Member

janpio commented May 13, 2022

"applying to Mongo models" sounds a bit like a Migration, similar to how we add indexes and uniques to MongoDB collections via prisma db push.

We have little experience with $jsonSchema, so really are happy about any input on how it is used. I think we understand that one can apply it to a colleciton and then new data is validated against that schema. But what about existing data when a json schema is added the first time? What about changes to the schema after data was created?

@dimaip
Copy link

dimaip commented May 13, 2022

But what about existing data when a json schema is added the first time?

It would still be malformed. BUT, Mongo Atlas has some nice tooling to validate and fix existing data.

What about changes to the schema after data was created?

Same.

@janpio janpio added the team/schema Issue for team Schema. label May 24, 2022
@dimaip
Copy link

dimaip commented Jul 6, 2022

So I've forked prisma-json-schema-generator and:

  • made it produce the JSON schema in flavour which Mongo understands
  • added some scripts to automatically apply the schema to collections and validate their contents

https://github.com/joindeed/prisma-mongo-json-schema-generator

Feel free to experiment with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. status/has-stopgap A stopgap for this functionality has been implemented. team/client Issue for team Client. team/schema Issue for team Schema. topic: database-functionality topic: json-schema topic: mongodb
Projects
None yet
Development

No branches or pull requests

4 participants