postgresai 0.14.0-dev.8 → 0.14.0-dev.80

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.
Files changed (96) hide show
  1. package/README.md +161 -61
  2. package/bin/postgres-ai.ts +2596 -428
  3. package/bun.lock +258 -0
  4. package/bunfig.toml +20 -0
  5. package/dist/bin/postgres-ai.js +31218 -1575
  6. package/dist/sql/01.role.sql +16 -0
  7. package/dist/sql/02.extensions.sql +8 -0
  8. package/dist/sql/03.permissions.sql +38 -0
  9. package/dist/sql/04.optional_rds.sql +6 -0
  10. package/dist/sql/05.optional_self_managed.sql +8 -0
  11. package/dist/sql/06.helpers.sql +439 -0
  12. package/dist/sql/sql/01.role.sql +16 -0
  13. package/dist/sql/sql/02.extensions.sql +8 -0
  14. package/dist/sql/sql/03.permissions.sql +38 -0
  15. package/dist/sql/sql/04.optional_rds.sql +6 -0
  16. package/dist/sql/sql/05.optional_self_managed.sql +8 -0
  17. package/dist/sql/sql/06.helpers.sql +439 -0
  18. package/dist/sql/sql/uninit/01.helpers.sql +5 -0
  19. package/dist/sql/sql/uninit/02.permissions.sql +30 -0
  20. package/dist/sql/sql/uninit/03.role.sql +27 -0
  21. package/dist/sql/uninit/01.helpers.sql +5 -0
  22. package/dist/sql/uninit/02.permissions.sql +30 -0
  23. package/dist/sql/uninit/03.role.sql +27 -0
  24. package/lib/auth-server.ts +124 -106
  25. package/lib/checkup-api.ts +386 -0
  26. package/lib/checkup-dictionary.ts +113 -0
  27. package/lib/checkup.ts +1435 -0
  28. package/lib/config.ts +6 -3
  29. package/lib/init.ts +655 -189
  30. package/lib/issues.ts +848 -193
  31. package/lib/mcp-server.ts +391 -91
  32. package/lib/metrics-loader.ts +127 -0
  33. package/lib/supabase.ts +824 -0
  34. package/lib/util.ts +61 -0
  35. package/package.json +22 -10
  36. package/packages/postgres-ai/README.md +26 -0
  37. package/packages/postgres-ai/bin/postgres-ai.js +27 -0
  38. package/packages/postgres-ai/package.json +27 -0
  39. package/scripts/embed-checkup-dictionary.ts +106 -0
  40. package/scripts/embed-metrics.ts +154 -0
  41. package/sql/01.role.sql +16 -0
  42. package/sql/02.extensions.sql +8 -0
  43. package/sql/03.permissions.sql +38 -0
  44. package/sql/04.optional_rds.sql +6 -0
  45. package/sql/05.optional_self_managed.sql +8 -0
  46. package/sql/06.helpers.sql +439 -0
  47. package/sql/uninit/01.helpers.sql +5 -0
  48. package/sql/uninit/02.permissions.sql +30 -0
  49. package/sql/uninit/03.role.sql +27 -0
  50. package/test/auth.test.ts +258 -0
  51. package/test/checkup.integration.test.ts +321 -0
  52. package/test/checkup.test.ts +1116 -0
  53. package/test/config-consistency.test.ts +36 -0
  54. package/test/init.integration.test.ts +508 -0
  55. package/test/init.test.ts +916 -0
  56. package/test/issues.cli.test.ts +538 -0
  57. package/test/issues.test.ts +456 -0
  58. package/test/mcp-server.test.ts +1527 -0
  59. package/test/schema-validation.test.ts +81 -0
  60. package/test/supabase.test.ts +568 -0
  61. package/test/test-utils.ts +128 -0
  62. package/tsconfig.json +12 -20
  63. package/dist/bin/postgres-ai.d.ts +0 -3
  64. package/dist/bin/postgres-ai.d.ts.map +0 -1
  65. package/dist/bin/postgres-ai.js.map +0 -1
  66. package/dist/lib/auth-server.d.ts +0 -31
  67. package/dist/lib/auth-server.d.ts.map +0 -1
  68. package/dist/lib/auth-server.js +0 -263
  69. package/dist/lib/auth-server.js.map +0 -1
  70. package/dist/lib/config.d.ts +0 -45
  71. package/dist/lib/config.d.ts.map +0 -1
  72. package/dist/lib/config.js +0 -181
  73. package/dist/lib/config.js.map +0 -1
  74. package/dist/lib/init.d.ts +0 -64
  75. package/dist/lib/init.d.ts.map +0 -1
  76. package/dist/lib/init.js +0 -399
  77. package/dist/lib/init.js.map +0 -1
  78. package/dist/lib/issues.d.ts +0 -75
  79. package/dist/lib/issues.d.ts.map +0 -1
  80. package/dist/lib/issues.js +0 -336
  81. package/dist/lib/issues.js.map +0 -1
  82. package/dist/lib/mcp-server.d.ts +0 -9
  83. package/dist/lib/mcp-server.d.ts.map +0 -1
  84. package/dist/lib/mcp-server.js +0 -168
  85. package/dist/lib/mcp-server.js.map +0 -1
  86. package/dist/lib/pkce.d.ts +0 -32
  87. package/dist/lib/pkce.d.ts.map +0 -1
  88. package/dist/lib/pkce.js +0 -101
  89. package/dist/lib/pkce.js.map +0 -1
  90. package/dist/lib/util.d.ts +0 -27
  91. package/dist/lib/util.d.ts.map +0 -1
  92. package/dist/lib/util.js +0 -46
  93. package/dist/lib/util.js.map +0 -1
  94. package/dist/package.json +0 -46
  95. package/test/init.integration.test.cjs +0 -269
  96. package/test/init.test.cjs +0 -76
package/README.md CHANGED
@@ -10,11 +10,13 @@ Command-line interface for PostgresAI monitoring and database management.
10
10
  npm install -g postgresai
11
11
  ```
12
12
 
13
- Or install the latest alpha release explicitly:
13
+ Or install the latest beta release explicitly:
14
14
  ```bash
15
- npm install -g postgresai@alpha
15
+ npm install -g postgresai@beta
16
16
  ```
17
17
 
18
+ Note: in this repository, `cli/package.json` uses a placeholder version (`0.0.0-dev.0`). The real published version is set by the git tag in CI when publishing to npm.
19
+
18
20
  ### From Homebrew (macOS)
19
21
 
20
22
  ```bash
@@ -27,44 +29,114 @@ brew install postgresai
27
29
 
28
30
  ## Usage
29
31
 
30
- The CLI provides three command aliases:
32
+ The `postgresai` package provides two command aliases:
33
+ ```bash
34
+ postgresai --help # Canonical, discoverable
35
+ pgai --help # Short and convenient
36
+ ```
37
+
38
+ You can also run it without installing via `npx`:
39
+
40
+ ```bash
41
+ npx postgresai --help
42
+ ```
43
+
44
+ ### Optional shorthand: `pgai`
45
+
46
+ If you want `npx pgai ...` as a shorthand for `npx postgresai ...`, install the separate `pgai` wrapper package:
47
+
31
48
  ```bash
32
- postgres-ai --help
33
- postgresai --help
34
- pgai --help # short alias
49
+ npx pgai --help
35
50
  ```
36
51
 
37
- ## init (create monitoring user in Postgres)
52
+ ## prepare-db (create monitoring user in Postgres)
38
53
 
39
- This command creates (or updates) the `postgres_ai_mon` user and grants the permissions described in the root `README.md` (it is idempotent).
54
+ This command creates (or updates) the `postgres_ai_mon` user, creates the required view(s), and grants the permissions described in the root `README.md` (it is idempotent). Where supported, it also enables observability extensions described there.
40
55
 
41
56
  Run without installing (positional connection string):
42
57
 
43
58
  ```bash
44
- npx postgresai init postgresql://admin@host:5432/dbname
59
+ npx postgresai prepare-db postgresql://admin@host:5432/dbname
45
60
  ```
46
61
 
47
- It also accepts libpq conninfo syntax:
62
+ It also accepts libpq "conninfo" syntax:
48
63
 
49
64
  ```bash
50
- npx postgresai init "dbname=dbname host=host user=admin"
65
+ npx postgresai prepare-db "dbname=dbname host=host user=admin"
51
66
  ```
52
67
 
53
68
  And psql-like options:
54
69
 
55
70
  ```bash
56
- npx postgresai init -h host -p 5432 -U admin -d dbname
71
+ npx postgresai prepare-db -h host -p 5432 -U admin -d dbname
57
72
  ```
58
73
 
59
74
  Password input options (in priority order):
60
75
  - `--password <password>`
61
76
  - `PGAI_MON_PASSWORD` environment variable
62
- - if not provided: a strong password is generated automatically and printed once
77
+ - if not provided: a strong password is generated automatically
78
+
79
+ By default, the generated password is printed **only in interactive (TTY) mode**. In non-interactive mode, you must either provide the password explicitly, or opt-in to printing it:
80
+ - `--print-password` (dangerous in CI logs)
63
81
 
64
82
  Optional permissions (RDS/self-managed extras from the root `README.md`) are enabled by default. To skip them:
65
83
 
66
84
  ```bash
67
- npx postgresai init postgresql://admin@host:5432/dbname --skip-optional-permissions
85
+ npx postgresai prepare-db postgresql://admin@host:5432/dbname --skip-optional-permissions
86
+ ```
87
+
88
+ ### Print SQL / dry run
89
+
90
+ To see what SQL would be executed (passwords redacted by default):
91
+
92
+ ```bash
93
+ npx postgresai prepare-db postgresql://admin@host:5432/dbname --print-sql
94
+ ```
95
+
96
+ ### Supabase mode
97
+
98
+ For Supabase projects, you can use the Management API instead of direct PostgreSQL connections. This is useful when direct database access is restricted.
99
+
100
+ ```bash
101
+ # Using environment variables
102
+ export SUPABASE_ACCESS_TOKEN='your_management_api_token'
103
+ export SUPABASE_PROJECT_REF='your_project_ref'
104
+ npx postgresai prepare-db --supabase
105
+
106
+ # Using command-line options
107
+ npx postgresai prepare-db --supabase \
108
+ --supabase-access-token 'your_token' \
109
+ --supabase-project-ref 'your_project_ref'
110
+
111
+ # Auto-detect project ref from a Supabase database URL
112
+ npx postgresai prepare-db postgresql://postgres:password@db.abc123.supabase.co:5432/postgres \
113
+ --supabase --supabase-access-token 'your_token'
114
+ ```
115
+
116
+ The Supabase access token can be created at https://supabase.com/dashboard/account/tokens.
117
+
118
+ Options:
119
+ - `--supabase` - Enable Supabase Management API mode
120
+ - `--supabase-access-token <token>` - Supabase Management API access token (or use `SUPABASE_ACCESS_TOKEN` env var)
121
+ - `--supabase-project-ref <ref>` - Supabase project reference (or use `SUPABASE_PROJECT_REF` env var)
122
+
123
+ Notes:
124
+ - The project reference can be auto-detected from Supabase database URLs
125
+ - All standard options work with Supabase mode (`--verify`, `--print-sql`, `--skip-optional-permissions`, etc.)
126
+ - When using `--verify`, the tool checks if all required setup is in place
127
+
128
+ ### Verify and password reset
129
+
130
+ Verify that everything is configured as expected (no changes):
131
+
132
+ ```bash
133
+ npx postgresai prepare-db postgresql://admin@host:5432/dbname --verify
134
+ ```
135
+
136
+ Reset monitoring user password only (no other changes):
137
+
138
+ ```bash
139
+ npx postgresai prepare-db postgresql://admin@host:5432/dbname --reset-password --password 'new_password'
68
140
  ```
69
141
 
70
142
  ## Quick start
@@ -73,7 +145,7 @@ npx postgresai init postgresql://admin@host:5432/dbname --skip-optional-permissi
73
145
 
74
146
  Authenticate via browser to obtain API key:
75
147
  ```bash
76
- pgai auth
148
+ postgresai auth
77
149
  ```
78
150
 
79
151
  This will:
@@ -85,17 +157,17 @@ This will:
85
157
 
86
158
  Start monitoring with demo database:
87
159
  ```bash
88
- postgres-ai mon quickstart --demo
160
+ postgresai mon local-install --demo
89
161
  ```
90
162
 
91
163
  Start monitoring with your own database:
92
164
  ```bash
93
- postgres-ai mon quickstart --db-url postgresql://user:pass@host:5432/db
165
+ postgresai mon local-install --db-url postgresql://user:pass@host:5432/db
94
166
  ```
95
167
 
96
168
  Complete automated setup with API key and database:
97
169
  ```bash
98
- postgres-ai mon quickstart --api-key your_key --db-url postgresql://user:pass@host:5432/db -y
170
+ postgresai mon local-install --api-key your_key --db-url postgresql://user:pass@host:5432/db -y
99
171
  ```
100
172
 
101
173
  This will:
@@ -112,22 +184,22 @@ This will:
112
184
  #### Service lifecycle
113
185
  ```bash
114
186
  # Complete setup with various options
115
- postgres-ai mon quickstart # Interactive setup for production
116
- postgres-ai mon quickstart --demo # Demo mode with sample database
117
- postgres-ai mon quickstart --api-key <key> # Setup with API key
118
- postgres-ai mon quickstart --db-url <url> # Setup with database URL
119
- postgres-ai mon quickstart --api-key <key> --db-url <url> # Complete automated setup
120
- postgres-ai mon quickstart -y # Auto-accept all defaults
187
+ postgresai mon local-install # Interactive setup for production
188
+ postgresai mon local-install --demo # Demo mode with sample database
189
+ postgresai mon local-install --api-key <key> # Setup with API key
190
+ postgresai mon local-install --db-url <url> # Setup with database URL
191
+ postgresai mon local-install --api-key <key> --db-url <url> # Complete automated setup
192
+ postgresai mon local-install -y # Auto-accept all defaults
121
193
 
122
194
  # Service management
123
- postgres-ai mon start # Start monitoring services
124
- postgres-ai mon stop # Stop monitoring services
125
- postgres-ai mon restart [service] # Restart all or specific monitoring service
126
- postgres-ai mon status # Show monitoring services status
127
- postgres-ai mon health [--wait <sec>] # Check monitoring services health
195
+ postgresai mon start # Start monitoring services
196
+ postgresai mon stop # Stop monitoring services
197
+ postgresai mon restart [service] # Restart all or specific monitoring service
198
+ postgresai mon status # Show monitoring services status
199
+ postgresai mon health [--wait <sec>] # Check monitoring services health
128
200
  ```
129
201
 
130
- ##### Quickstart options
202
+ ##### local-install options
131
203
  - `--demo` - Demo mode with sample database (testing only, cannot use with --api-key)
132
204
  - `--api-key <key>` - Postgres AI API key for automated report uploads
133
205
  - `--db-url <url>` - PostgreSQL connection URL to monitor (format: `postgresql://user:pass@host:port/db`)
@@ -135,27 +207,27 @@ postgres-ai mon health [--wait <sec>] # Check monitoring services health
135
207
 
136
208
  #### Monitoring target databases (`mon targets` subgroup)
137
209
  ```bash
138
- postgres-ai mon targets list # List databases to monitor
139
- postgres-ai mon targets add <conn-string> <name> # Add database to monitor
140
- postgres-ai mon targets remove <name> # Remove monitoring target
141
- postgres-ai mon targets test <name> # Test target connectivity
210
+ postgresai mon targets list # List databases to monitor
211
+ postgresai mon targets add <conn-string> <name> # Add database to monitor
212
+ postgresai mon targets remove <name> # Remove monitoring target
213
+ postgresai mon targets test <name> # Test target connectivity
142
214
  ```
143
215
 
144
216
  #### Configuration and maintenance
145
217
  ```bash
146
- postgres-ai mon config # Show monitoring configuration
147
- postgres-ai mon update-config # Apply configuration changes
148
- postgres-ai mon update # Update monitoring stack
149
- postgres-ai mon reset [service] # Reset service data
150
- postgres-ai mon clean # Cleanup artifacts
151
- postgres-ai mon check # System readiness check
152
- postgres-ai mon shell <service> # Open shell to monitoring service
218
+ postgresai mon config # Show monitoring configuration
219
+ postgresai mon update-config # Apply configuration changes
220
+ postgresai mon update # Update monitoring stack
221
+ postgresai mon reset [service] # Reset service data
222
+ postgresai mon clean # Cleanup artifacts
223
+ postgresai mon check # System readiness check
224
+ postgresai mon shell <service> # Open shell to monitoring service
153
225
  ```
154
226
 
155
227
  ### MCP server (`mcp` group)
156
228
 
157
229
  ```bash
158
- pgai mcp start # Start MCP stdio server exposing tools
230
+ postgresai mcp start # Start MCP stdio server exposing tools
159
231
  ```
160
232
 
161
233
  Cursor configuration example (Settings → MCP):
@@ -164,7 +236,7 @@ Cursor configuration example (Settings → MCP):
164
236
  {
165
237
  "mcpServers": {
166
238
  "PostgresAI": {
167
- "command": "pgai",
239
+ "command": "postgresai",
168
240
  "args": ["mcp", "start"],
169
241
  "env": {
170
242
  "PGAI_API_BASE_URL": "https://postgres.ai/api/general/"
@@ -175,16 +247,16 @@ Cursor configuration example (Settings → MCP):
175
247
  ```
176
248
 
177
249
  Tools exposed:
178
- - list_issues: returns the same JSON as `pgai issues list`.
250
+ - list_issues: returns the same JSON as `postgresai issues list`.
179
251
  - view_issue: view a single issue with its comments (args: { issue_id, debug? })
180
252
  - post_issue_comment: post a comment (args: { issue_id, content, parent_comment_id?, debug? })
181
253
 
182
254
  ### Issues management (`issues` group)
183
255
 
184
256
  ```bash
185
- pgai issues list # List issues (shows: id, title, status, created_at)
186
- pgai issues view <issueId> # View issue details and comments
187
- pgai issues post_comment <issueId> <content> # Post a comment to an issue
257
+ postgresai issues list # List issues (shows: id, title, status, created_at)
258
+ postgresai issues view <issueId> # View issue details and comments
259
+ postgresai issues post_comment <issueId> <content> # Post a comment to an issue
188
260
  # Options:
189
261
  # --parent <uuid> Parent comment ID (for replies)
190
262
  # --debug Enable debug output
@@ -198,27 +270,27 @@ By default, issues commands print human-friendly YAML when writing to a terminal
198
270
  - Use `--json` to force JSON output:
199
271
 
200
272
  ```bash
201
- pgai issues list --json | jq '.[] | {id, title}'
273
+ postgresai issues list --json | jq '.[] | {id, title}'
202
274
  ```
203
275
 
204
276
  - Rely on auto-detection: when stdout is not a TTY (e.g., piped or redirected), output is JSON automatically:
205
277
 
206
278
  ```bash
207
- pgai issues view <issueId> > issue.json
279
+ postgresai issues view <issueId> > issue.json
208
280
  ```
209
281
 
210
282
  #### Grafana management
211
283
  ```bash
212
- postgres-ai mon generate-grafana-password # Generate new Grafana password
213
- postgres-ai mon show-grafana-credentials # Show Grafana credentials
284
+ postgresai mon generate-grafana-password # Generate new Grafana password
285
+ postgresai mon show-grafana-credentials # Show Grafana credentials
214
286
  ```
215
287
 
216
288
  ### Authentication and API key management
217
289
  ```bash
218
- postgres-ai auth # Authenticate via browser (recommended)
219
- postgres-ai add-key <key> # Manually store API key
220
- postgres-ai show-key # Show stored key (masked)
221
- postgres-ai remove-key # Remove stored key
290
+ postgresai auth # Authenticate via browser (OAuth)
291
+ postgresai auth --set-key <key> # Store API key directly
292
+ postgresai show-key # Show stored key (masked)
293
+ postgresai remove-key # Remove stored key
222
294
  ```
223
295
 
224
296
  ## Configuration
@@ -263,26 +335,33 @@ Normalization:
263
335
 
264
336
  ### Examples
265
337
 
266
- Linux/macOS (bash/zsh):
338
+ For production (uses default URLs):
339
+
340
+ ```bash
341
+ # Production auth - uses console.postgres.ai by default
342
+ postgresai auth --debug
343
+ ```
344
+
345
+ For staging/development environments:
267
346
 
268
347
  ```bash
348
+ # Linux/macOS (bash/zsh)
269
349
  export PGAI_API_BASE_URL=https://v2.postgres.ai/api/general/
270
350
  export PGAI_UI_BASE_URL=https://console-dev.postgres.ai
271
- pgai auth --debug
351
+ postgresai auth --debug
272
352
  ```
273
353
 
274
- Windows PowerShell:
275
-
276
354
  ```powershell
355
+ # Windows PowerShell
277
356
  $env:PGAI_API_BASE_URL = "https://v2.postgres.ai/api/general/"
278
357
  $env:PGAI_UI_BASE_URL = "https://console-dev.postgres.ai"
279
- pgai auth --debug
358
+ postgresai auth --debug
280
359
  ```
281
360
 
282
361
  Via CLI options (overrides env):
283
362
 
284
363
  ```bash
285
- pgai auth --debug \
364
+ postgresai auth --debug \
286
365
  --api-base-url https://v2.postgres.ai/api/general/ \
287
366
  --ui-base-url https://console-dev.postgres.ai
288
367
  ```
@@ -290,6 +369,27 @@ pgai auth --debug \
290
369
  Notes:
291
370
  - If `PGAI_UI_BASE_URL` is not set, the default is `https://console.postgres.ai`.
292
371
 
372
+ ## Development
373
+
374
+ ### Testing
375
+
376
+ The CLI uses [Bun](https://bun.sh/) as the test runner with built-in coverage reporting.
377
+
378
+ ```bash
379
+ # Run tests with coverage (default)
380
+ bun run test
381
+
382
+ # Run tests without coverage (faster iteration during development)
383
+ bun run test:fast
384
+
385
+ # Run tests with coverage and show report location
386
+ bun run test:coverage
387
+ ```
388
+
389
+ Coverage configuration is in `bunfig.toml`. Reports are generated in `coverage/` directory:
390
+ - `coverage/lcov-report/index.html` - HTML coverage report
391
+ - `coverage/lcov.info` - LCOV format for CI integration
392
+
293
393
  ## Requirements
294
394
 
295
395
  - Node.js 18 or higher