diff --git a/components/metabase/README.md b/components/metabase/README.md index 01309df1c92f7..df5026849dcb7 100644 --- a/components/metabase/README.md +++ b/components/metabase/README.md @@ -7,7 +7,8 @@ The Metabase API opens a gateway to interact with Metabase programmatically, ena This Metabase integration provides the following actions: - **Run Query** - Execute a saved question/card and return the results -- **Get Dashboard** - Retrieve dashboard information and its cards +- **Export Query with Format** - Execute a saved question/card with parameters and export results in CSV, JSON, XLSX, or API format +- **Get Dashboard** - Retrieve dashboard information and its cards - **Create Dashboard** - Create a new dashboard in Metabase - **Get Database** - Retrieve database information and metadata diff --git a/components/metabase/actions/create-dashboard/create-dashboard.mjs b/components/metabase/actions/create-dashboard/create-dashboard.mjs index fb08d7eecd259..632ad3e23c93c 100644 --- a/components/metabase/actions/create-dashboard/create-dashboard.mjs +++ b/components/metabase/actions/create-dashboard/create-dashboard.mjs @@ -5,7 +5,7 @@ export default { key: "metabase-create-dashboard", name: "Create Dashboard", description: "Create a new Dashboard. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apidashboard/post/api/dashboard/).", - version: "0.0.3", + version: "0.0.4", annotations: { destructiveHint: false, openWorldHint: true, diff --git a/components/metabase/actions/export-query-with-format/export-query-with-format.mjs b/components/metabase/actions/export-query-with-format/export-query-with-format.mjs new file mode 100644 index 0000000000000..c2da255c8811e --- /dev/null +++ b/components/metabase/actions/export-query-with-format/export-query-with-format.mjs @@ -0,0 +1,105 @@ +import app from "../../metabase.app.mjs"; + +export default { + key: "metabase-export-query-with-format", + name: "Export Query with Format", + description: "Execute a saved question/card with parameters and export results in the specified format (CSV, JSON, XLSX, or API). [See the documentation](https://www.metabase.com/docs/latest/api#tag/apicard/post/api/card/%7Bcard-id%7D/query).", + version: "0.0.1", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + type: "action", + props: { + app, + cardId: { + propDefinition: [ + app, + "cardId", + ], + }, + exportFormat: { + type: "string", + label: "Export Format", + description: "The format to export the query results in", + options: [ + { + label: "CSV", + value: "csv", + }, + { + label: "JSON", + value: "json", + }, + { + label: "XLSX", + value: "xlsx", + }, + { + label: "API", + value: "api", + }, + ], + }, + formatRows: { + type: "boolean", + label: "Format Rows", + description: "Whether to format rows for display", + optional: true, + default: false, + }, + pivotResults: { + type: "boolean", + label: "Pivot Results", + description: "Whether to pivot the results", + optional: true, + default: false, + }, + parameters: { + type: "string[]", + label: "Parameters", + description: "Query parameters as JSON objects. Each parameter should be a JSON string with the parameter properties. Example: `{\"type\": \"category\", \"target\": [\"variable\", [\"template-tag\", \"parameter_name\"]], \"value\": \"your_value\"}`", + optional: true, + }, + }, + async run({ $ }) { + const { + app, + cardId, + exportFormat, + formatRows, + pivotResults, + parameters, + } = this; + + // Parse parameters from JSON strings to objects + const parsedParameters = parameters?.map((param, index) => { + if (typeof param === "string") { + try { + return JSON.parse(param); + } catch (error) { + throw new Error(`Invalid JSON in parameter at index ${index}: ${error.message}`); + } + } + return param; + }); + + const response = await app.exportCardQuery({ + $, + cardId, + exportFormat, + data: { + format_rows: formatRows, + pivot_results: pivotResults, + ...(parsedParameters && parsedParameters.length > 0 && { + parameters: parsedParameters, + }), + }, + }); + + $.export("$summary", `Successfully exported query results as ${exportFormat.toUpperCase()}`); + + return response; + }, +}; diff --git a/components/metabase/actions/get-dashboard/get-dashboard.mjs b/components/metabase/actions/get-dashboard/get-dashboard.mjs index c05f4c4496d52..78425c7823c30 100644 --- a/components/metabase/actions/get-dashboard/get-dashboard.mjs +++ b/components/metabase/actions/get-dashboard/get-dashboard.mjs @@ -4,7 +4,7 @@ export default { key: "metabase-get-dashboard", name: "Get Dashboard", description: "Retrieve dashboard information and its cards. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apidashboard/get/api/dashboard/{id}).", - version: "0.0.3", + version: "0.0.4", annotations: { destructiveHint: false, openWorldHint: true, diff --git a/components/metabase/actions/get-database/get-database.mjs b/components/metabase/actions/get-database/get-database.mjs index f37f11af5ab95..ceb75378f81e6 100644 --- a/components/metabase/actions/get-database/get-database.mjs +++ b/components/metabase/actions/get-database/get-database.mjs @@ -4,7 +4,7 @@ export default { key: "metabase-get-database", name: "Get Database", description: "Retrieve database information. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apidatabase/get/api/database/{id}).", - version: "0.0.3", + version: "0.0.4", annotations: { destructiveHint: false, openWorldHint: true, diff --git a/components/metabase/actions/run-query/run-query.mjs b/components/metabase/actions/run-query/run-query.mjs index 2ba4e96efdf17..e40619aa68c74 100644 --- a/components/metabase/actions/run-query/run-query.mjs +++ b/components/metabase/actions/run-query/run-query.mjs @@ -4,7 +4,7 @@ export default { key: "metabase-run-query", name: "Run Query", description: "Execute a saved question/card and return the results. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apicard/post/api/card/{card-id}/query).", - version: "0.0.3", + version: "0.0.4", annotations: { destructiveHint: false, openWorldHint: true, diff --git a/components/metabase/metabase.app.mjs b/components/metabase/metabase.app.mjs index 77bdeae72509b..e117f3f5cdbfb 100644 --- a/components/metabase/metabase.app.mjs +++ b/components/metabase/metabase.app.mjs @@ -140,6 +140,14 @@ export default { ...args, }); }, + exportCardQuery({ + cardId, exportFormat, ...args + } = {}) { + return this.post({ + path: `/card/${cardId}/query/${exportFormat}`, + ...args, + }); + }, createDashboard(args = {}) { return this.post({ path: "/dashboard", diff --git a/components/metabase/package.json b/components/metabase/package.json index 36c3cf08f99ae..507525bc07170 100644 --- a/components/metabase/package.json +++ b/components/metabase/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/metabase", - "version": "0.1.1", + "version": "0.2.0", "description": "Pipedream Metabase Components", "main": "metabase.app.mjs", "keywords": [