Skip to content

Commit d926c8d

Browse files
committed
[FEATURE] Update Algolia components to make them easier to understand and use
1 parent e0cadd6 commit d926c8d

File tree

9 files changed

+323
-241
lines changed

9 files changed

+323
-241
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import app from "../../algolia.app.mjs";
2+
3+
export default {
4+
key: "algolia-delete-records",
5+
name: "Delete Records",
6+
description: "Delete records from the given index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/helpers/#delete-records)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
indexName: {
12+
propDefinition: [
13+
app,
14+
"indexName",
15+
],
16+
},
17+
recordIds: {
18+
type: "string[]",
19+
label: "Record IDs",
20+
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}}`.",
21+
},
22+
},
23+
methods: {
24+
deleteRecords(args = {}) {
25+
return this.app._client().deleteObjects(args);
26+
},
27+
},
28+
async run({ $ }) {
29+
const {
30+
deleteRecords,
31+
indexName,
32+
recordIds,
33+
} = this;
34+
35+
if (!Array.isArray(recordIds)) {
36+
throw new Error("Record IDs must be an array");
37+
}
38+
39+
const response = await deleteRecords({
40+
indexName,
41+
objectIDs: recordIds,
42+
waitForTasks: true,
43+
});
44+
45+
$.export("$summary", "Successfully deleted records.");
46+
47+
return response;
48+
},
49+
};
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-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",
21+
description: "The records to add to the index. Each record should be a JSON object. Eg. `{\"objectID\": \"1\", \"name\": \"Jane Doe\"}`.",
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.parseArray(records),
39+
waitForTasks: true,
40+
});
41+
42+
$.export("$summary", "Successfully created records.");
43+
44+
return response;
45+
},
46+
};

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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
3+
function isJson(value) {
4+
value =
5+
typeof(value) !== "string"
6+
? JSON.stringify(value)
7+
: value;
8+
9+
try {
10+
value = JSON.parse(value);
11+
} catch (e) {
12+
return false;
13+
}
14+
15+
return typeof(value) === "object" && value !== null;
16+
}
17+
18+
function valueToObject(value) {
19+
if (!isJson(value)) {
20+
return value;
21+
}
22+
return JSON.parse(value);
23+
}
24+
25+
function parseArray(value) {
26+
try {
27+
if (!value) {
28+
return [];
29+
}
30+
31+
if (Array.isArray(value)) {
32+
return value;
33+
}
34+
35+
const parsedValue = JSON.parse(value);
36+
37+
if (!Array.isArray(parsedValue)) {
38+
throw new Error("Not an array");
39+
}
40+
41+
return parsedValue;
42+
43+
} catch (e) {
44+
throw new ConfigurationError("Make sure the custom expression contains a valid JSON array object");
45+
}
46+
}
47+
48+
export default {
49+
parseArray: (value) => parseArray(value)?.map(valueToObject),
50+
};

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)