Change Eve Python DOMAIN collection schema dynamically during runtime

105 views Asked by At

I am using the Eve Python API Framework for MongoDB. I am writing a feature that allows my users to edit metadata sections for the documents that they are writing.

Example JSON:

{
    "metadata": {
        "document_type": ["story"],
        "keywords": ["fantasy", "thriller"]
    }
}

We have a CMS for the document editor that admins can use to allow them to do things like add new metadata fields for the authors (normal users) to add more information about their posts. For example, the site admin may want to add a field called "additional_authors" which is a list of strings. If they add this section to the frontend we would like some way to add it to the Eve Schema on the backend in real time without restarting the server. It is very important that it be real time and not part of a coding change in Eve or requiring Eve to restart.

Our current hard-coded metadata schema looks like this for the document collection:

{
       "metadata": {
            "type": "dict",
            "schema": {
                "document_type": {"type": "list", "required": True},
                "keywords": {"type": "list", "required": True}
            }
        }
}

I understand that we can go with a non-strict approach when writing the "metadata" type dict so that it does not care what is inside but from my understanding this means we would not be able to use "projection" properly meaning that if I only wanted to return "metadata.additional_authors" of all documents through my API call, I would not be able to do so. Also, this would mean that we would have to deal with the required check ourselves using hooks instead of the built-in Eve schema check.

Is there anyway around this issue by essentially having a dynamic schema based on a MongoDB document that we can store the entire collection configuration dict in and retrieve it without restarting the server for it to take effect? Even if this means adding a hook to the new schema_dict collection and calling some internal Eve function I am all ears.

Thank you ahead of time for your help.

0

There are 0 answers