Skip to content

Commit 1a006e0

Browse files
authored
[FEATURE] Update Algolia components to make them easier to understand and use (#14984)
1 parent 41bc31c commit 1a006e0

File tree

11 files changed

+407
-240
lines changed

11 files changed

+407
-240
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import app from "../../algolia.app.mjs";
2+
3+
export default {
4+
key: "algolia-browse-records",
5+
name: "Browse Records",
6+
description: "Browse for records in the given index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/methods/search/browse/?client=javascript).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
indexName: {
12+
propDefinition: [
13+
app,
14+
"indexName",
15+
],
16+
},
17+
},
18+
methods: {
19+
browse(args) {
20+
return this.app._client().browse(args);
21+
},
22+
},
23+
async run({ $ }) {
24+
const {
25+
browse,
26+
indexName,
27+
} = this;
28+
29+
const response = await browse({
30+
indexName,
31+
});
32+
33+
$.export("$summary", `Successfully fetched records from ${indexName} index.`);
34+
35+
return response;
36+
},
37+
};

components/algolia/actions/create-objects/create-objects.mjs

Lines changed: 0 additions & 54 deletions
This file was deleted.

components/algolia/actions/delete-objects/delete-objects.mjs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import app from "../../algolia.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "algolia-delete-records",
6+
name: "Delete Records",
7+
description: "Delete records from the given index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/helpers/#delete-records)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
indexName: {
13+
propDefinition: [
14+
app,
15+
"indexName",
16+
],
17+
},
18+
recordIds: {
19+
type: "string[]",
20+
label: "Record IDs",
21+
description: "IDs of the records to delete also known as `objectIDs`. Eg. `[\"1\", \"2\"]`. If you don't know the IDs, you can use the **Browse Records** action to find them, then map them and then use them here as a custom expression. Eg. `{{steps.map_records_to_objectids.$return_value}}`.",
22+
},
23+
},
24+
methods: {
25+
deleteRecords(args = {}) {
26+
return this.app._client().deleteObjects(args);
27+
},
28+
},
29+
async run({ $ }) {
30+
const {
31+
deleteRecords,
32+
indexName,
33+
recordIds,
34+
} = this;
35+
36+
const response = await deleteRecords({
37+
indexName,
38+
objectIDs: utils.parseArray(recordIds),
39+
waitForTasks: true,
40+
});
41+
42+
$.export("$summary", "Successfully deleted records.");
43+
44+
return response;
45+
},
46+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import app from "../../algolia.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "algolia-save-records",
6+
name: "Save Records",
7+
description: "Adds records to an index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/helpers/#save-records).",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
indexName: {
13+
propDefinition: [
14+
app,
15+
"indexName",
16+
],
17+
},
18+
records: {
19+
type: "string[]",
20+
label: "Records From JSON Objects",
21+
description: "The records to add to the index. Each record should be a JSON object. Eg. `{\"objectID\": \"1\", \"name\": \"Jane Doe\"}`. For a better user experience, you can use the [**CSV File To Objects**](https://pipedream.com/apps/helper-functions/actions/csv-file-to-objects) **Helper Function** action to convert a CSV file to an array of objects and then map the objects to the records field here as a **Custom Expression**. Eg. `{{steps.csv_file_to_objects.$return_value}}`.",
22+
},
23+
},
24+
methods: {
25+
saveRecords(args = {}) {
26+
return this.app._client().saveObjects(args);
27+
},
28+
},
29+
async run({ $ }) {
30+
const {
31+
saveRecords,
32+
indexName,
33+
records,
34+
} = this;
35+
36+
const response = await saveRecords({
37+
indexName,
38+
objects: utils.parseArrayAndMap(records),
39+
waitForTasks: true,
40+
});
41+
42+
$.export("$summary", "Successfully created records.");
43+
return response;
44+
},
45+
};

components/algolia/algolia.app.mjs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export default {
99
description: "The name of the index",
1010
type: "string",
1111
async options() {
12-
const indexes = await this.getIndexes();
13-
12+
const { items: indexes } = await this.listIndices();
1413
return indexes.map((index) => index.name);
1514
},
1615
},
@@ -25,23 +24,8 @@ export default {
2524
_client() {
2625
return algoliasearch(this._applicationId(), this._apiKey());
2726
},
28-
_index(indexName) {
29-
return this._client().initIndex(indexName);
30-
},
31-
async getIndexes() {
32-
const response = await this._client().listIndices();
33-
34-
return response.items;
35-
},
36-
async createObjects({
37-
indexName, objects, options,
38-
}) {
39-
return this._index(indexName).saveObjects(objects, options);
40-
},
41-
async deleteObjects({
42-
indexName, objectIds,
43-
}) {
44-
return this._index(indexName).deleteObjects(objectIds);
27+
listIndices() {
28+
return this._client().listIndices();
4529
},
4630
},
4731
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
3+
const parseJson = (input) => {
4+
const parse = (value) => {
5+
if (typeof(value) === "string") {
6+
try {
7+
return parseJson(JSON.parse(value));
8+
} catch (e) {
9+
return value;
10+
}
11+
} else if (typeof(value) === "object" && value !== null) {
12+
return Object.entries(value)
13+
.reduce((acc, [
14+
key,
15+
val,
16+
]) => Object.assign(acc, {
17+
[key]: parse(val),
18+
}), {});
19+
}
20+
return value;
21+
};
22+
23+
return parse(input);
24+
};
25+
26+
function parseArray(value) {
27+
try {
28+
if (!value) {
29+
return [];
30+
}
31+
32+
if (Array.isArray(value)) {
33+
return value;
34+
}
35+
36+
const parsedValue = JSON.parse(value);
37+
38+
if (!Array.isArray(parsedValue)) {
39+
throw new Error("Not an array");
40+
}
41+
42+
return parsedValue;
43+
44+
} catch (e) {
45+
throw new ConfigurationError("Make sure the custom expression contains a valid JSON array object");
46+
}
47+
}
48+
49+
export default {
50+
parseArray,
51+
parseArrayAndMap: (value) => parseArray(value)?.map(parseJson),
52+
};

components/algolia/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/algolia",
3-
"version": "0.0.5",
3+
"version": "0.1.0",
44
"description": "Pipedream Algolia Components",
55
"main": "algolia.app.js",
66
"keywords": [
@@ -14,6 +14,6 @@
1414
"access": "public"
1515
},
1616
"dependencies": {
17-
"algoliasearch": "^4.13.1"
17+
"algoliasearch": "^5.17.1"
1818
}
1919
}

0 commit comments

Comments
 (0)