Skip to content

Commit

Permalink
add insert a stream example
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Oct 2, 2022
1 parent b603725 commit 1977fa4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/insert_json_stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createClient } from '@clickhouse/client'
import Stream from 'stream'

void (async () => {
const client = createClient()
const tableName = 'insert_json_stream'
await client.exec({
query: `DROP TABLE IF EXISTS ${tableName}`,
})
await client.exec({
query: `
CREATE TABLE ${tableName} (id UInt64)
ENGINE MergeTree()
ORDER BY (id)
`,
})

const stream = new Stream.Readable({
objectMode: true,
read() {
/* stub */
},
})

stream.push([{ id: '42' }])
setTimeout(function closeStream() {
stream.push(null)
}, 100)

await client.insert({
table: tableName,
values: stream,
format: 'JSONEachRow',
})

const rows = await client.query({
query: `SELECT * from ${tableName}`,
format: 'JSONEachRow',
})

console.log(await rows.json())

await client.close()
})()

0 comments on commit 1977fa4

Please sign in to comment.