2

Run Query

by
Published Jan 2, 2023
Script clickhouse Verified

The script

Submitted by hugo989 Bun
Verified 17 days ago
1
import {
2
  ClickHouseSettings,
3
  createClient,
4
  DataFormat,
5
  QueryParams,
6
} from "@clickhouse/[email protected]";
7

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

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

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

49
function removeObjectEmptyFields(
50
  object?: Record<string, any>,
51
  removeEmptyArraysAndObjects = true,
52
  createNewObject = true,
53
) {
54
  if (!object || typeof object !== "object") return {}
55
  const obj = createNewObject ? { ...object } : object
56
  const emptyValues = [undefined, null, ""]
57
  for (const key in obj) {
58
    const value = obj[key]
59
    if (emptyValues.includes(value)) {
60
      delete obj[key]
61
    } else if (typeof value === "object") {
62
      if (Object.keys(value).length) {
63
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
64
      }
65
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
66
        delete obj[key]
67
      }
68
    }
69
  }
70
  return obj
71
}
72

Other submissions
  • Submitted by adam186 Deno
    Created 409 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
    
    
  • Submitted by pieter jongsma557 Deno
    Created 962 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
    }