What's the difference between query('restaurants').find() and services.restaurants.find()?

Hello @mariusbolik

Services → Reusable functions that are predefined by strapi with some custom business logic (those are usually wrapped around the query functions actually but with some additional custom logic inside them)

Query → Direct query to database

Example of how these are used:

strapi.services.restaurants.find(), that services will actually call the next code:

find(params, populate) {
    return strapi.query('restaurant').find(params, populate);
  },

It is calling the strapi.query() inside it. The thing is that you can modify these services and add your own business logic to it, but you can’t modify query() functions.

Take a look at another example that strapi uses:
strapi.services.restaurants.create(restaurantData)

This one will execute a service called create()

async create(data, { files } = {}) {
    const validData = await strapi.entityValidator.validateEntity(strapi.models.restaurant, data);
    const entry = await strapi.query('restaurant').create(validData);

    if (files) {
      // automatically uploads the files based on the entry and the model
      await strapi.entityService.uploadFiles(entry, files, {
        model: 'restaurant',
        // if you are using a plugin's model you will have to add the `source` key (source: 'users-permissions')
      });
      return this.findOne({ id: entry.id });
    }

    return entry;
  },

As you can see, that service uses some additional business logic, not only the query().create() as in the example with query().find()

1 Like