MattieBelt / mattie-strapi-bundle

Mattie plugin bundle for Strapi
https://mattie-bundle.mattiebelt.com
MIT License
40 stars 19 forks source link

Custom rules on fields and Published conditions #70

Open thiboot opened 2 years ago

thiboot commented 2 years ago

Hello,

With the old version of Algolia Hook i was able to do some customizations on object fields and trigger the record only on publish.

'use strict';

const index = 'songs';

const songToAlgolia = ({id, title, slug, content, historicalFacts, illustration, audio, categories, albums, books}) => {
  strapi.services.algolia.saveObject({
    id,
    title,
    slug,
    content,
    historicalFacts,
    illustration: illustration.length > 0  ? illustration.map(({name, alternativeText, caption, url}) => ({name, alternativeText, caption, url})) : null,
    audio: audio.length > 0 ? audio.map(({name, alternativeText, caption, url}) => ({name, alternativeText, caption, url})) : null,
    categories: categories?.length || 0,
    albums: albums?.length || 0,
    books: books?.length || 0,
  }, index);
}

module.exports = {
  lifecycles: {
    afterUpdate(result, params, data) {
      if (result.published_at) {
        songToAlgolia(result);
      } else {
        strapi.services.algolia.deleteObject(result.id, index);
      }
    },
    afterDelete(result, params) {
      strapi.services.algolia.deleteObject(result.id, index);
    },
  },
};

Is it possible to do it in the new search plugin ? If no can i adapt the old plugin to work with Strapi v4 ?

Meljesson commented 2 years ago

I would like to know the same. My org uses algolia a lot and I have used this way of indexing our data from Strapi and it is blocking our migration to v4.