Skip to content

Commit

Permalink
Mitra: new action components
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortes committed Oct 21, 2024
1 parent 4feefb4 commit 1c8e517
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 4 deletions.
49 changes: 49 additions & 0 deletions components/mitra/actions/delete-data/delete-data.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import app from "../../mitra.app.mjs";

export default {
key: "mitra-delete-data",
name: "Delete Data",
description: "Deletes a record from a table in the Mitra database. [See the documentation]()", // Add documentation link if available
version: "0.0.1",
type: "action",
props: {
app,
tableName: {
propDefinition: [
app,
"tableName",
],
},
dimensionContentId: {
type: "string",
label: "Dimension Content ID",
description: "The unique identifier of the record to delete.",
},
},
methods: {
deleteData({
tableName, dimensionContentId, ...args
} = {}) {
return this.app.delete({
tableName,
path: `/${dimensionContentId}`,
...args,
});
},
},
async run({ $ }) {
const {
deleteData,
tableName,
dimensionContentId,
} = this;

const response = await deleteData({
tableName,
dimensionContentId,
});

$.export("$summary", "Succesfully deleted record from the Mitra database.");
return response;
},
};
47 changes: 47 additions & 0 deletions components/mitra/actions/get-data/get-data.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import mitra from "../../mitra.app.mjs";

export default {
key: "mitra-get-data",
name: "Get Data",
description: "Fetches data from the specified table, allowing dynamic filters via query parameters. [See the documentation](https://mitralab.io/docs/api)",
version: "0.0.1",
type: "action",
props: {
mitra,
tableName: {
propDefinition: [
mitra,
"tableName",
],
},
params: {
type: "object",
label: "Query Parameters",
description: "Dynamic filters for querying records (e.g., `status`, `hours_gt`).",
},
},
methods: {
fetchData({
tableName, ...args
} = {}) {
return this.app._makeRequest({
tableName,
...args,
});
},
},
async run({ $ }) {
const {
fetchData,
tableName,
params,
} = this;

const response = await fetchData({
tableName,
params,
});
$.export("$summary", "Succesfully fetched data from the database.");
return response;
},
};
51 changes: 51 additions & 0 deletions components/mitra/actions/insert-data/insert-data.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import utils from "../../common/utils.mjs";
import app from "../../mitra.app.mjs";

export default {
key: "mitra-insert-data",
name: "Insert Data",
description: "Inserts one or more records into a table in the Mitra database.",
version: "0.0.7",
type: "action",
props: {
app,
tableName: {
propDefinition: [
app,
"tableName",
],
},
records: {
propDefinition: [
app,
"records",
],
},
},
methods: {
insertData({
tableName, ...args
} = {}) {
return this.app.post({
tableName,
...args,
});
},
},
async run({ $ }) {
const {
insertData,
tableName,
records,
} = this;

const response = await insertData({
$,
tableName,
data: utils.parseArray(records),
});

$.export("$summary", "Successfully inserted records into the Mitra database.");
return response;
},
};
51 changes: 51 additions & 0 deletions components/mitra/actions/update-data/update-data.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import app from "../../mitra.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "mitra-update-data",
name: "Update Data",
description: "Updates one or more records in a table.",
version: "0.0.1",
type: "action",
props: {
app,
tableName: {
propDefinition: [
app,
"tableName",
],
},
records: {
propDefinition: [
app,
"records",
],
},
},
methods: {
updateData({
tableName, ...args
} = {}) {
return this.app.put({
tableName,
...args,
});
},
},
async run({ $ }) {
const {
updateData,
tableName,
records,
} = this;

const response = await updateData({
$,
tableName,
data: utils.parseArray(records),
});

$.export("$summary", "Successfully updated records into the Mitra database.");
return response;
},
};
50 changes: 50 additions & 0 deletions components/mitra/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ConfigurationError } from "@pipedream/platform";

function isJson(value) {
try {
JSON.parse(value);
} catch (e) {
return false;
}

return true;
}

function valueToObject(value) {
if (typeof(value) === "object") {
return value;
}

if (!isJson(value)) {
throw new ConfigurationError(`Make sure the custom expression contains a valid JSON object: \`${value}\``);
}

return JSON.parse(value);
}

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 array object");
}
}

export default {
parseArray: (value) => parseArray(value).map(valueToObject),
};
63 changes: 59 additions & 4 deletions components/mitra/mitra.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,66 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "mitra",
propDefinitions: {},
propDefinitions: {
tableName: {
type: "string",
label: "Table Name",
description: "The name of the table in the database in case it was not set in the app.",
optional: true,
},
records: {
type: "string[]",
label: "Records",
description: "An array of records to insert or update, in JSON format.",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
getUrl(tableName, path) {
const {
url,
table_name: defaultTableName,
} = this.$auth;
const baseUrl = `${url}/${tableName || defaultTableName}`;
return path
? `${baseUrl}${path}`
: baseUrl;
},
getHeaders(headers) {
return {
...headers,
"Content-Type": "application/json",
"Authorization": `Bearer ${this.$auth.api_key}`,
};
},
_makeRequest({
$ = this, path, headers, tableName, ...args
} = {}) {
return axios($, {
...args,
debug: true,
url: this.getUrl(tableName, path),
headers: this.getHeaders(headers),
});
},
post(args = {}) {
return this._makeRequest({
method: "POST",
...args,
});
},
put(args = {}) {
return this._makeRequest({
method: "PUT",
...args,
});
},
delete(args = {}) {
return this._makeRequest({
methot: "DELETE",
...args,
});
},
},
};

0 comments on commit 1c8e517

Please sign in to comment.