Filtering with Queries or with TS?
What is the preferred approach to filtering (for example filtering on tenant id or on soft deletes) .
In this case the _id of the document is provided but additional checks need to be done.
Is it using regular and then doing the filtering in TypeScript
Or
Is it creating a index on the to be filtered table and then doing the filtering with and with a falsy check?
My thoughts:
Using the TypeScript approach gives the developer more fine grained control on handling the exact error case and adding logging and meaningful responses to the client. It does add boilerplate though.
Using the query approach seems to be easier when it comes to adding new constraints to a request and it seems to have less boilerplate.
I would like your thoughts on this and on how you handle these cases.
2 Replies
There are some general best-practices for queries, but it also partly depends on what you want/need to achieve with the query.
If you have the document ID and it's just a single document, then
ctx.db.get will always win.
If you need to get multiple documents and you have all of their IDs, then Promise.all combined with ctx.db.get will work well.
If you're retrieving multiple documents but it's more of a pattern-matching situation, then it's best to try using indices where possible, and then normal JS filters beyond that.
Could you describe more details about your use case?Nvm my assumption was wrong I thought it was possible to filter with an index and my filter parameters and the document id but as far as my research revealed, _ fields can not be used with indices