metabase-cli 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +291 -0
- package/dist/index.d.mts +408 -0
- package/dist/index.d.ts +408 -0
- package/dist/index.js +735 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +675 -0
- package/dist/index.mjs.map +1 -0
- package/dist/metabase.js +1710 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CodingNinjas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# metabase-cli
|
|
2
|
+
|
|
3
|
+
Headless CLI and Node.js library for interacting with [Metabase](https://www.metabase.com/) instances. Manage profiles, run queries, and CRUD questions, dashboards, collections, and snippets — all from the terminal.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Global install
|
|
9
|
+
npm install -g metabase-cli
|
|
10
|
+
|
|
11
|
+
# Or run directly
|
|
12
|
+
npx metabase-cli --help
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Node.js 18+.
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# 1. Add a profile
|
|
21
|
+
metabase-cli profile add prod --domain https://metabase.example.com --email you@example.com --password secret
|
|
22
|
+
|
|
23
|
+
# 2. Login (fetches and caches your user info)
|
|
24
|
+
metabase-cli login
|
|
25
|
+
|
|
26
|
+
# 3. Run a query
|
|
27
|
+
metabase-cli query run --sql "SELECT * FROM users LIMIT 10" --db 1
|
|
28
|
+
|
|
29
|
+
# 4. Search for a dashboard
|
|
30
|
+
metabase-cli search "Revenue" --models dashboard
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Profiles
|
|
34
|
+
|
|
35
|
+
Profiles store connection details for your Metabase instances. Config is saved at `~/.metabase-cli/config.json`.
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Session auth (email/password)
|
|
39
|
+
metabase-cli profile add prod --domain https://metabase.example.com --email you@example.com --password secret
|
|
40
|
+
|
|
41
|
+
# API key auth
|
|
42
|
+
metabase-cli profile add staging --domain https://staging.metabase.example.com --api-key mb_xxxxx
|
|
43
|
+
|
|
44
|
+
# List profiles (* = active)
|
|
45
|
+
metabase-cli profile list
|
|
46
|
+
|
|
47
|
+
# Switch active profile
|
|
48
|
+
metabase-cli profile switch staging
|
|
49
|
+
|
|
50
|
+
# Show current profile
|
|
51
|
+
metabase-cli profile current
|
|
52
|
+
|
|
53
|
+
# Remove a profile
|
|
54
|
+
metabase-cli profile remove staging
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Commands
|
|
58
|
+
|
|
59
|
+
### Authentication
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
metabase-cli login # Login and cache session + user info
|
|
63
|
+
metabase-cli logout # End session
|
|
64
|
+
metabase-cli whoami # Show cached user info
|
|
65
|
+
metabase-cli whoami --refresh # Re-fetch user info from server
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Queries
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# Run SQL against a database
|
|
72
|
+
metabase-cli query run --sql "SELECT * FROM orders LIMIT 5" --db 1
|
|
73
|
+
|
|
74
|
+
# Output as JSON
|
|
75
|
+
metabase-cli query run --sql "SELECT count(*) FROM users" --db 1 --format json
|
|
76
|
+
|
|
77
|
+
# Output as CSV
|
|
78
|
+
metabase-cli query run --sql "SELECT * FROM products" --db 1 --format csv
|
|
79
|
+
|
|
80
|
+
# Select specific columns
|
|
81
|
+
metabase-cli query run --sql "SELECT * FROM users" --db 1 --columns "id,email,name"
|
|
82
|
+
|
|
83
|
+
# Limit rows
|
|
84
|
+
metabase-cli query run --sql "SELECT * FROM events" --db 1 --limit 100
|
|
85
|
+
|
|
86
|
+
# Export to file (format auto-detected from extension)
|
|
87
|
+
metabase-cli query run --sql "SELECT * FROM orders" --db 1 --output orders.csv
|
|
88
|
+
metabase-cli query run --sql "SELECT * FROM orders" --db 1 --output orders.xlsx
|
|
89
|
+
metabase-cli query run --sql "SELECT * FROM orders" --db 1 --output orders.json
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Questions (Saved Cards)
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# List questions
|
|
96
|
+
metabase-cli question list
|
|
97
|
+
metabase-cli question list --filter mine
|
|
98
|
+
|
|
99
|
+
# Show question details
|
|
100
|
+
metabase-cli question show 42
|
|
101
|
+
|
|
102
|
+
# Run a saved question
|
|
103
|
+
metabase-cli question run 42
|
|
104
|
+
metabase-cli question run 42 --format csv
|
|
105
|
+
|
|
106
|
+
# Run with parameters
|
|
107
|
+
metabase-cli question run 42 --params '{"start_date":"2025-01-01"}'
|
|
108
|
+
|
|
109
|
+
# Export a saved question to file
|
|
110
|
+
metabase-cli question run 42 --output results.csv
|
|
111
|
+
metabase-cli question run 42 --output results.xlsx
|
|
112
|
+
|
|
113
|
+
# Create a question
|
|
114
|
+
metabase-cli question create --name "Active Users" --sql "SELECT * FROM users WHERE active = true" --db 1 --collection 5
|
|
115
|
+
|
|
116
|
+
# Create with display type and visualization settings
|
|
117
|
+
metabase-cli question create --name "Revenue Trend" --sql "SELECT date, sum(amount) FROM orders GROUP BY date" --db 1 --display line --viz '{"graph.show_values":true}'
|
|
118
|
+
|
|
119
|
+
# Create with parameterized query (template tags)
|
|
120
|
+
metabase-cli question create --name "Users Since" --sql "SELECT * FROM users WHERE created_at >= {{start_date}}" --db 1 \
|
|
121
|
+
--template-tags '{"start_date":{"type":"date","name":"start_date","display-name":"Start Date","default":"2024-01-01"}}'
|
|
122
|
+
|
|
123
|
+
# Update a question (safe mode blocks if you're not the creator)
|
|
124
|
+
metabase-cli question update 42 --name "New Name"
|
|
125
|
+
metabase-cli question update 42 --display line --viz '{"graph.show_values":true}'
|
|
126
|
+
metabase-cli question update 42 --sql "SELECT ..." --unsafe # bypass safe mode
|
|
127
|
+
|
|
128
|
+
# Delete a question
|
|
129
|
+
metabase-cli question delete 42
|
|
130
|
+
metabase-cli question delete 42 --unsafe
|
|
131
|
+
|
|
132
|
+
# Copy a question
|
|
133
|
+
metabase-cli question copy 42 --name "Copy of Active Users" --collection 10
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Dashboards
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
metabase-cli dashboard list
|
|
140
|
+
metabase-cli dashboard show 7
|
|
141
|
+
metabase-cli dashboard create --name "Sales Overview" --collection 5
|
|
142
|
+
metabase-cli dashboard update 7 --name "Updated Name"
|
|
143
|
+
metabase-cli dashboard update 7 --name "..." --unsafe
|
|
144
|
+
metabase-cli dashboard delete 7
|
|
145
|
+
metabase-cli dashboard copy 7 --name "Sales Overview (copy)"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Collections
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
metabase-cli collection list
|
|
152
|
+
metabase-cli collection tree # Hierarchical view
|
|
153
|
+
metabase-cli collection show 5
|
|
154
|
+
metabase-cli collection items 5 # List items in collection
|
|
155
|
+
metabase-cli collection items 5 --models card # Only questions
|
|
156
|
+
metabase-cli collection create --name "Analytics" --parent 3
|
|
157
|
+
metabase-cli collection update 5 --name "New Name"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Databases, Tables & Fields
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
# Databases
|
|
164
|
+
metabase-cli database list
|
|
165
|
+
metabase-cli database show 1
|
|
166
|
+
metabase-cli database schemas 1
|
|
167
|
+
metabase-cli database tables 1 public # Tables in schema
|
|
168
|
+
|
|
169
|
+
# Tables
|
|
170
|
+
metabase-cli table show 15
|
|
171
|
+
metabase-cli table metadata 15 # Fields and types
|
|
172
|
+
metabase-cli table fks 15 # Foreign keys
|
|
173
|
+
|
|
174
|
+
# Fields
|
|
175
|
+
metabase-cli field show 100
|
|
176
|
+
metabase-cli field values 100 # Distinct values
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Search
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
metabase-cli search "revenue"
|
|
183
|
+
metabase-cli search "revenue" --models card,dashboard
|
|
184
|
+
metabase-cli search "users" --limit 5 --format json
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### SQL Snippets
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
metabase-cli snippet list
|
|
191
|
+
metabase-cli snippet show 3
|
|
192
|
+
metabase-cli snippet create --name "Active filter" --content "WHERE active = true"
|
|
193
|
+
metabase-cli snippet update 3 --content "WHERE active = true AND deleted_at IS NULL"
|
|
194
|
+
metabase-cli snippet update 3 --content "..." --unsafe
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Safe Mode
|
|
198
|
+
|
|
199
|
+
By default, **update** and **delete** operations are blocked if you are not the creator of the entity. This prevents accidentally modifying questions, dashboards, or snippets owned by other team members.
|
|
200
|
+
|
|
201
|
+
Safe mode compares the entity's `creator_id` against your cached user ID (set at login time — no extra API call per operation).
|
|
202
|
+
|
|
203
|
+
To bypass safe mode:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
# Per-command
|
|
207
|
+
metabase-cli question update 42 --name "..." --unsafe
|
|
208
|
+
|
|
209
|
+
# Via environment variable
|
|
210
|
+
METABASE_UNSAFE=1 metabase-cli question update 42 --name "..."
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Output Formats
|
|
214
|
+
|
|
215
|
+
All query commands support `--format`:
|
|
216
|
+
|
|
217
|
+
| Format | Description | Stdout | `--output` |
|
|
218
|
+
|---------|--------------------------|--------|------------|
|
|
219
|
+
| `table` | ASCII table (default) | Yes | Yes |
|
|
220
|
+
| `json` | Raw JSON | Yes | Yes |
|
|
221
|
+
| `csv` | Comma-separated values | Yes | Yes |
|
|
222
|
+
| `tsv` | Tab-separated values | Yes | Yes |
|
|
223
|
+
| `xlsx` | Excel spreadsheet | No | Yes |
|
|
224
|
+
|
|
225
|
+
### Exporting to Files
|
|
226
|
+
|
|
227
|
+
Use `--output <file>` to write results directly to a file. The format is auto-detected from the file extension:
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
metabase-cli query run --sql "SELECT * FROM orders" --db 1 --output orders.csv
|
|
231
|
+
metabase-cli question run 42 --output results.xlsx
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
When using `--output` with CSV, JSON, or XLSX formats, the CLI uses Metabase's native export API which **bypasses the 2000-row limit** — all rows are exported.
|
|
235
|
+
|
|
236
|
+
## Library Usage
|
|
237
|
+
|
|
238
|
+
The package also exports a programmatic API:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import { MetabaseClient, DatasetApi, CardApi } from "metabase-cli";
|
|
242
|
+
|
|
243
|
+
const client = new MetabaseClient({
|
|
244
|
+
name: "prod",
|
|
245
|
+
domain: "https://metabase.example.com",
|
|
246
|
+
auth: { method: "api-key", apiKey: "mb_xxxxx" },
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Run a query
|
|
250
|
+
const dataset = new DatasetApi(client);
|
|
251
|
+
const result = await dataset.queryNative(1, "SELECT * FROM users LIMIT 10");
|
|
252
|
+
console.log(result.data.rows);
|
|
253
|
+
|
|
254
|
+
// Export to CSV/JSON/XLSX (bypasses 2000-row limit)
|
|
255
|
+
const csvBuffer = await dataset.exportBinary(
|
|
256
|
+
{ type: "native", database: 1, native: { query: "SELECT * FROM orders" } },
|
|
257
|
+
"csv",
|
|
258
|
+
);
|
|
259
|
+
fs.writeFileSync("orders.csv", csvBuffer);
|
|
260
|
+
|
|
261
|
+
// Get a question
|
|
262
|
+
const cards = new CardApi(client);
|
|
263
|
+
const question = await cards.get(42);
|
|
264
|
+
console.log(question.name);
|
|
265
|
+
|
|
266
|
+
// Export a saved question
|
|
267
|
+
const xlsxBuffer = await cards.queryExportBinary(42, "xlsx");
|
|
268
|
+
fs.writeFileSync("results.xlsx", xlsxBuffer);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Security
|
|
272
|
+
|
|
273
|
+
Profile credentials are stored in `~/.metabase-cli/config.json`. The file is created with `0600` permissions (owner-only read/write), but **passwords are stored in plaintext**. For production use, prefer API key auth (`--api-key`) over email/password auth.
|
|
274
|
+
|
|
275
|
+
Do not commit or share your `~/.metabase-cli/config.json` file.
|
|
276
|
+
|
|
277
|
+
## Development
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
git clone <repo-url>
|
|
281
|
+
cd metabase-cli
|
|
282
|
+
npm install
|
|
283
|
+
npm run build # Build with tsup
|
|
284
|
+
npm run dev # Watch mode
|
|
285
|
+
npm run typecheck # Type checking
|
|
286
|
+
npm test # Run tests
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## License
|
|
290
|
+
|
|
291
|
+
MIT
|