diff options
Diffstat (limited to 'content/snippets/browse-update-algolia-index.md')
-rw-r--r-- | content/snippets/browse-update-algolia-index.md | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/content/snippets/browse-update-algolia-index.md b/content/snippets/browse-update-algolia-index.md new file mode 100644 index 0000000..6dbe91e --- /dev/null +++ b/content/snippets/browse-update-algolia-index.md @@ -0,0 +1,53 @@ +--- +title: Browse Algolia index and update objects (JS) +description: Browse Algolia index and update objects (JS) +tags: ["algolia"] +--- + +```js +const algolia = require("algoliasearch"); +const client = algolia("<YOUR-APP-ID>", "<YOUR-WRITE-API-KEY>"); + +const index = client.initIndex("<YOUR-INDEX-NAME>"); + +function transformFunction(hit) { + return { + ...hit, + foo: "bar", + }; +} + +async function transformObjects(batch, isDryRun) { + const newArr = batch.map((el) => { + return transformFunction(el); + }); + + // Dry run + if (isDryRun) { + console.log(newArr, "no objects were updated, this is a dry run"); + } + + // Real run + else { + try { + const data = await index.saveObjects(newArr); + const { objectIDs } = data; + console.log( + "The following objectIDs were successfully updated:", + objectIDs + ); + } catch (err) { + console.log("an error occurred updating the records", err); + } + } +} + +index + .browseObjects({ + query: "", + batch: (batch) => { + transformObjects(batch, false); + }, + }) + .then(() => console.log("code completed")); +``` |