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

As @sunnyson pointed out not really no. The only time it would really be needed to call the service is if you have customized the service in ./api/someModel/services/someModel.js or if you are building a custom plugin with custom services.

For the most general of use-cases it’s easier and faster to just directly reference the query instead since our default services from the core-api do the following:

module.exports = {
  find(params, populate) {
    return strapi.query('restaurant').find(params, populate);
  },
  findOne(params, populate) {
    return strapi.query('restaurant').findOne(params, populate);
  },
  count(params) {
    return strapi.query('restaurant').count(params);
  },
  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;
  },
  async update(params, data, { files } = {}) {
    const validData = await strapi.entityValidator.validateEntityUpdate(
      strapi.models.restaurant,
      data
    );
    const entry = await strapi.query('restaurant').update(params, 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;
  },
  delete(params) {
    return strapi.query('restaurant').delete(params);
  },
  search(params) {
    return strapi.query('restaurant').search(params);
  },
  countSearch(params) {
    return strapi.query('restaurant').countSearch(params);
  },
};

From the above examples you can see there is some additional logic on the create and update services but none of the others.

1 Like