-
Notifications
You must be signed in to change notification settings - Fork 30
/
insert_ephemeral_columns.ts
47 lines (42 loc) · 1.15 KB
/
insert_ephemeral_columns.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'
// Ephemeral columns documentation: https://clickhouse.com/docs/en/sql-reference/statements/create/table#ephemeral
void (async () => {
const tableName = 'insert_ephemeral_columns'
const client = createClient({})
await client.command({
query: `
CREATE OR REPLACE TABLE ${tableName}
(
id UInt64,
message String DEFAULT message_default,
message_default String EPHEMERAL
)
ENGINE MergeTree()
ORDER BY (id)
`,
})
await client.insert({
table: tableName,
values: [
{
id: '42',
message_default: 'foo',
},
{
id: '144',
message_default: 'bar',
},
],
format: 'JSONEachRow',
// The name of the ephemeral column has to be specified here
// to trigger the default values logic for the rest of the columns
columns: ['id', 'message_default'],
})
const rows = await client.query({
query: `SELECT *
FROM ${tableName}`,
format: 'JSONEachRow',
})
console.info(await rows.json())
await client.close()
})()