Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions components/algolia/actions/browse-records/browse-records.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import app from "../../algolia.app.mjs";

export default {
key: "algolia-browse-records",
name: "Browse Records",
description: "Browse for records in the given index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/methods/search/browse/?client=javascript).",
version: "0.0.1",
type: "action",
props: {
app,
indexName: {
propDefinition: [
app,
"indexName",
],
},
},
methods: {
browse(args) {
return this.app._client().browse(args);
},
},
async run({ $ }) {
const {
browse,
indexName,
} = this;

const response = await browse({
indexName,
});

$.export("$summary", `Successfully fetched records from ${indexName} index.`);

return response;
},
};
54 changes: 0 additions & 54 deletions components/algolia/actions/create-objects/create-objects.mjs

This file was deleted.

35 changes: 0 additions & 35 deletions components/algolia/actions/delete-objects/delete-objects.mjs

This file was deleted.

46 changes: 46 additions & 0 deletions components/algolia/actions/delete-records/delete-records.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import app from "../../algolia.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "algolia-delete-records",
name: "Delete Records",
description: "Delete records from the given index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/helpers/#delete-records)",
version: "0.0.1",
type: "action",
props: {
app,
indexName: {
propDefinition: [
app,
"indexName",
],
},
recordIds: {
type: "string[]",
label: "Record IDs",
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}}`.",
},
},
methods: {
deleteRecords(args = {}) {
return this.app._client().deleteObjects(args);
},
},
async run({ $ }) {
const {
deleteRecords,
indexName,
recordIds,
} = this;

const response = await deleteRecords({
indexName,
objectIDs: utils.parseArray(recordIds),
waitForTasks: true,
});

$.export("$summary", "Successfully deleted records.");

return response;
},
};
45 changes: 45 additions & 0 deletions components/algolia/actions/save-records/save-records.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import app from "../../algolia.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "algolia-save-records",
name: "Save Records",
description: "Adds records to an index. [See the documentation](https://www.algolia.com/doc/libraries/javascript/v5/helpers/#save-records).",
version: "0.0.1",
type: "action",
props: {
app,
indexName: {
propDefinition: [
app,
"indexName",
],
},
records: {
type: "string[]",
label: "Records From JSON Objects",
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}}`.",
},
},
methods: {
saveRecords(args = {}) {
return this.app._client().saveObjects(args);
},
},
async run({ $ }) {
const {
saveRecords,
indexName,
records,
} = this;

const response = await saveRecords({
indexName,
objects: utils.parseArrayAndMap(records),
waitForTasks: true,
});

$.export("$summary", "Successfully created records.");
return response;
},
};
22 changes: 3 additions & 19 deletions components/algolia/algolia.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export default {
description: "The name of the index",
type: "string",
async options() {
const indexes = await this.getIndexes();

const { items: indexes } = await this.listIndices();
return indexes.map((index) => index.name);
},
},
Expand All @@ -25,23 +24,8 @@ export default {
_client() {
return algoliasearch(this._applicationId(), this._apiKey());
},
_index(indexName) {
return this._client().initIndex(indexName);
},
async getIndexes() {
const response = await this._client().listIndices();

return response.items;
},
async createObjects({
indexName, objects, options,
}) {
return this._index(indexName).saveObjects(objects, options);
},
async deleteObjects({
indexName, objectIds,
}) {
return this._index(indexName).deleteObjects(objectIds);
listIndices() {
return this._client().listIndices();
},
},
};
52 changes: 52 additions & 0 deletions components/algolia/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ConfigurationError } from "@pipedream/platform";

const parseJson = (input) => {
const parse = (value) => {
if (typeof(value) === "string") {
try {
return parseJson(JSON.parse(value));
} catch (e) {
return value;
}
} else if (typeof(value) === "object" && value !== null) {
return Object.entries(value)
.reduce((acc, [
key,
val,
]) => Object.assign(acc, {
[key]: parse(val),
}), {});
}
return value;
};

return parse(input);
};

function parseArray(value) {
try {
if (!value) {
return [];
}

if (Array.isArray(value)) {
return value;
}

const parsedValue = JSON.parse(value);

if (!Array.isArray(parsedValue)) {
throw new Error("Not an array");
}

return parsedValue;

} catch (e) {
throw new ConfigurationError("Make sure the custom expression contains a valid JSON array object");
}
}

export default {
parseArray,
parseArrayAndMap: (value) => parseArray(value)?.map(parseJson),
};
4 changes: 2 additions & 2 deletions components/algolia/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/algolia",
"version": "0.0.5",
"version": "0.1.0",
"description": "Pipedream Algolia Components",
"main": "algolia.app.js",
"keywords": [
Expand All @@ -14,6 +14,6 @@
"access": "public"
},
"dependencies": {
"algoliasearch": "^4.13.1"
"algoliasearch": "^5.17.1"
}
}
Loading
Loading