0
Run Query
One script reply has been approved by the moderators Verified
Created by adam186 690 days ago Viewed 33027 times
0
Submitted by adam186 Deno
Verified 690 days ago
1
import { removeObjectEmptyFields } from "https://deno.land/x/windmill_helpers@v1.0.1/mod.ts";
2
import {
3
  ClickHouseSettings,
4
  createClient,
5
  DataFormat,
6
  QueryParams,
7
} from "npm:@clickhouse/client@0.0.10";
8

9
/**
10
 * @param parse_into Name of the method that should be used to parse the results.
11
 *
12
 * @param format Format of the resulting dataset. Wrap the format name with `"` in the
13
 * argument editor.
14
 *
15
 * @param clickhouse_settings ClickHouse settings that can be applied on query level.
16
 *
17
 * @param query_params Parameters for query binding.
18
 *
19
 * @param query_params AbortSignal instance to cancel a query in progress.
20
 */
21
type Clickhouse = {
22
  host: string;
23
  username: string;
24
  password: string;
25
};
26
export async function main(
27
  auth: Clickhouse,
28
  query: string,
29
  parse_into: "json" | "text" = "json",
30
  format?: DataFormat,
31
  clickhouse_settings?: ClickHouseSettings,
32
  query_params?: Record<string, unknown>,
33
  abort_signal?: AbortSignal,
34
) {
35
  const client = createClient(removeObjectEmptyFields(auth));
36

37
  const params = removeObjectEmptyFields({
38
    query,
39
    format,
40
    clickhouse_settings,
41
    query_params,
42
    abort_signal,
43
  }) as QueryParams;
44
  const result = await client.query(params);
45
  await client.close();
46

47
  return await result[parse_into]();
48
}
49

Other submissions