Run Query

Script clickhouse Verified

by adam186 ยท 1/2/2023

The script

Submitted by adam186 Deno
Verified 368 days ago
1
import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
2
import {
3
  ClickHouseSettings,
4
  createClient,
5
  DataFormat,
6
  QueryParams,
7
} from "npm:@clickhouse/[email protected]";
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
  • Submitted by pieter jongsma557 Deno
    Created 922 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import {
    3
      ClickHouseSettings,
    4
      createClient,
    5
      DataFormat,
    6
      QueryParams,
    7
    } from "npm:@clickhouse/[email protected]";
    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
    }