@kosha-ai/integration-clickhouse 0.0.0-stage → 0.8.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 +21 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/integration-clickhouse.d.ts +13 -0
- package/dist/lib/integration-clickhouse.d.ts.map +1 -0
- package/dist/lib/integration-clickhouse.js +215 -0
- package/docs/setup.md +123 -0
- package/migrations/001_init/migration.sql +56 -0
- package/package.json +53 -3
- package/README.md +0 -3
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,UAAU,IAAI,OAAO,EACrB,yBAAyB,GAC1B,MAAM,iCAAiC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { clickhouse, clickhouse as default, ClickHouseConnectionError, } from './lib/integration-clickhouse.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { KoshaIntegration } from '@kosha-ai/integration-sdk';
|
|
2
|
+
export declare class ClickHouseConnectionError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* A ClickHouse server, reached over its HTTP interface — but as a database,
|
|
7
|
+
* not a REST API, hence `query` rather than `api`. Every call is read-only:
|
|
8
|
+
* Kosha has no write path for this integration regardless of what the
|
|
9
|
+
* connected ClickHouse user is otherwise allowed to do.
|
|
10
|
+
*/
|
|
11
|
+
export declare function clickhouse(): KoshaIntegration;
|
|
12
|
+
export default clickhouse;
|
|
13
|
+
//# sourceMappingURL=integration-clickhouse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integration-clickhouse.d.ts","sourceRoot":"","sources":["../../src/lib/integration-clickhouse.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAIV,gBAAgB,EAEjB,MAAM,2BAA2B,CAAC;AAInC,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,OAAO,EAAE,MAAM;CAI5B;AA6KD;;;;;GAKG;AACH,wBAAgB,UAAU,IAAI,gBAAgB,CAuF7C;AAED,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { ClickHouseLogLevel, createClient, } from '@clickhouse/client';
|
|
2
|
+
import { assertAllowedHost, assertPrivateHost, basic as basicAuth, defineIntegration, packageDirs, } from '@kosha-ai/integration-sdk';
|
|
3
|
+
const { docsPath, migrationsPath } = packageDirs(import.meta.url);
|
|
4
|
+
export class ClickHouseConnectionError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'ClickHouseConnectionError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const DEFAULT_PORTS = { https: 8443, http: 8123 };
|
|
11
|
+
const MAX_EXECUTION_TIME_SECONDS = 30;
|
|
12
|
+
// A little longer than the server-side limit, so a slow query reports
|
|
13
|
+
// ClickHouse's own timeout error rather than the client giving up first.
|
|
14
|
+
const REQUEST_TIMEOUT_MS = (MAX_EXECUTION_TIME_SECONDS + 5) * 1000;
|
|
15
|
+
// The whole result is held in Kosha's memory before it is returned, so a
|
|
16
|
+
// query that would return more is refused by ClickHouse rather than loaded.
|
|
17
|
+
const MAX_RESULT_ROWS = 10_000;
|
|
18
|
+
const MAX_RESULT_BYTES = 50 * 1024 * 1024;
|
|
19
|
+
function required(fields, key) {
|
|
20
|
+
const value = fields?.[key];
|
|
21
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
22
|
+
throw new ClickHouseConnectionError(`${key} is missing from this configuration`);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* `accessToken` is `base64(username:password)` — the same encoding
|
|
28
|
+
* `envelopeForCredential`'s `BASIC` case in the server produces for every
|
|
29
|
+
* Basic-auth integration.
|
|
30
|
+
*/
|
|
31
|
+
function decodeCredential(credential) {
|
|
32
|
+
const [username, ...rest] = Buffer.from(credential.accessToken, 'base64')
|
|
33
|
+
.toString('utf8')
|
|
34
|
+
.split(':');
|
|
35
|
+
if (!username) {
|
|
36
|
+
throw new ClickHouseConnectionError('Credential did not decode to a username and password');
|
|
37
|
+
}
|
|
38
|
+
return { username, password: rest.join(':') };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `url` is the server's full HTTP address, `https://host:port` or
|
|
42
|
+
* `http://host:port`. The server already refused any other scheme when the
|
|
43
|
+
* configuration was saved (a `url` field); it is checked again here because a
|
|
44
|
+
* row saved before that check, or written straight to the table, never went
|
|
45
|
+
* through it.
|
|
46
|
+
*/
|
|
47
|
+
function parseUrl(raw) {
|
|
48
|
+
let url;
|
|
49
|
+
try {
|
|
50
|
+
url = new URL(raw.trim());
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
throw new ClickHouseConnectionError(`url must be a full address such as https://host:8443, not ${raw}`);
|
|
54
|
+
}
|
|
55
|
+
const protocol = url.protocol.slice(0, -1);
|
|
56
|
+
if (protocol !== 'https' && protocol !== 'http') {
|
|
57
|
+
throw new ClickHouseConnectionError(`url must start with https:// or http://, not ${raw}`);
|
|
58
|
+
}
|
|
59
|
+
// `URL` drops a port that equals the scheme's own default (443, 80), so read
|
|
60
|
+
// it back from what was written before falling back to ClickHouse's ports.
|
|
61
|
+
const authority = raw
|
|
62
|
+
.trim()
|
|
63
|
+
.replace(/^[a-z]+:\/\//i, '')
|
|
64
|
+
.split(/[/?#]/)[0];
|
|
65
|
+
const written = url.port || /:(\d+)$/.exec(authority)?.[1];
|
|
66
|
+
return {
|
|
67
|
+
protocol,
|
|
68
|
+
// An IPv6 literal comes back bracketed; the host checks want it bare.
|
|
69
|
+
host: url.hostname.replace(/^\[|\]$/g, ''),
|
|
70
|
+
port: written ? Number(written) : DEFAULT_PORTS[protocol],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function targetOf(fields, credential) {
|
|
74
|
+
const { username, password } = decodeCredential(credential);
|
|
75
|
+
const { protocol, host, port } = parseUrl(required(fields, 'url'));
|
|
76
|
+
const urlHost = host.includes(':') ? `[${host}]` : host;
|
|
77
|
+
return {
|
|
78
|
+
url: `${protocol}://${urlHost}:${port}`,
|
|
79
|
+
protocol,
|
|
80
|
+
host,
|
|
81
|
+
database: required(fields, 'database'),
|
|
82
|
+
username,
|
|
83
|
+
password,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Opens a fresh client, hands it to `fn`, and always closes it.
|
|
88
|
+
*
|
|
89
|
+
* Read-only is enforced by ClickHouse itself: every request carries
|
|
90
|
+
* `readonly=1`, which the server applies before running the statement and
|
|
91
|
+
* which rejects INSERT/ALTER/CREATE/DROP — Kosha never parses the SQL text.
|
|
92
|
+
* `1` rather than `2` because `1` also forbids the query from changing any
|
|
93
|
+
* setting itself, so a `SETTINGS max_execution_time=…` clause cannot lift
|
|
94
|
+
* the timeout sent alongside it (settings on the request are still
|
|
95
|
+
* accepted). ClickHouse has no transactions to roll back, so this setting is
|
|
96
|
+
* the whole of the guarantee.
|
|
97
|
+
*
|
|
98
|
+
* No connection pooling across calls: every call builds and closes its own
|
|
99
|
+
* client, so no two calls — or tenants — ever share one.
|
|
100
|
+
*/
|
|
101
|
+
async function withReadOnlyClient(fields, credential, fn) {
|
|
102
|
+
const target = targetOf(fields, credential);
|
|
103
|
+
// Re-checked on every call, not only when the configuration was saved: a
|
|
104
|
+
// hostname that resolved yesterday may not today. Over http the username
|
|
105
|
+
// and password travel in the clear, so http is refused unless every address
|
|
106
|
+
// the host resolves to is private.
|
|
107
|
+
await assertAllowedHost(target.host);
|
|
108
|
+
if (target.protocol === 'http')
|
|
109
|
+
await assertPrivateHost(target.host);
|
|
110
|
+
const client = createClient({
|
|
111
|
+
url: target.url,
|
|
112
|
+
username: target.username,
|
|
113
|
+
password: target.password,
|
|
114
|
+
database: target.database,
|
|
115
|
+
request_timeout: REQUEST_TIMEOUT_MS,
|
|
116
|
+
// A refused query is the caller's to read, in the response Kosha sends
|
|
117
|
+
// back; left on, the client also prints every one as an error, stack and
|
|
118
|
+
// all, to the server's log.
|
|
119
|
+
log: { level: ClickHouseLogLevel.OFF },
|
|
120
|
+
clickhouse_settings: {
|
|
121
|
+
readonly: '1',
|
|
122
|
+
max_execution_time: MAX_EXECUTION_TIME_SECONDS,
|
|
123
|
+
// `throw`, not `break`: a cut-off result would look complete.
|
|
124
|
+
max_result_rows: String(MAX_RESULT_ROWS),
|
|
125
|
+
max_result_bytes: String(MAX_RESULT_BYTES),
|
|
126
|
+
result_overflow_mode: 'throw',
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
return await fn(client);
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
await client.close().catch(() => undefined);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A ClickHouse server, reached over its HTTP interface — but as a database,
|
|
138
|
+
* not a REST API, hence `query` rather than `api`. Every call is read-only:
|
|
139
|
+
* Kosha has no write path for this integration regardless of what the
|
|
140
|
+
* connected ClickHouse user is otherwise allowed to do.
|
|
141
|
+
*/
|
|
142
|
+
export function clickhouse() {
|
|
143
|
+
return defineIntegration({
|
|
144
|
+
key: 'clickhouse',
|
|
145
|
+
name: 'ClickHouse',
|
|
146
|
+
docsPath,
|
|
147
|
+
migrationsPath,
|
|
148
|
+
auth: basicAuth(),
|
|
149
|
+
configFields: [
|
|
150
|
+
{
|
|
151
|
+
key: 'url',
|
|
152
|
+
label: 'URL',
|
|
153
|
+
type: 'url',
|
|
154
|
+
required: true,
|
|
155
|
+
help: 'The ClickHouse HTTP address, https://host:port or http://host:port (the port defaults to 8443 for https, 8123 for http), e.g. https://abc123.us-east-1.aws.clickhouse.cloud:8443. http is refused unless the host resolves only to private addresses, since the password would otherwise cross the internet unencrypted.',
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
key: 'database',
|
|
159
|
+
label: 'Database',
|
|
160
|
+
type: 'string',
|
|
161
|
+
required: true,
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
query: {
|
|
165
|
+
readOnly: true,
|
|
166
|
+
async run({ credential, fields, sql, params }) {
|
|
167
|
+
// ClickHouse binds named, typed parameters ({name:Type}), which a
|
|
168
|
+
// positional array cannot describe.
|
|
169
|
+
// TODO: support named params, e.g. { "id": 42 } for {id:UInt32}.
|
|
170
|
+
// Today the query API only accepts params as a list ([42]), which
|
|
171
|
+
// ClickHouse cannot use, so callers must write values into the SQL.
|
|
172
|
+
// Allowing an object needs changes in the request DTO, the server's
|
|
173
|
+
// QueryRequest type and the SDK; then pass it here as `query_params`.
|
|
174
|
+
if (params && params.length > 0) {
|
|
175
|
+
throw new ClickHouseConnectionError('ClickHouse does not support positional params; inline the values in the SQL');
|
|
176
|
+
}
|
|
177
|
+
return withReadOnlyClient(fields, credential, async (client) => {
|
|
178
|
+
const result = await client.query({ query: sql, format: 'JSON' });
|
|
179
|
+
// The type argument is one row; the client wraps it in
|
|
180
|
+
// ClickHouse's JSON envelope (`meta`, `data`, `rows`).
|
|
181
|
+
const json = await result.json();
|
|
182
|
+
return {
|
|
183
|
+
rows: json.data,
|
|
184
|
+
fields: (json.meta ?? []).map((column) => column.name),
|
|
185
|
+
rowCount: json.rows ?? json.data.length,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
identity: {
|
|
191
|
+
/**
|
|
192
|
+
* `http` is unusable here — it is bound to no base URL — so it is
|
|
193
|
+
* ignored; the same real connection this integration will later query
|
|
194
|
+
* with is what proves the credential.
|
|
195
|
+
*/
|
|
196
|
+
async fetchAccount({ accessToken, fields }) {
|
|
197
|
+
const credential = { accessToken };
|
|
198
|
+
const { host, database } = targetOf(fields, credential);
|
|
199
|
+
const user = await withReadOnlyClient(fields, credential, async (client) => {
|
|
200
|
+
const result = await client.query({
|
|
201
|
+
query: 'SELECT currentUser() AS user',
|
|
202
|
+
format: 'JSONEachRow',
|
|
203
|
+
});
|
|
204
|
+
const [row] = await result.json();
|
|
205
|
+
if (!row?.user) {
|
|
206
|
+
throw new ClickHouseConnectionError('ClickHouse did not report the current user');
|
|
207
|
+
}
|
|
208
|
+
return row.user;
|
|
209
|
+
});
|
|
210
|
+
return { externalAccountId: `${user}@${host}/${database}` };
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
export default clickhouse;
|
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.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.8.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.8.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@kosha-ai/integration-sdk": "0.8.0"
|
|
55
|
+
}
|
|
6
56
|
}
|
package/README.md
DELETED