Skip to content

Commit f891b6f

Browse files
Metabase - Add export query with format action (#19144)
* Metabase - Add export query with format action Add new action to export card queries with parameters in multiple formats (CSV, JSON, XLSX, API). This implements the /query/{export-format} endpoint which accepts parameters, resolving the critical blocker mentioned in issue #18970. Features: - Support for csv, json, xlsx, and api export formats - Parameters support for filtered queries - format_rows and pivot_results options - Follows existing Metabase component patterns Closes #18970 * Add error handling for JSON.parse in parameters Address CodeRabbit feedback: wrap JSON.parse in try-catch to provide clear error messages when users provide invalid JSON strings. * Bump patch version of dependent Metabase actions * Improve suggestion --------- Co-authored-by: Leo Vu <vunguyenhung@outlook.com>
1 parent 5db4929 commit f891b6f

File tree

8 files changed

+120
-6
lines changed

8 files changed

+120
-6
lines changed

components/metabase/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ The Metabase API opens a gateway to interact with Metabase programmatically, ena
77
This Metabase integration provides the following actions:
88

99
- **Run Query** - Execute a saved question/card and return the results
10-
- **Get Dashboard** - Retrieve dashboard information and its cards
10+
- **Export Query with Format** - Execute a saved question/card with parameters and export results in CSV, JSON, XLSX, or API format
11+
- **Get Dashboard** - Retrieve dashboard information and its cards
1112
- **Create Dashboard** - Create a new dashboard in Metabase
1213
- **Get Database** - Retrieve database information and metadata
1314

components/metabase/actions/create-dashboard/create-dashboard.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "metabase-create-dashboard",
66
name: "Create Dashboard",
77
description: "Create a new Dashboard. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apidashboard/post/api/dashboard/).",
8-
version: "0.0.3",
8+
version: "0.0.4",
99
annotations: {
1010
destructiveHint: false,
1111
openWorldHint: true,
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import app from "../../metabase.app.mjs";
2+
3+
export default {
4+
key: "metabase-export-query-with-format",
5+
name: "Export Query with Format",
6+
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).",
7+
version: "0.0.1",
8+
annotations: {
9+
destructiveHint: false,
10+
openWorldHint: true,
11+
readOnlyHint: true,
12+
},
13+
type: "action",
14+
props: {
15+
app,
16+
cardId: {
17+
propDefinition: [
18+
app,
19+
"cardId",
20+
],
21+
},
22+
exportFormat: {
23+
type: "string",
24+
label: "Export Format",
25+
description: "The format to export the query results in",
26+
options: [
27+
{
28+
label: "CSV",
29+
value: "csv",
30+
},
31+
{
32+
label: "JSON",
33+
value: "json",
34+
},
35+
{
36+
label: "XLSX",
37+
value: "xlsx",
38+
},
39+
{
40+
label: "API",
41+
value: "api",
42+
},
43+
],
44+
},
45+
formatRows: {
46+
type: "boolean",
47+
label: "Format Rows",
48+
description: "Whether to format rows for display",
49+
optional: true,
50+
default: false,
51+
},
52+
pivotResults: {
53+
type: "boolean",
54+
label: "Pivot Results",
55+
description: "Whether to pivot the results",
56+
optional: true,
57+
default: false,
58+
},
59+
parameters: {
60+
type: "string[]",
61+
label: "Parameters",
62+
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\"}`",
63+
optional: true,
64+
},
65+
},
66+
async run({ $ }) {
67+
const {
68+
app,
69+
cardId,
70+
exportFormat,
71+
formatRows,
72+
pivotResults,
73+
parameters,
74+
} = this;
75+
76+
// Parse parameters from JSON strings to objects
77+
const parsedParameters = parameters?.map((param, index) => {
78+
if (typeof param === "string") {
79+
try {
80+
return JSON.parse(param);
81+
} catch (error) {
82+
throw new Error(`Invalid JSON in parameter at index ${index}: ${error.message}`);
83+
}
84+
}
85+
return param;
86+
});
87+
88+
const response = await app.exportCardQuery({
89+
$,
90+
cardId,
91+
exportFormat,
92+
data: {
93+
format_rows: formatRows,
94+
pivot_results: pivotResults,
95+
...(parsedParameters && parsedParameters.length > 0 && {
96+
parameters: parsedParameters,
97+
}),
98+
},
99+
});
100+
101+
$.export("$summary", `Successfully exported query results as ${exportFormat.toUpperCase()}`);
102+
103+
return response;
104+
},
105+
};

components/metabase/actions/get-dashboard/get-dashboard.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "metabase-get-dashboard",
55
name: "Get Dashboard",
66
description: "Retrieve dashboard information and its cards. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apidashboard/get/api/dashboard/{id}).",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,

components/metabase/actions/get-database/get-database.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "metabase-get-database",
55
name: "Get Database",
66
description: "Retrieve database information. [See the documentation](https://www.metabase.com/docs/latest/api#tag/apidatabase/get/api/database/{id}).",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,

components/metabase/actions/run-query/run-query.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "metabase-run-query",
55
name: "Run Query",
66
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).",
7-
version: "0.0.3",
7+
version: "0.0.4",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,

components/metabase/metabase.app.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ export default {
140140
...args,
141141
});
142142
},
143+
exportCardQuery({
144+
cardId, exportFormat, ...args
145+
} = {}) {
146+
return this.post({
147+
path: `/card/${cardId}/query/${exportFormat}`,
148+
...args,
149+
});
150+
},
143151
createDashboard(args = {}) {
144152
return this.post({
145153
path: "/dashboard",

components/metabase/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/metabase",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Pipedream Metabase Components",
55
"main": "metabase.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)