-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
307 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}, | ||
}, | ||
}; |