Best way to modify a provider's metadata programmatically


#1

Hi guys, this time I want to modify some fields of my data source, that is, the fields of certain indexes that I have in Elasticsearch, but dynamically, depending on some conditions. I want to modify the labels, the formats, the currency, the display format etc, once the database has already been consulted.

I have seen the structure of the provider and I see that it has a metadata object and another customMeta, where I can find the metadata of each particular index including the fields.

Is it correct to modify the fields directly there or is there a better way to achieve this?


#3

Hi @oliver89, the ChartFactor api provides the proper way to perform these metadata modifications. Using the addSourceMetaData function, it is possible to achieve this in a clean way.

const metadata = {
    "fields": {
        "city": { "label": "My new City Label" },
        "state": { "label": "My new State Label" }
    }
}

cf.getProviderByConfig({
    name: 'Elasticsearch',
}).addSourceMetaData('chicago_taxi_trips', metadata);

In our documentation we have a section in which you can see you can see in depth how to Programmatically updating source custom metadata.

Please note that when you want to modify the metadata after the database has been queried for the first time, the best practice is to unregister and re-register the provider to avoid exceptions related to metadata immutability.

Let me know if this answered your question.


#4

Hi @juan.dominguez, now I get it, it worked for me. Thanks for the wonderful explanation you have in the documentation.