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

Drain insert response stream in Web version #213

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.2.7 (Web only)

### Bug fixes

- Drain insert response stream in Web version - required to properly work with `async_insert`, especially in the Cloudflare Workers context.

## 0.2.6 (Common, Node.js)

### New features
Expand Down
58 changes: 58 additions & 0 deletions examples/insert_cloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'

void (async () => {
const client = createClient({
host: getFromEnv('CLICKHOUSE_HOST'),
password: getFromEnv('CLICKHOUSE_PASSWORD'),
// See https://clickhouse.com/docs/en/optimize/asynchronous-inserts
clickhouse_settings: {
async_insert: 1,
wait_for_async_insert: 1,
},
})

// Create the table if necessary
const table = 'async_insert_example'
await client.command({
query: `
CREATE TABLE IF NOT EXISTS ${table}
(id UInt32, data String)
ENGINE MergeTree
ORDER BY id
`,
// Tell the server to send the response only when the DDL is fully executed
clickhouse_settings: {
wait_end_of_query: 1,
},
})

// Generate some random data for the sake of example...
const batch = [...new Array(1_000).keys()].map(() => ({
id: Math.floor(Math.random() * 100_000) + 1,
data: Math.random().toString(36).slice(2),
}))

await client.insert({
table,
format: 'JSONEachRow', // or other, depends on your data
values: batch,
})

const res = await client
.query({
query: `SELECT count(*) FROM ${table}`,
format: 'JSONEachRow',
})
.then((r) => r.json())

console.info(res)
})()

// Node.js only
function getFromEnv(key: string) {
if (process.env[key]) {
return process.env[key]
}
console.error(`${key} environment variable should be set`)
process.exit(1)
}
14 changes: 14 additions & 0 deletions packages/client-common/__tests__/integration/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,18 @@ describe('insert', () => {
})
)
})

it('should work with async inserts', async () => {
await client.insert({
table: tableName,
values: jsonValues,
format: 'JSONEachRow',
// See https://clickhouse.com/docs/en/optimize/asynchronous-inserts
clickhouse_settings: {
async_insert: 1,
wait_for_async_insert: 1,
},
})
await assertJsonValues(client, tableName)
})
})
2 changes: 1 addition & 1 deletion packages/client-common/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.2.6'
export default '0.2.7'
2 changes: 1 addition & 1 deletion packages/client-node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.2.6'
export default '0.2.7'
5 changes: 4 additions & 1 deletion packages/client-web/src/connection/web_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ export class WebConnection implements Connection<ReadableStream> {
session_id: params.session_id,
query_id,
})
await this.request({
const res = await this.request({
values: params.values,
params,
searchParams,
})
if (res.body !== null) {
await res.text() // drain the response (it's empty anyway)
}
return {
query_id,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client-web/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '0.2.6'
export default '0.2.7'
Loading