@matimo/postgres 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matimo/postgres",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Postgres tools for Matimo",
5
5
  "type": "module",
6
6
  "files": [
@@ -9,12 +9,16 @@
9
9
  "definition.yaml"
10
10
  ],
11
11
  "dependencies": {
12
- "pg": "^8.18.0"
12
+ "pg": "^8.18.0",
13
+ "@matimo/core": "0.1.4"
13
14
  },
14
15
  "peerDependencies": {
15
- "matimo": "0.1.2"
16
+ "matimo": "0.1.4"
16
17
  },
17
18
  "devDependencies": {
18
19
  "@types/pg": "^8.6.6"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc"
19
23
  }
20
24
  }
@@ -19,7 +19,7 @@ parameters:
19
19
  execution:
20
20
  type: function
21
21
  # The function file path is resolved relative to this definition.yaml
22
- code: ./execute-sql.ts
22
+ code: ./execute-sql.js
23
23
  timeout: 30000
24
24
 
25
25
  authentication:
@@ -0,0 +1,61 @@
1
+ import { Client } from 'pg';
2
+ import { MatimoError, ErrorCode } from '@matimo/core/runtime';
3
+ export default async function (input) {
4
+ const sql = input.sql || '';
5
+ const params = input.params;
6
+ if (!sql || sql.trim().length === 0) {
7
+ throw new MatimoError('Missing SQL statement', ErrorCode.EXECUTION_FAILED, {
8
+ toolName: 'postgres-execute-sql',
9
+ });
10
+ }
11
+ // Build connection string from either MATIMO_POSTGRES_URL or separate env vars
12
+ const envUrl = process.env.MATIMO_POSTGRES_URL;
13
+ let connectionString = envUrl;
14
+ if (!connectionString) {
15
+ const host = process.env.MATIMO_POSTGRES_HOST;
16
+ const port = process.env.MATIMO_POSTGRES_PORT || '5432';
17
+ const user = process.env.MATIMO_POSTGRES_USER;
18
+ const password = process.env.MATIMO_POSTGRES_PASSWORD;
19
+ const database = process.env.MATIMO_POSTGRES_DB;
20
+ if (host && user && password && database) {
21
+ // Build a simple connection string. Do not log secrets.
22
+ connectionString = `postgresql://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}:${port}/${database}`;
23
+ }
24
+ }
25
+ if (!connectionString) {
26
+ throw new MatimoError('Postgres connection information not provided. Set MATIMO_POSTGRES_URL or MATIMO_POSTGRES_HOST/PORT/USER/PASSWORD/DB', ErrorCode.EXECUTION_FAILED, { toolName: 'postgres-execute-sql' });
27
+ }
28
+ const client = new Client({ connectionString });
29
+ try {
30
+ await client.connect();
31
+ const result = await client.query(sql, (params ?? []));
32
+ return { rows: result.rows, rowCount: result.rowCount };
33
+ }
34
+ catch (err) {
35
+ // Extract meaningful error message
36
+ const originalError = String(err.message || err);
37
+ const details = {
38
+ originalMessage: originalError,
39
+ };
40
+ // Check if it's a connection error vs query error
41
+ if (originalError.includes('ECONNREFUSED')) {
42
+ details.hint = 'Connection refused - is Postgres running at the configured host/port?';
43
+ }
44
+ else if (originalError.includes('role') && originalError.includes('does not exist')) {
45
+ details.hint = 'Database user does not exist - check MATIMO_POSTGRES_USER env var';
46
+ }
47
+ else if (originalError.includes('database') && originalError.includes('does not exist')) {
48
+ details.hint = 'Database does not exist - check MATIMO_POSTGRES_DB env var';
49
+ }
50
+ // Wrap errors to avoid leaking secrets
51
+ throw new MatimoError(`Postgres query failed: ${originalError}`, ErrorCode.EXECUTION_FAILED, {
52
+ toolName: 'postgres-execute-sql',
53
+ details,
54
+ });
55
+ }
56
+ finally {
57
+ await client.end().catch(() => {
58
+ // Ignore connection close errors
59
+ });
60
+ }
61
+ }
@@ -1,5 +1,5 @@
1
1
  import { Client } from 'pg';
2
- import { MatimoError, ErrorCode } from '@matimo/core';
2
+ import { MatimoError, ErrorCode } from '@matimo/core/runtime';
3
3
 
4
4
  export default async function (input: Record<string, unknown>) {
5
5
  const sql = (input.sql as string) || '';