blob: 6dbe91ef0434283a571ddf8b6c58af5d29ce070c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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"));
```
|