Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An optional values parameter in the exec method #290

Merged
merged 4 commits into from
Jul 11, 2024
Merged

An optional values parameter in the exec method #290

merged 4 commits into from
Jul 11, 2024

Conversation

slvrtrn
Copy link
Contributor

@slvrtrn slvrtrn commented Jul 11, 2024

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

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials

@slvrtrn slvrtrn requested a review from mshustov July 11, 2024 14:22
@@ -214,10 +226,14 @@ export class ClickHouseClient<Stream = unknown> {
* but format clause is not applicable. The caller of this method is expected to consume the stream,
* otherwise, the request will eventually be timed out.
*/
async exec(params: ExecParams): Promise<ExecResult<Stream>> {
async exec(
params: ExecParams | ExecParamsWithValues<Stream>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I avoided parametrizing ExecParams in this case, as it's a bit tricky to maintain full backward compatibility.

const { controller, controllerCleanup } = this.getAbortController(params)
try {
const { stream, summary, response_headers } = await this.request(
{
method: 'POST',
url: transformUrl({ url: this.params.url, searchParams }),
body: params.query,
body: sendQueryInParams ? params.values : params.query,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd prefer to keep a single logic path to send a request, but let's postpone until the next major release when we can always send query in URL and values as body (even if it's empty)

Copy link

sonarcloud bot commented Jul 11, 2024

@slvrtrn slvrtrn merged commit e78b6f0 into main Jul 11, 2024
29 checks passed
@slvrtrn slvrtrn deleted the exec-values branch July 11, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants