strapi-plugin-meilisearch icon indicating copy to clipboard operation
strapi-plugin-meilisearch copied to clipboard

Override Services

Open martin-landhi opened this issue 2 years ago • 6 comments

How can i override services of this plugin?

I tried wit "strapi-server.ts" on "extensions/meilisearch/strapi-server.ts"

module.exports = (plugin) => {
    plugin.services['meilisearch'].addCollectionNamePrefix = () => {
        console.log('asd');
    };
      
    return plugin;
};

but seems do not work. I am doing something wrong? it this possible?

martin-landhi avatar Jul 17 '23 17:07 martin-landhi

Hey @martin-landhi, I'm not aware of plugin service overriding. Can you show me the docs? Also, out of curiosity, why do you need to overwrite?

bidoubiwa avatar Jul 18 '23 11:07 bidoubiwa

Hey @bidoubiwa

Basically i need to search by geolocation in meilisearch. But meilisearch do not allow right now use geolocation with multiple address per document. So i need to populate to meilisearch several documents and then use "districtAttribute" to get only one document as result.

For that reason i need to override the services/meilisearch/connector.js that is where the documents are sended to meilisearch in this plugin and add my custom logic there to get populated those extra documents.

My idea is add my logic inside "sanitizeEntries" function.

Are the a posibility in strapi to add my custom logic there overriding that service in strapi-server.js? That is described here:

https://docs.strapi.io/dev-docs/plugins-extension

Let me know if you need more information.

Thanks

martin-landhi avatar Jul 18 '23 12:07 martin-landhi

Hey @martin-landhi could transformEntries answers your needs? https://github.com/meilisearch/strapi-plugin-meilisearch#-transform-entries

You can also add distinctAttributes in the plugin settings here: https://github.com/meilisearch/strapi-plugin-meilisearch#-add-meilisearch-settings

My bad if it does not help. In which case I'll give it a deeper look :)

bidoubiwa avatar Jul 18 '23 13:07 bidoubiwa

Hey @bidoubiwa

transformEntries could works but i need access to add extra entries to meilisearch from there. How can i do that?

Example:

meilisearch: {
        config: {
          user: {
            settings: {
              searchableAttributes: ['extras', 'email', 'username'],
              filterableAttributes: ["_geo", "website", "extras", "type", "extras.proCategories"],
              sortableAttributes: ['_geo','sortOrder'],
              distinctAttribute: 'username'
            },
            entriesQuery: {
              populate: ['projects.mainPhoto.media', 'extras.proCategories', 'website', 'profileImage'],
            },
            transformEntry({ entry }) {
              
              let newEntry = {...entry};
              newEntry.name = "other name"
              //ADD newEntry AS EXTRA DOCUMENT HERE TO MEILISEARCH

              return entry;
            },
          },

martin-landhi avatar Jul 18 '23 13:07 martin-landhi

@bidoubiwa or any other idea to do this?

i could do it touching the project files directly on node_modules but i dont know how to rewrite them withut touch that, if i could not do it with transformEntry

martin-landhi avatar Jul 18 '23 19:07 martin-landhi

Hey @martin-landhi, unfortunately, not out of the top of my head :/ Another possibility is for you to fork the project, make the changes and publish it on npm. For example: @martin-landhi/strapi-plugin-meilisearch. For example, https://www.npmjs.com/package/@mattiebelt/strapi-plugin-meilisearch

bidoubiwa avatar Jul 20 '23 10:07 bidoubiwa