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

Change incorrect totals TS type #280

Merged
merged 5 commits into from
Jun 6, 2024
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
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## New features

- Added an option to override the credentials for a particular `query`/`command`/`exec`/`insert` request via the `BaseQueryParams.auth` setting; when set, the credentials will be taken from there instead of the username/password provided during the client instantiation.
- Allow overriding `session_id` per query ([@holi0317](https://github.com/Holi0317), [#271](https://github.com/ClickHouse/clickhouse-js/issues/271)).
- Added an option to override the credentials for a particular `query`/`command`/`exec`/`insert` request via the `BaseQueryParams.auth` setting; when set, the credentials will be taken from there instead of the username/password provided during the client instantiation ([#278](https://github.com/ClickHouse/clickhouse-js/issues/278)).
- Added an option to override the `session_id` for a particular `query`/`command`/`exec`/`insert` request via the `BaseQueryParams.session_id` setting; when set, it will be used instead of the session id provided during the client instantiation ([@holi0317](https://github.com/Holi0317), [#271](https://github.com/ClickHouse/clickhouse-js/issues/271)).

## Bug fixes

- Fixed the incorrect `ResponseJSON<T>.totals` TypeScript type. Now it correctly matches the shape of the data (`T`, default = `unknown`) instead of the former `Record<string, number>` definition ([#274](https://github.com/ClickHouse/clickhouse-js/issues/274)).

# 1.0.2 (Common, Node.js, Web)

Expand Down
65 changes: 65 additions & 0 deletions packages/client-common/__tests__/integration/totals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { ClickHouseClient } from '@clickhouse/client-common'
import { createSimpleTable } from '@test/fixtures/simple_table'
import { createTestClient, guid } from '@test/utils'

describe('Queries with totals', () => {
let client: ClickHouseClient
let tableName: string

beforeEach(async () => {
client = createTestClient()
tableName = `totals_test_${guid()}`
await createSimpleTable(client, tableName)
})
afterEach(async () => {
await client.close()
})

it('should return the expected totals', async () => {
await client.insert({
table: tableName,
values: [
{ id: '42', name: 'hello', sku: [0, 1] },
{ id: '43', name: 'hello', sku: [2, 3] },
{ id: '44', name: 'foo', sku: [3, 4] },
{ id: '45', name: 'foo', sku: [4, 5] },
{ id: '46', name: 'foo', sku: [6, 7] },
],
format: 'JSONEachRow',
})

const rs1 = await client.query({
query: `SELECT *, count(*) AS count FROM ${tableName} GROUP BY id, name, sku WITH TOTALS ORDER BY id ASC`,
format: 'JSON',
})
const result1 = await rs1.json()
expect(result1.data).toEqual([
{ id: '42', name: 'hello', sku: [0, 1], count: '1' },
{ id: '43', name: 'hello', sku: [2, 3], count: '1' },
{ id: '44', name: 'foo', sku: [3, 4], count: '1' },
{ id: '45', name: 'foo', sku: [4, 5], count: '1' },
{ id: '46', name: 'foo', sku: [6, 7], count: '1' },
])
expect(result1.totals).toEqual({
id: '0',
name: '',
sku: [],
count: '5',
})

const rs2 = await client.query({
query: `SELECT 1 :: Int16 AS x, name, count(*) AS count FROM ${tableName} GROUP BY name WITH TOTALS ORDER BY name ASC`,
format: 'JSON',
})
const result2 = await rs2.json()
expect(result2.data).toEqual([
{ x: 1, name: 'foo', count: '3' },
{ x: 1, name: 'hello', count: '2' },
])
expect(result2.totals).toEqual({
x: 1,
name: '',
count: '5',
})
})
})
2 changes: 1 addition & 1 deletion packages/client-common/src/clickhouse_types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface ResponseJSON<T = unknown> {
data: Array<T>
query_id?: string
totals?: Record<string, number>
totals?: T
extremes?: Record<string, any>
// # Supported only by responses in JSON, XML.
// # Otherwise, it can be read from x-clickhouse-summary header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createClient } from '../../src'
describe('[Node.js] errors parsing', () => {
it('should return an error when URL is unreachable', async () => {
const client = createClient({
host: 'http://localhost:1111',
url: 'http://localhost:1111',
})
await expectAsync(
client.query({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('[Node.js] ping', () => {
})
it('does not swallow a client error', async () => {
client = createTestClient({
host: 'http://localhost:3333',
url: 'http://localhost:3333',
})

const result = await client.ping()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createClient } from '../../src'
describe('[Web] errors parsing', () => {
it('should return an error when URL is unreachable', async () => {
const client = createClient({
host: 'http://localhost:1111',
url: 'http://localhost:1111',
})
await expectAsync(
client.query({
Expand Down
2 changes: 1 addition & 1 deletion packages/client-web/__tests__/integration/web_ping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('[Web] ping', () => {
})
it('does not swallow a client error', async () => {
client = createTestClient({
host: 'http://localhost:3333',
url: 'http://localhost:3333',
})

const result = await client.ping()
Expand Down
Loading