/strapi-hook-algolia

Primary LanguageJavaScriptMIT LicenseMIT

strapi-hook-algolia

This hook allows you to use Algolia as a service in Strapi. Algolia is a hosted search engine capable of delivering real-time results from the first keystroke. Algolia's powerful API lets you quickly and seamlessly implement search within your websites, mobile, and voice applications.

Usage

  1. Create a Algolia account
  2. Edit your config, add your own Application ID & Admin API Key
  3. Use the algolia service in the Lifecycle callbacks of your Content type(s)

strapi.services.algolia

saveObject should be used in the Lifecycle callback _afterCreate. Fired after an insert.

  afterCreate(result) {
    strapi.services.algolia.saveObject(result, index);
  },

updateObject should be used in the Lifecycle callback afterSave. Fired after an update query.

  afterCreate(result) {
    strapi.services.algolia.saveObject(result, index);
  },

deleteObject should be used in the Lifecycle callback afterDelete. Fired after a delete query.

  afterDelete: (result) => {
    strapi.services.algolia.deleteObject(result.id, 'information');
  },

Full Example

const index = 'post';

module.exports = {
  lifecycles: {
    afterCreate(result) {
      strapi.services.algolia.saveObject(result, index);
    },
    afterUpdate(result, params, data) {
      strapi.services.algolia.saveObject(result, index);
    },
    afterDelete(result, params, data) {
      strapi.services.algolia.deleteObject(result.id, index);
    },
  },
};

Strapi config

To activate and configure the hook, you need to create or update the file ./config/hook.js in your strapi app.

  module.exports = {
    settings: {
      ...
      "algolia": {
        "enabled": true,
        "applicationId": "ABCDEFGHIJ",
        "apiKey": "secure_algolia_api_key",
        "debug": true,              // default: false
        "prefix": "my_own_prefix"   // default: Strapi environment (strapi.config.environment)
      },
    }
  };

Installation

npm i strapi-hook-algolia

Resources

Links