maarten-dp / flask-restless-datamodel

Server side code for flask-restless-client
MIT License
1 stars 4 forks source link

Allowing to select specific fields to retrieve instead of sticking to pre-defined datamodel #5

Open Colin-b opened 2 years ago

Colin-b commented 2 years ago

Would you consider the addition of a feature consisting in sending additional filters to select only some fields in the datamodel, instead of requesting the full relations?

Use case: My model is linked to some other models but I only want to retrieve some fields on this model (and nothing on related models). So I would like to speed up the request time by requesting only relevant information to the database instead of the full model + relations (and it would also reduce the time to serialize/deserialize + data network transit time)

Best Regards

maarten-dp commented 2 years ago

Hi Colin,

Normally the models are loaded by a single call, which includes first layer relational information. The rest is lazily loaded when it's requested. On the server side SQLAlchemy does relatively efficient data loading as well.

So if you're having performance issues, I'm betting that you're loading an instance that has a considerable amount of relational information. And in this case, the bottleneck would indeed be the (de)serializing of data.

The restless client is built on two functionalities:

I assume the issues you're running into have to do with the CRUD part (i.e. flask restless), but on the off chance that you're having issues with the RPC side, you should be able to optimize the return value of your remote call.

If we're indeed talking about performance issues with the CRUD part, then a possible solution is to implement your own (de)serializers, where you can omit the inclusion of the relational information. You could potentially toggle relational data on or off with a custom request parameter which you would then evaluate in your serializer.

Unfortunately, dynamically getting to choose which fields you load, is not something that is supported by flask-restless, as it evaluates inclusion fields at init time. And to enable this is something that will have to happen in the flask-restless repo, which unfortunately, I have no control over.

So in short, I'm afraid your request is out of my hands and the best advice I can give you is to implement your own (de)serializers.

Either way, these are my current thoughts, but I'm willing to have a deeper look if you have compelling arguments, or clever solutions.