From 1977fa466201929a2736bd8ebc442731e0f00d12 Mon Sep 17 00:00:00 2001 From: restrry Date: Sun, 2 Oct 2022 12:17:05 +0200 Subject: [PATCH] add insert a stream example --- examples/insert_json_stream.ts | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/insert_json_stream.ts diff --git a/examples/insert_json_stream.ts b/examples/insert_json_stream.ts new file mode 100644 index 00000000..8783b23d --- /dev/null +++ b/examples/insert_json_stream.ts @@ -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() +})()