Skip to content

Commit 1c8e517

Browse files
committed
Mitra: new action components
1 parent 4feefb4 commit 1c8e517

File tree

6 files changed

+307
-4
lines changed

6 files changed

+307
-4
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import app from "../../mitra.app.mjs";
2+
3+
export default {
4+
key: "mitra-delete-data",
5+
name: "Delete Data",
6+
description: "Deletes a record from a table in the Mitra database. [See the documentation]()", // Add documentation link if available
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
tableName: {
12+
propDefinition: [
13+
app,
14+
"tableName",
15+
],
16+
},
17+
dimensionContentId: {
18+
type: "string",
19+
label: "Dimension Content ID",
20+
description: "The unique identifier of the record to delete.",
21+
},
22+
},
23+
methods: {
24+
deleteData({
25+
tableName, dimensionContentId, ...args
26+
} = {}) {
27+
return this.app.delete({
28+
tableName,
29+
path: `/${dimensionContentId}`,
30+
...args,
31+
});
32+
},
33+
},
34+
async run({ $ }) {
35+
const {
36+
deleteData,
37+
tableName,
38+
dimensionContentId,
39+
} = this;
40+
41+
const response = await deleteData({
42+
tableName,
43+
dimensionContentId,
44+
});
45+
46+
$.export("$summary", "Succesfully deleted record from the Mitra database.");
47+
return response;
48+
},
49+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import mitra from "../../mitra.app.mjs";
2+
3+
export default {
4+
key: "mitra-get-data",
5+
name: "Get Data",
6+
description: "Fetches data from the specified table, allowing dynamic filters via query parameters. [See the documentation](https://mitralab.io/docs/api)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
mitra,
11+
tableName: {
12+
propDefinition: [
13+
mitra,
14+
"tableName",
15+
],
16+
},
17+
params: {
18+
type: "object",
19+
label: "Query Parameters",
20+
description: "Dynamic filters for querying records (e.g., `status`, `hours_gt`).",
21+
},
22+
},
23+
methods: {
24+
fetchData({
25+
tableName, ...args
26+
} = {}) {
27+
return this.app._makeRequest({
28+
tableName,
29+
...args,
30+
});
31+
},
32+
},
33+
async run({ $ }) {
34+
const {
35+
fetchData,
36+
tableName,
37+
params,
38+
} = this;
39+
40+
const response = await fetchData({
41+
tableName,
42+
params,
43+
});
44+
$.export("$summary", "Succesfully fetched data from the database.");
45+
return response;
46+
},
47+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import utils from "../../common/utils.mjs";
2+
import app from "../../mitra.app.mjs";
3+
4+
export default {
5+
key: "mitra-insert-data",
6+
name: "Insert Data",
7+
description: "Inserts one or more records into a table in the Mitra database.",
8+
version: "0.0.7",
9+
type: "action",
10+
props: {
11+
app,
12+
tableName: {
13+
propDefinition: [
14+
app,
15+
"tableName",
16+
],
17+
},
18+
records: {
19+
propDefinition: [
20+
app,
21+
"records",
22+
],
23+
},
24+
},
25+
methods: {
26+
insertData({
27+
tableName, ...args
28+
} = {}) {
29+
return this.app.post({
30+
tableName,
31+
...args,
32+
});
33+
},
34+
},
35+
async run({ $ }) {
36+
const {
37+
insertData,
38+
tableName,
39+
records,
40+
} = this;
41+
42+
const response = await insertData({
43+
$,
44+
tableName,
45+
data: utils.parseArray(records),
46+
});
47+
48+
$.export("$summary", "Successfully inserted records into the Mitra database.");
49+
return response;
50+
},
51+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import app from "../../mitra.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "mitra-update-data",
6+
name: "Update Data",
7+
description: "Updates one or more records in a table.",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
tableName: {
13+
propDefinition: [
14+
app,
15+
"tableName",
16+
],
17+
},
18+
records: {
19+
propDefinition: [
20+
app,
21+
"records",
22+
],
23+
},
24+
},
25+
methods: {
26+
updateData({
27+
tableName, ...args
28+
} = {}) {
29+
return this.app.put({
30+
tableName,
31+
...args,
32+
});
33+
},
34+
},
35+
async run({ $ }) {
36+
const {
37+
updateData,
38+
tableName,
39+
records,
40+
} = this;
41+
42+
const response = await updateData({
43+
$,
44+
tableName,
45+
data: utils.parseArray(records),
46+
});
47+
48+
$.export("$summary", "Successfully updated records into the Mitra database.");
49+
return response;
50+
},
51+
};

components/mitra/common/utils.mjs

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+
try {
5+
JSON.parse(value);
6+
} catch (e) {
7+
return false;
8+
}
9+
10+
return true;
11+
}
12+
13+
function valueToObject(value) {
14+
if (typeof(value) === "object") {
15+
return value;
16+
}
17+
18+
if (!isJson(value)) {
19+
throw new ConfigurationError(`Make sure the custom expression contains a valid JSON object: \`${value}\``);
20+
}
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 array object");
45+
}
46+
}
47+
48+
export default {
49+
parseArray: (value) => parseArray(value).map(valueToObject),
50+
};

components/mitra/mitra.app.mjs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "mitra",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
tableName: {
8+
type: "string",
9+
label: "Table Name",
10+
description: "The name of the table in the database in case it was not set in the app.",
11+
optional: true,
12+
},
13+
records: {
14+
type: "string[]",
15+
label: "Records",
16+
description: "An array of records to insert or update, in JSON format.",
17+
},
18+
},
519
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
20+
getUrl(tableName, path) {
21+
const {
22+
url,
23+
table_name: defaultTableName,
24+
} = this.$auth;
25+
const baseUrl = `${url}/${tableName || defaultTableName}`;
26+
return path
27+
? `${baseUrl}${path}`
28+
: baseUrl;
29+
},
30+
getHeaders(headers) {
31+
return {
32+
...headers,
33+
"Content-Type": "application/json",
34+
"Authorization": `Bearer ${this.$auth.api_key}`,
35+
};
36+
},
37+
_makeRequest({
38+
$ = this, path, headers, tableName, ...args
39+
} = {}) {
40+
return axios($, {
41+
...args,
42+
debug: true,
43+
url: this.getUrl(tableName, path),
44+
headers: this.getHeaders(headers),
45+
});
46+
},
47+
post(args = {}) {
48+
return this._makeRequest({
49+
method: "POST",
50+
...args,
51+
});
52+
},
53+
put(args = {}) {
54+
return this._makeRequest({
55+
method: "PUT",
56+
...args,
57+
});
58+
},
59+
delete(args = {}) {
60+
return this._makeRequest({
61+
methot: "DELETE",
62+
...args,
63+
});
964
},
1065
},
1166
};

0 commit comments

Comments
 (0)