Skip to content

Commit 095ccaf

Browse files
committed
[FEATURE] Update Algolia components to make them easier to understand and use
1 parent 7970203 commit 095ccaf

File tree

11 files changed

+414
-246
lines changed

11 files changed

+414
-246
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
if (!Array.isArray(recordIds)) {
37+
throw new Error("Record IDs must be an array");
38+
}
39+
40+
const response = await deleteRecords({
41+
indexName,
42+
objectIDs: utils.parseArray(recordIds),
43+
waitForTasks: true,
44+
});
45+
46+
$.export("$summary", "Successfully deleted records.");
47+
48+
return response;
49+
},
50+
};
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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,
50+
parseArrayAndMap: (value) => parseArray(value)?.map(valueToObject),
51+
};

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)