npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-hook-algolia

v3.8.1

Published

A hook to setup an Algolia service for the Strapi framework

Downloads

148

Readme

strapi-hook-algolia


Strapi v4.x support, see @mattie-bundle/strapi-plugin-search at https://mattie-bundle.mattiebelt.com/search/plugin.


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).