System Information
- Strapi Version: 3.6.6
- Operating System: Linuzzz
- Database: MySQL
- Node Version: 16
I am writing my own custom controllers and services on strapi backend and it was always a mystery to me how to specify what fields I want retrieved from the database.
By default this
strapi.query('model').findOne({id: "1"});
will return an entire model for me.
But if I am interested only in one field, this seems to be an overkill.
How do I do it in a Strapi way? So far I was just using knex and raw queries where I can do whatever SQL allows. But the issue with this approach that it produces unportable code because the response is in database specific format and if I decide to migrate to Postgres for example, I will have to modify all the code that deals with the responses.
Anyone can help with this please?
Also, what is the idiomatic way in Strapi to do “JOIN” queries? Say I need to get something like:
Say I have a Post model, it relates to a User model (by id) and the User has a role that can be “admin” or “newbie” (just an example).
How can I get something like below working?
strapi.query('post').findOne({user.role: "admin"});