I would like to set up a collection on MongoDB where each document holds multiple eve resources.
This is a NoSQL approach that differs from SQL where you would have different tables that share a key- An example of an SQL style approach can be seen in this answer: https://stackoverflow.com/a/26175023
I want to take full advantage of Eve's routes so that I may create different pre
and post
hooks for each different resource on the document.
For example, let's say I want to have a collection named city_metrics
that holds citizen count and pet count as different resources, and I want different hooks to fire when dog_metrics
resource and a citizen_metrics
resource are updated. They all relate to a single city_id
of the city they relate to, which is a separate collection.
Here's an example of a broken setup that I hope shows what I'm trying to accomplish here:
dog_metrics_schema = {
"number_of_dogs": {"type": "integer"},
"average_fluffiness": {"type": "float"},
}
dog_metrics_eve_resource = {
"type": "dict",
"item_title": "dog_metrics",
"description": "Dog metrics",
"schema": dog_metrics_schema,
"datasource": {"source": "city_metrics"},
}
citizen_metrics_schema = {
"number_of_citizens": {"type": "integer"},
}
citizen_metrics_eve_resource = {
"type": "dict",
"item_title": "citizen_metrics",
"description": "Citizen metrics",
"schema": citizen_metrics_schema,
"datasource": {"source": "city_metrics"},
}
city_metrics_eve_resource = {
"item_title": "city_metrics",
"description": "Metrics that relate to a City",
"schema": {
"city_id": {"type": "objectid", "required": True, "unique": True},
},
"datasource": {"source": "city_metrics"},
"mongo_indexes": {"city_id": ([("city_id", 1)], {"background": True})},
}
DOMAIN = {
"city_metrics": city_metrics_eve_resource,
"dog_metrics": dog_metrics_eve_resource,
"citizen_metrics": citizen_metrics_eve_resource
}
I want to be able to manipulate dog_metrics
and citizen_metrics
through their own routes as if they were their own resource, the fact that they share a document is to be treated as an internal implementation detail.
Does anyone know how to set something like this up?
Further reading on features I have tried to use:
https://docs.python-eve.org/en/stable/features.html#sub-resources
https://docs.python-eve.org/en/stable/config.html#limiting-the-fieldset-exposed-by-the-api-endpoint
https://stackoverflow.com/a/57104359