ClickHouse / clickhouse-js

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

An optional `values` parameter in the `exec` method #290

Closed slvrtrn closed 3 months ago

slvrtrn commented 3 months ago

Summary

The exec method now accepts an optional values parameter, which allows you to pass the request body as a Stream.Readable. This can be useful in case of custom insert streaming with arbitrary ClickHouse data formats (which might not be explicitly supported and allowed by the client in the insert method yet).

A very minimal example with CSV (from the tests, it does not make sense as CSV is supported via the normal insert, just an illustration):

const stream = Stream.Readable.from(['42,foobar,"[1,2]"'], {
  objectMode: false,
})
const execResult = await client.exec({
  query: `INSERT INTO ${tableName} FORMAT CSV`,
  values: stream,
})
// the result stream contains nothing useful for an insert 
// and should be immediately drained to release the socket
await drainStream(execResult.stream)

const rs = await client.query({
  query: `SELECT * FROM ${tableName}`,
  format: 'JSONEachRow',
})
expect(await rs.json()).toEqual([
  {
    id: '42',
    name: 'foobar',
    sku: [1, 2],
  },
])

Checklist

sonarcloud[bot] commented 3 months ago

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
100.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud