Giter Site home page Giter Site logo

strapi-hook-algolia's Introduction

strapi-hook-algolia

NPM Version Monthly download on NPM

This hook allows you to use Algolia as a service in Strapi strapi.services.algolia. 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.

Supported Strapi versions:

  • v3.6.x (recommended)
  • v3.x

Older version may work with the beta version of this hook, but are not supported.

Installation

# using yarn
yarn add strapi-hook-algolia

# using npm
npm install strapi-hook-algolia --save

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 hooks of your ContentType

saveObject() should be used in the Lifecycle callback afterCreate, fired after an insert, and afterUpdate, fired after an update.

  afterCreate(result, data) {
    strapi.services.algolia.saveObject(result, 'index');
  }
  afterUpdate(result, params, data) {
    strapi.services.algolia.saveObject(result, 'index');
  }

deleteObject() should be used in the Lifecycle callback afterDelete, fired after a delete query.

  afterDelete(result, params) {
    strapi.services.algolia.deleteObject(result.id, 'index');
  }

Full example

const index = 'post';

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

Draft & publish example
Here is an example on how to only index published entries, for when using the draft and publish feature.

const index = 'post';

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

Using the algoliasearch client
You can access the algolia javascript client, read the official documentation to know more.

// api/my-model/controllers/my-model.js

module.exports = {
  async myController(ctx) {
    // https://www.algolia.com/doc/api-reference/api-methods/
    const { client } = strapi.services.algolia;
    await client.listIndices();

    // ...
  },
}

Hook 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_admin_api_key',
        debug: true,              // default: false
        prefix: 'my_own_prefix',  // default: Strapi environment (strapi.config.environment)
      },
    }
  };

Prefix

The default prefix uses strapi.config.environment, resulting in for example: 'development_post'.
A custom prefix can be configured with the prefix option in the hook config.
If you don't want to use any prefix at all, you can configure it with: prefix: false.

Support

Resources

License

  • Copyright (c) 2020-2021 Mattias van den Belt & Strapi Solutions (MIT License).

strapi-hook-algolia's People

Contributors

mattiebelt avatar francois2metz avatar stafyniaksacha avatar meck93 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.