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

Allow to override session id for a particular request #281

Merged
merged 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 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)).

# 1.0.2 (Common, Node.js, Web)

Expand Down
7 changes: 5 additions & 2 deletions jasmine.node.integration.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"spec_dir": "packages/client-node/__tests__",
"spec_files": ["integration/*.test.ts"],
"spec_dir": "packages/",
"spec_files": [
"client-node/__tests__/integration/*.test.ts",
"client-common/__tests__/integration/*.test.ts"
],
"env": {
"failSpecWithNoExpectations": true,
"stopSpecOnExpectationFailure": true,
Expand Down
22 changes: 0 additions & 22 deletions packages/client-common/__tests__/integration/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,6 @@ describe('exec', () => {
)
})

describe('sessions', () => {
let sessionClient: ClickHouseClient
beforeEach(() => {
sessionClient = createTestClient({
session_id: `test-session-${guid()}`,
})
})
afterEach(async () => {
await sessionClient.close()
})

it('should allow the use of a session', async () => {
// Temporary tables cannot be used without a session
const tableName = `temp_table_${guid()}`
await expectAsync(
sessionClient.exec({
query: `CREATE TEMPORARY TABLE ${tableName} (val Int32)`,
}),
).toBeResolved()
})
})

it('can specify a parameterized query', async () => {
const result = await client.query({
query: `SELECT * from system.tables where name = 'numbers'`,
Expand Down
77 changes: 77 additions & 0 deletions packages/client-common/__tests__/integration/session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { ClickHouseClient } from '@clickhouse/client-common'
import { createTestClient, guid, TestEnv, whenOnEnv } from '@test/utils'

describe('sessions settings', () => {
let client: ClickHouseClient
afterEach(async () => {
await client.close()
})

whenOnEnv(TestEnv.LocalSingleNode).it('should use sessions', async () => {
client = createTestClient({
session_id: `test-session-${guid()}`,
})

const tableName = `temp_table_${guid()}`
await client.command({
query: getTempTableDDL(tableName),
})
await client.insert({
table: tableName,
values: [{ id: 42, name: 'foo' }],
format: 'JSONEachRow',
})
await client.exec({
query: `INSERT INTO ${tableName} VALUES (43, 'bar')`,
})
const rs = await client.query({
query: `SELECT * FROM ${tableName} ORDER BY id ASC`,
format: 'JSONEachRow',
})
expect(await rs.json()).toEqual([
{ id: 42, name: 'foo' },
{ id: 43, name: 'bar' },
])
})

whenOnEnv(TestEnv.LocalSingleNode).it(
'should use session override',
async () => {
// no session_id by default
client = createTestClient()

const sessionId = `test-session-${guid()}`
const tableName = `temp_table_${guid()}`
await client.command({
query: getTempTableDDL(tableName),
session_id: sessionId,
})
await client.insert({
table: tableName,
values: [{ id: 144, name: 'qaz' }],
format: 'JSONEachRow',
session_id: sessionId,
})
await client.exec({
query: `INSERT INTO ${tableName} VALUES (255, 'qux')`,
session_id: sessionId,
})
const rs = await client.query({
query: `SELECT * FROM ${tableName} ORDER BY id ASC`,
format: 'JSONEachRow',
session_id: sessionId,
})
expect(await rs.json()).toEqual([
{ id: 144, name: 'qaz' },
{ id: 255, name: 'qux' },
])
},
)

function getTempTableDDL(tableName: string) {
return `
CREATE TEMPORARY TABLE ${tableName}
(id Int32, name String)
`
}
})
10 changes: 7 additions & 3 deletions packages/client-common/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export interface BaseQueryParams {
/** A specific `query_id` that will be sent with this request.
* If it is not set, a random identifier will be generated automatically by the client. */
query_id?: string
/** A specific ClickHouse Session id for this query.
* If it is not set, {@link BaseClickHouseClientConfigOptions.session_id} will be used.
* @default undefined (no override) */
session_id?: string
/** When defined, overrides the credentials from the {@link BaseClickHouseClientConfigOptions.username}
* and {@link BaseClickHouseClientConfigOptions.password} settings for this particular request.
Expand Down Expand Up @@ -163,17 +166,18 @@ export class ClickHouseClient<Stream = unknown> {
): Promise<QueryResult<Stream, Format>> {
const format = params.format ?? 'JSON'
const query = formatQuery(params.query, format)
const queryParams = this.withClientQueryParams(params)
const { stream, query_id } = await this.connection.query({
query,
...this.withClientQueryParams(params),
...queryParams,
})
return this.makeResultSet(stream, format, query_id, (err) => {
this.logWriter.error({
err,
module: 'Client',
message: 'Error while processing the ResultSet.',
args: {
session_id: this.sessionId,
session_id: queryParams.session_id,
query,
query_id,
},
Expand Down Expand Up @@ -256,10 +260,10 @@ export class ClickHouseClient<Stream = unknown> {
...this.clientClickHouseSettings,
...params.clickhouse_settings,
},
session_id: this.sessionId,
query_params: params.query_params,
abort_signal: params.abort_signal,
query_id: params.query_id,
session_id: params.session_id ?? this.sessionId,
auth: params.auth,
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client-common/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface BaseClickHouseClientConfigOptions {
level?: ClickHouseLogLevel
}
/** ClickHouse Session id to attach to the outgoing requests.
* @default empty string */
* @default empty string (no session) */
session_id?: string
/** @deprecated since version 1.0.0. Use {@link http_headers} instead. <br/>
* Additional HTTP headers to attach to the outgoing requests.
Expand Down
Loading