JSFiddle - React, Tailwind, and code Playground

by dekkard

HTML

<script src="https://cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.jquery.min.js"></script>

JavaScript

var AlgoliaIndexMgr = (function(indexName) {
  var APPID = "Q51F4NGDBP";
  var SEARCH_API_KEY = "29fe3e2ea7de8014c6e9d595107c2794";
  var ADMIN_API_KEY = "1e75f5b6abfe3ae0bc25a7c445eec42d";

  var client = algoliasearch(APPID, ADMIN_API_KEY, {
    protocol: 'https:'
  });
  var index = client.initIndex(indexName);

  var getIndexedAttributes = function(callback) {
    return index
      .getSettings(function(err, content) {
        callback((err)? err : null, (!err) ? content.attributesToIndex : null);
      });
  };

  var updateRecord = function(newRecordData, callback) {
    // Check that objectID is present in passed data
    if (!newRecordData.objectID) {
      console.error("objectID not specified");
      throw new Error("objectID not specified!");
    }
		
    // Check the object exists
    index.getObject(newRecordData.objectID, function(err, content) {
    	if (err) {
      	console.error("Object not found: " + newRecordData.objectID);
        callback(err);
        return;
      }
      
      // Perform update
      index.partialUpdateObject(newRecordData, callback);
    });
  };
  
  var addRecords = function(newRecordsDataArray, callback) {
    index.addObjects(newRecordsDataArray, callback);
  };
  
  var deleteRecords = function(objectIDArray, callback) {
    index.deleteObjects(objectIDArray, callback);
  };


	// API
  return {
  	addRecords: addRecords,
    deleteRecords: deleteRecords,
    updateRecord: updateRecord,
    getIndexedAttributes: getIndexedAttributes
  };

});


// USAGE:

// List fields you chose to index in the index 'getstarted_actors'
AlgoliaIndexMgr('getstarted_actors').getIndexedAttributes(function(err, content) {
  if (err) {
    console.error("getIndexedAttributes error: " + err);
    return;
  }
  console.log("getIndexedAttributes success: %o", content);
});

// Add a record
AlgoliaIndexMgr('getstarted_actors').addRecords(
	[
    {
      "name" : "John Doe",
      "alternative_name" : "The greatest"
    },
    {
     ...