"Dynamic" datasource in python eve?

104 views Asked by At

how could I achieve a sort of a dynamic datasource got from the incoming post-payload ? So all incoming payloads "create" a new collection in mongodb or use the old one if available...

Ex. payload: {"a1": "999", "d1": "06.05.2020 04:29:16", "d2": "0", "d3": "0", "id2": "3777"}

part of settings.py:

mdata = {
    'schema': {
            **'a1': {
                'type': 'string'**
            },
            'd1': {
                'type': 'string'
            },
            'd2': {
                'type': 'string'
            },
            'd3': {
                'type': 'string'
            },
            'id2': {
                'type': 'string'
            }
        },
    'datasource': {
            **'source': "a1-value" #should be 'a1' value from the post payload**
        },
    "resource_methods": ["POST"],
    }

DOMAIN = {
    "accounts": accounts,
    "user": user,
    "mdata": madata,
    }
1

There are 1 answers

0
Iman Shafiei On

In the eve, the collection name is your resource name:

for example, assume that this is your DOMAIN:

DOMAIN = {
    'test': <your_test_endpoint_variable>,
}

so when you request this endpoint a collection with a test name is created.

But the source is

Name of the database collection consumed by the resource. If omitted, the resource name is assumed to also be a valid collection name.

I suggest reading this section of the document:

https://docs.python-eve.org/en/stable/config.html#advanced-datasource-patterns