From 16f52b7bef745097f7076dde76715db378b54343 Mon Sep 17 00:00:00 2001 From: JJ Date: Wed, 19 Mar 2025 14:56:26 +0000 Subject: first commit --- content/snippets/browse-update-algolia-index.md | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 content/snippets/browse-update-algolia-index.md (limited to 'content/snippets/browse-update-algolia-index.md') 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("", ""); + +const index = client.initIndex(""); + +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")); +``` -- cgit v1.2.3