ClickHouse / clickhouse-js

Official JS client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
213 stars 26 forks source link

Inserting decimal numbers using NodeJs client #261

Closed kevcodez closed 6 months ago

kevcodez commented 6 months ago

According to the docs, we're not supposed to use JSON strings but Tab/CSV separated with the following code

await client.insert({
  table: 'my_table',
  values: ['12.01\t5000000.405\n'],
  format: 'TabSeparated',
})

However, this results in the error:

The client does not support JSON encoding in [TabSeparated] format.

If I take it correctly from the docs, it's not possibly to use TabSeparated / CsvSeparated to insert an array of strings?

Am I missing something here?

slvrtrn commented 6 months ago

For "raw" formats, a stream is required as the input. You could create it from an array, similarly to how it is done in this example: https://github.com/ClickHouse/clickhouse-js/blob/main/examples/node/stream_created_from_array_raw.ts#L23-L30. CSV/TabSepated/CustomSeparated all work the same way.

slvrtrn commented 6 months ago

This bit regarding decimals and JSON looks pretty outdated.

Maybe it was an issue a while ago when we started the project and wrote the initial docs, but as of the current CH versions, it's all good. I will update the docs.

So, it works fine with JSONEachRow. I pushed an example: https://github.com/ClickHouse/clickhouse-js/blob/main/examples/insert_decimals.ts

slvrtrn commented 6 months ago

Docs will be updated when this PR is merged: https://github.com/ClickHouse/clickhouse-docs/pull/2207