-
Notifications
You must be signed in to change notification settings - Fork 30
/
insert_exclude_columns.ts
65 lines (57 loc) · 1.49 KB
/
insert_exclude_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'
void (async () => {
const tableName = 'insert_exclude_columns'
const client = createClient()
await client.command({
query: `
CREATE OR REPLACE TABLE ${tableName}
(id UInt32, message String)
ENGINE MergeTree()
ORDER BY (id)
`,
})
/**
* Explicitly specifying a list of columns to insert the data into
*/
await client.insert({
table: tableName,
values: [{ message: 'foo' }],
format: 'JSONEachRow',
// `id` column value for this row will be zero
columns: ['message'],
})
await client.insert({
table: tableName,
values: [{ id: 42 }],
format: 'JSONEachRow',
// `message` column value for this row will be an empty string
columns: ['id'],
})
/**
* Alternatively, it is possible to exclude certain columns instead
*/
await client.insert({
table: tableName,
values: [{ message: 'bar' }],
format: 'JSONEachRow',
// `id` column value for this row will be zero
columns: {
except: ['id'],
},
})
await client.insert({
table: tableName,
values: [{ id: 144 }],
format: 'JSONEachRow',
// `message` column value for this row will be an empty string
columns: {
except: ['message'],
},
})
const rows = await client.query({
query: `SELECT * FROM ${tableName} ORDER BY id, message DESC`,
format: 'JSONEachRow',
})
console.info(await rows.json())
await client.close()
})()