Search code examples
strapi

Fetch internal data


I'm new to Strapi and I'm trying to build an app. I'm using a single type ('global-settings') to store various data (e.g. field : 'apiUrl') that I'm planning to use internally, in a plugin.

I didn't manage to figure out how I retrieve that data and use it within the plugin.

Can you please help me or indicate where I could find this information?

Thanks!


Solution

  • Alexandru-gorgos, this will be the best way, you can use it on any controller, service, cronjob, etc.

    On my example, (line 6) I am using strapi.services to reach for app-settings and do a lookup into that single-type model.

    On the controller I am using sanitizer to clean the response, you don't really need that if you are not returning that data. You can use only the entity to work with your data.

    Using that you will have access to find(), update() and delete for sigle-types

    'use strict';
    const { sanitizeEntity } = require('strapi-utils');
    module.exports = {
      index: async (ctx, next) => {
        const entity = await strapi.services['app-settings'].find();
        return sanitizeEntity(entity, { model: strapi.models['app-settings'] });
      },
    };
    

    enter image description here

    Ref:

    1. backend-customization single-type