azimuttapp / azimutt

Explore and optimize any database
https://azimutt.app
MIT License
1.09k stars 78 forks source link

Handle MongoDB schema discriminators #294

Open BryanBerger98 opened 1 month ago

BryanBerger98 commented 1 month ago

Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.

With MongoDB, you can have multiple schemas assigned to the same model or key. This is called a discriminator. It needs a discrimination key to determine which schema should be used.

For example, imagine the User schema has a role key which can be used as a discrimination key. The admin value assigned to the role key allows us to use the AdminUserSchema and the customer value assigned to the role key allows us to use the CustomerUserSchema.

The Mongoose docs explain it well: https://mongoosejs.com/docs/discriminators.html

loicknuchel commented 2 weeks ago

Hi @BryanBerger98 Thanks for this. This is already supported by the MongoDB connector but not handled in the gateway :sweat_smile: I can do it quite easily but in the meantime you can export your schema with the CLI and then import it to Azimutt in JSON, it should work. For example: npx azimutt explort $mongo_url --mixed-json kind. I defined this attribute for the whole db, but maybe it's not good. Do you have some collection with it and some without it? Is it always the same attribute for all collections? If no, any idea how to get that input? Cheers!

loicknuchel commented 2 weeks ago

Hi @BryanBerger98 I deployed a new version of the gateway (0.1.7) which handle the discriminator. You can add it to your connection url like: mongodb+srv://<user>:<pass>@<host>?discriminator=kind Tell me how it goes ;) Cheers!