@kosha-ai/integration-postgres 0.7.0 → 0.8.1

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.
@@ -4,9 +4,7 @@ export declare class PostgresConnectionError extends Error {
4
4
  }
5
5
  /**
6
6
  * A Postgres database, reached directly over its own wire protocol rather
7
- * than proxied as HTTP — hence `query`, not `api`. Every call is read-only:
8
- * Kosha has no write path for this integration regardless of what the
9
- * connected database user is otherwise allowed to do.
7
+ * than proxied as HTTP — hence `query`, not `api`.
10
8
  */
11
9
  export declare function postgres(): KoshaIntegration;
12
10
  export default postgres;
@@ -1 +1 @@
1
- {"version":3,"file":"integration-postgres.d.ts","sourceRoot":"","sources":["../../src/lib/integration-postgres.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAIV,gBAAgB,EAEjB,MAAM,2BAA2B,CAAC;AAInC,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,OAAO,EAAE,MAAM;CAI5B;AAsKD;;;;;GAKG;AACH,wBAAgB,QAAQ,IAAI,gBAAgB,CAgE3C;AAED,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"integration-postgres.d.ts","sourceRoot":"","sources":["../../src/lib/integration-postgres.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAKV,gBAAgB,EAEjB,MAAM,2BAA2B,CAAC;AAInC,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,OAAO,EAAE,MAAM;CAI5B;AAwKD;;;GAGG;AACH,wBAAgB,QAAQ,IAAI,gBAAgB,CAsF3C;AAED,eAAe,QAAQ,CAAC"}
@@ -92,8 +92,8 @@ async function connectClient(target) {
92
92
  return plain;
93
93
  }
94
94
  /**
95
- * Opens a fresh connection, runs `fn` inside a read-only transaction, and
96
- * always rolls back — never commits — regardless of how `fn` ends.
95
+ * Opens a fresh connection and runs `fn` inside the configuration's selected
96
+ * transaction mode.
97
97
  *
98
98
  * Read-only is enforced by Postgres itself (`BEGIN READ ONLY` rejects
99
99
  * INSERT/UPDATE/DELETE/DDL/`SELECT ... FOR UPDATE` at the engine level), not
@@ -105,7 +105,7 @@ async function connectClient(target) {
105
105
  * should ever be able to share a session — every other credential this SDK
106
106
  * resolves is similarly per-call rather than cached.
107
107
  */
108
- async function withReadOnlyClient(fields, credential, fn) {
108
+ async function withQueryClient(fields, credential, accessMode, fn) {
109
109
  const target = targetOf(fields, credential);
110
110
  // Re-checked on every call, not only when the configuration was saved: a
111
111
  // hostname that answered publicly yesterday can point inside the network
@@ -119,23 +119,22 @@ async function withReadOnlyClient(fields, credential, fn) {
119
119
  throw new PostgresConnectionError(`Could not connect to ${target.host}:${target.port}/${target.database}: ${error instanceof Error ? error.message : String(error)}`);
120
120
  }
121
121
  try {
122
- await client.query('BEGIN READ ONLY');
122
+ await client.query(accessMode === 'readonly' ? 'BEGIN READ ONLY' : 'BEGIN');
123
123
  await client.query(`SET LOCAL statement_timeout = '${STATEMENT_TIMEOUT}'`);
124
- return await fn(client);
124
+ const result = await fn(client);
125
+ await client.query(accessMode === 'readwrite' ? 'COMMIT' : 'ROLLBACK');
126
+ return result;
125
127
  }
126
128
  finally {
127
- // Always rolled back, even on success: nothing this integration runs is
128
- // ever meant to persist, and a read-only transaction has nothing to
129
- // commit anyway.
129
+ // Roll back on errors and after a read-only transaction. After COMMIT this
130
+ // is harmless and keeps cleanup safe if the client state changes.
130
131
  await client.query('ROLLBACK').catch(() => undefined);
131
132
  await client.end();
132
133
  }
133
134
  }
134
135
  /**
135
136
  * A Postgres database, reached directly over its own wire protocol rather
136
- * than proxied as HTTP — hence `query`, not `api`. Every call is read-only:
137
- * Kosha has no write path for this integration regardless of what the
138
- * connected database user is otherwise allowed to do.
137
+ * than proxied as HTTP — hence `query`, not `api`.
139
138
  */
140
139
  export function postgres() {
141
140
  return defineIntegration({
@@ -158,11 +157,21 @@ export function postgres() {
158
157
  type: 'string',
159
158
  required: true,
160
159
  },
160
+ {
161
+ key: 'accessMode',
162
+ label: 'Access',
163
+ type: 'select',
164
+ options: [
165
+ { label: 'Read only', value: 'readonly' },
166
+ { label: 'Read and write', value: 'readwrite' },
167
+ ],
168
+ help: 'Defaults to read only. Read and write commits changes made by the query.',
169
+ },
161
170
  ],
162
171
  query: {
163
- readOnly: true,
164
- async run({ credential, fields, sql, params }) {
165
- return withReadOnlyClient(fields, credential, async (client) => {
172
+ accessModes: ['readonly', 'readwrite'],
173
+ async run({ credential, fields, accessMode = 'readonly', sql, params, }) {
174
+ return withQueryClient(fields, credential, accessMode, async (client) => {
166
175
  const result = await client.query({ text: sql, values: params });
167
176
  return {
168
177
  rows: result.rows,
@@ -181,7 +190,7 @@ export function postgres() {
181
190
  async fetchAccount({ accessToken, fields }) {
182
191
  const credential = { accessToken };
183
192
  const { host, database } = targetOf(fields, credential);
184
- const row = await withReadOnlyClient(fields, credential, async (client) => {
193
+ const row = await withQueryClient(fields, credential, 'readonly', async (client) => {
185
194
  const result = await client.query('SELECT current_user');
186
195
  return result.rows[0];
187
196
  });
package/docs/setup.md CHANGED
@@ -14,6 +14,8 @@ When you create the configuration, give Kosha:
14
14
  Kosha server network. A link-local address (`169.254.x.x`) is refused.
15
15
 
16
16
  - **Database** — the database name.
17
+ - **Access** — `readonly` by default, or `readwrite` when successful queries
18
+ should be committed.
17
19
 
18
20
  Connections are always encrypted in transit (TLS), but the server's
19
21
  certificate isn't verified against a CA — most managed Postgres providers
@@ -24,7 +26,7 @@ configurable yet.
24
26
 
25
27
  Postgres has no concept of "the workspace's key" the way Apollo's API key
26
28
  does, but the usage is the same in practice: one admin connects **one**
27
- read-only username and password, once, and every query Kosha runs
29
+ username and password, once, and every query Kosha runs
28
30
  afterwards — on behalf of however many people in the org — rides on that
29
31
  same connection. This is a fact about how organizations run this integration,
30
32
  not a different mechanism: Kosha's connect flow is always "one secret, one
@@ -57,13 +59,12 @@ Nothing stops you from connecting a second, different credential under a
57
59
  different owner later (per team, per environment, whatever you need) — it's
58
60
  just that most setups only ever need the one.
59
61
 
60
- ### Read-only, enforced by Postgres itself
62
+ ### Access mode, enforced by Postgres itself
61
63
 
62
- Every query Kosha runs through this connection happens inside a `BEGIN READ
63
- ONLY` transaction that is always rolled back, never committed. Postgres
64
- itself rejects `INSERT`/`UPDATE`/`DELETE`/DDL/`SELECT ... FOR UPDATE` inside
65
- that transaction — Kosha does not parse or filter the SQL text, so there is
66
- no way to word a write around it.
64
+ `readonly` configurations run inside a `BEGIN READ ONLY` transaction that is
65
+ always rolled back. `readwrite` configurations run inside a normal transaction
66
+ and commit successful queries. Postgres enforces the read-only mode itself;
67
+ Kosha does not parse or filter the SQL text.
67
68
 
68
69
  That guarantee doesn't bound _cost_: a read-only transaction can still run an
69
70
  expensive or endless `SELECT`. Kosha sets a 30-second statement timeout per
@@ -0,0 +1,6 @@
1
+ ALTER TABLE "postgres_configuration"
2
+ ADD COLUMN "accessMode" TEXT NOT NULL DEFAULT 'readonly';
3
+
4
+ ALTER TABLE "postgres_configuration"
5
+ ADD CONSTRAINT "postgres_configuration_accessMode_check"
6
+ CHECK ("accessMode" IN ('readonly', 'readwrite'));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kosha-ai/integration-postgres",
3
- "version": "0.7.0",
4
- "description": "Postgres integration for Kosha: read-only queries against a connected database",
3
+ "version": "0.8.1",
4
+ "description": "Postgres integration for Kosha: configurable read-only or read-write queries against a connected database",
5
5
  "keywords": [
6
6
  "kosha",
7
7
  "postgres",
@@ -48,10 +48,10 @@
48
48
  "key": "postgres"
49
49
  },
50
50
  "peerDependencies": {
51
- "@kosha-ai/integration-sdk": "0.7.0"
51
+ "@kosha-ai/integration-sdk": "0.8.1"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@types/pg": "^8.11.0",
55
- "@kosha-ai/integration-sdk": "0.7.0"
55
+ "@kosha-ai/integration-sdk": "0.8.1"
56
56
  }
57
57
  }