@kosha-ai/integration-clickhouse 0.0.0-stage → 0.7.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Codemancers
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/docs/setup.md ADDED
@@ -0,0 +1,123 @@
1
+ # ClickHouse setup
2
+
3
+ There is nothing to register with a provider. You need two things, and —
4
+ unlike an OAuth provider — both are usually entered by one admin, not by
5
+ each person who will end up running a query.
6
+
7
+ ### 1. The server
8
+
9
+ When you create the configuration, give Kosha:
10
+
11
+ - **URL** — the ClickHouse HTTP address, `https://host:port` or
12
+ `http://host:port` (e.g. `https://abc123.us-east-1.aws.clickhouse.cloud:8443`
13
+ or `http://ch.acme.internal:8123`). Any other scheme is refused when saved.
14
+ The port defaults to `8443` for https and `8123` for http, ClickHouse's HTTP
15
+ interface — not the native `9000`, which this integration does not support.
16
+
17
+ The hostname may be public or private; it only needs to resolve from the
18
+ Kosha server network. This supports private servers such as a self-hosted
19
+ ClickHouse in the same VPC.
20
+
21
+ `http` works only for a host that resolves to private addresses alone (e.g.
22
+ `10.x`, `192.168.x`, `localhost`): over plain HTTP the username and password
23
+ travel unencrypted, so Kosha refuses to send them to a public address.
24
+
25
+ - **Database** — the database queries run against when the SQL does not name
26
+ one, e.g. `default`.
27
+
28
+ ### 2. The credentials
29
+
30
+ Unlike GitHub or Slack, where each person signs in as themselves, a ClickHouse
31
+ connection is usually shared: an admin saves **one** ClickHouse username and
32
+ password, once, under a fixed owner such as `org`. Every query that names that
33
+ owner then runs as that database user, whoever in your app it is run for. Use
34
+ a dedicated, restricted user for this (see below).
35
+
36
+ Concretely, that's `POST /api/connect/credential` called once, with a fixed
37
+ placeholder `externalUserId` the admin picks (`"org"`, the org's own id,
38
+ anything stable) rather than a real person:
39
+
40
+ ```jsonc
41
+ POST /api/connect/credential
42
+ {
43
+ "provider": "clickhouse",
44
+ "configurationSlug": "analytics",
45
+ "externalUserId": "org",
46
+ "credential": { "username": "kosha_readonly", "password": "…" }
47
+ }
48
+ ```
49
+
50
+ Kosha signs in with it before storing anything (`SELECT currentUser()`), so a
51
+ wrong password is refused at this step rather than on the first query.
52
+
53
+ Every later query uses that same `org` owner, whichever person in your app it
54
+ is for. To check the connection, `SELECT 1 AS ok` works on any server and for
55
+ any user; for real data, query your own tables — `events` here stands for one
56
+ of yours, not a table ClickHouse creates:
57
+
58
+ ```jsonc
59
+ POST /api/query/clickhouse/analytics/org
60
+ { "sql": "SELECT event, count() AS n FROM events GROUP BY event" }
61
+ ```
62
+
63
+ The answer is `{ "rows": [...], "fields": [...], "rowCount": n }`. A
64
+ statement ClickHouse refuses answers `400` with ClickHouse's own reason.
65
+
66
+ The admin panel's **Test connect** stores the credential under the signed-in
67
+ admin (`kosha-admin:<user id>`) instead — fine for checking a configuration,
68
+ but your app should use the owner it chose above.
69
+
70
+ ### Parameters
71
+
72
+ ClickHouse binds named, typed parameters (`{id:UInt32}`), which a positional
73
+ array cannot describe, so a request carrying `params` is refused. Put the
74
+ values in the SQL itself.
75
+
76
+ ### Read-only, enforced by ClickHouse itself
77
+
78
+ ClickHouse has no transactions, so there is nothing to roll back. Instead
79
+ every request carries the setting `readonly=1`, which ClickHouse applies
80
+ before running the statement: `INSERT`, `ALTER`, `CREATE`, `DROP` and any
81
+ other write are refused by ClickHouse itself — Kosha does not parse or filter
82
+ the SQL text. `readonly=1` also stops the statement from changing settings,
83
+ so neither `SET readonly = 0` nor a `SETTINGS max_execution_time = …` clause
84
+ can lift the limits Kosha sends.
85
+
86
+ Each statement is limited to 30 seconds of execution
87
+ (`max_execution_time=30`), and every call opens and closes its own
88
+ connection.
89
+
90
+ A query may return at most 10,000 rows and 50 MB
91
+ (`max_result_rows`, `max_result_bytes`). A query that would return more is
92
+ refused with a `400` rather than cut short, so a result is never silently
93
+ incomplete. To read more, page through it in the SQL itself:
94
+
95
+ ```sql
96
+ SELECT * FROM events ORDER BY id LIMIT 1000 OFFSET 0
97
+ SELECT * FROM events ORDER BY id LIMIT 1000 OFFSET 1000
98
+ ```
99
+
100
+ ### Use a dedicated, restricted user
101
+
102
+ `readonly=1` stops writes, but it does not stop _reads_ ClickHouse can do on
103
+ the user's behalf — including table functions such as `url()`, `s3()` or
104
+ `remote()`, which make the ClickHouse server fetch from other hosts. What
105
+ the connected user may reach is the boundary, so connect a user created for
106
+ Kosha alone:
107
+
108
+ ```sql
109
+ CREATE USER kosha_readonly IDENTIFIED BY '…';
110
+ GRANT SELECT ON analytics.* TO kosha_readonly;
111
+ ```
112
+
113
+ Grant `SELECT` only on the databases Kosha should read, and nothing else.
114
+ Without the `URL`, `S3` or `REMOTE` privilege, ClickHouse refuses the table
115
+ functions above for this user, and it can read no other database.
116
+ Never connect `default` or an admin user, even though Kosha's own setting
117
+ would block its writes.
118
+
119
+ Do **not** give the user `SETTINGS readonly = 1` (or a profile that sets it):
120
+ a user locked to read-only cannot accept the execution limit Kosha sends with
121
+ each request, so every query would be refused with _Cannot modify
122
+ 'max_execution_time' setting in readonly mode_. Kosha already sends
123
+ `readonly=1` itself; the grants above are what limit the user.
@@ -0,0 +1,56 @@
1
+ CREATE TABLE "clickhouse_configuration" (
2
+ "id" TEXT NOT NULL,
3
+ "environmentId" TEXT NOT NULL,
4
+ "name" TEXT NOT NULL,
5
+ "slug" TEXT NOT NULL,
6
+ -- Encrypted, so TEXT rather than JSONB.
7
+ "credentials" TEXT,
8
+ -- The database this configuration points at. Operator-supplied, so
9
+ -- checked against link-local (cloud metadata) addresses before Kosha ever
10
+ -- connects to it.
11
+ -- "url" is the full HTTP address, `https://host:port` or
12
+ -- `http://host:port`. Plain http is only meant for a server on the same
13
+ -- private network as Kosha.
14
+ "url" TEXT NOT NULL,
15
+ "database" TEXT NOT NULL,
16
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
17
+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
18
+
19
+ CONSTRAINT "clickhouse_configuration_pkey" PRIMARY KEY ("id")
20
+ );
21
+
22
+ CREATE TABLE "clickhouse_connection" (
23
+ "accountId" TEXT NOT NULL,
24
+ -- Copied from `account` so the unique index below can use it.
25
+ "userId" TEXT NOT NULL,
26
+ "environmentId" TEXT NOT NULL,
27
+ "configurationId" TEXT NOT NULL,
28
+ "metadata" JSONB,
29
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
30
+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
31
+
32
+ CONSTRAINT "clickhouse_connection_pkey" PRIMARY KEY ("accountId")
33
+ );
34
+
35
+ CREATE UNIQUE INDEX "clickhouse_configuration_environmentId_slug_key" ON "clickhouse_configuration"("environmentId", "slug");
36
+
37
+ CREATE INDEX "clickhouse_configuration_environmentId_idx" ON "clickhouse_configuration"("environmentId");
38
+
39
+ CREATE UNIQUE INDEX "clickhouse_connection_environmentId_configurationId_userId_key" ON "clickhouse_connection"("environmentId", "configurationId", "userId");
40
+
41
+ CREATE INDEX "clickhouse_connection_environmentId_idx" ON "clickhouse_connection"("environmentId");
42
+
43
+ CREATE INDEX "clickhouse_connection_configurationId_idx" ON "clickhouse_connection"("configurationId");
44
+
45
+ CREATE INDEX "clickhouse_connection_userId_idx" ON "clickhouse_connection"("userId");
46
+
47
+ ALTER TABLE "clickhouse_configuration" ADD CONSTRAINT "clickhouse_configuration_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "environment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
48
+
49
+ ALTER TABLE "clickhouse_connection" ADD CONSTRAINT "clickhouse_connection_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
50
+
51
+ ALTER TABLE "clickhouse_connection" ADD CONSTRAINT "clickhouse_connection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
52
+
53
+ ALTER TABLE "clickhouse_connection" ADD CONSTRAINT "clickhouse_connection_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "environment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
54
+
55
+ -- RESTRICT: a configuration that still has connections cannot be deleted.
56
+ ALTER TABLE "clickhouse_connection" ADD CONSTRAINT "clickhouse_connection_configurationId_fkey" FOREIGN KEY ("configurationId") REFERENCES "clickhouse_configuration"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@kosha-ai/integration-clickhouse",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.7.0",
4
+ "description": "ClickHouse integration for Kosha: read-only queries against a connected ClickHouse server",
5
+ "keywords": [
6
+ "kosha",
7
+ "clickhouse",
8
+ "integrations"
9
+ ],
10
+ "license": "MIT",
11
+ "homepage": "https://github.com/codemancers/kosha#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/codemancers/kosha/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/codemancers/kosha.git",
18
+ "directory": "packages/integrations/clickhouse"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "type": "module",
24
+ "main": "./dist/index.js",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ "./package.json": "./package.json",
29
+ ".": {
30
+ "@org/source": "./src/index.ts",
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "default": "./dist/index.js"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "docs",
39
+ "migrations",
40
+ "!**/*.tsbuildinfo"
41
+ ],
42
+ "dependencies": {
43
+ "tslib": "^2.3.0",
44
+ "@clickhouse/client": "^1.23.1"
45
+ },
46
+ "kosha": {
47
+ "kind": "integration",
48
+ "key": "clickhouse"
49
+ },
50
+ "peerDependencies": {
51
+ "@kosha-ai/integration-sdk": "0.7.0"
52
+ },
53
+ "devDependencies": {
54
+ "@kosha-ai/integration-sdk": "0.7.0"
55
+ }
6
56
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.