Skip to content

Commit

Permalink
Update CHANGELOG, role test and example
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn committed Nov 5, 2024
1 parent f398782 commit 9e6cc55
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 269 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.8.0 (Common, Node.js, Web)

## New features

- Added support for specifying roles via request query parameters. See [this example](examples/role.ts) for more details. ([@pulpdrew](https://github.com/pulpdrew), [#328](https://github.com/ClickHouse/clickhouse-js/pull/328))

# 1.7.0 (Common, Node.js, Web)

## Bug fixes
Expand Down
47 changes: 26 additions & 21 deletions examples/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'
* See https://clickhouse.com/docs/en/interfaces/http#setting-role-with-query-parameters
*/
void (async () => {
const format = 'JSON'
const username = 'role_user'
const format = 'JSONEachRow'
const username = 'clickhouse_js_role_user'
const password = 'role_user_password'
const table1 = 'table_1'
const table2 = 'table_2'
const table1 = 'clickhouse_js_role_table_1'
const table2 = 'clickhouse_js_role_table_2'

// Create 2 tables, a role for each table allowing SELECT, and a user with access to those roles
const defaultClient = createClient()
Expand All @@ -23,71 +23,76 @@ void (async () => {
const client = createClient({
username,
password,
// This role will be applied to all the queries by default,
// unless it is overridden in a specific method call
role: table1Role,
})

// Selecting from table1 is allowed using table1Role
let rs = await client.query({
const resultSet1 = await client.query({
query: `select count(*) from ${table1}`,
format,
})
console.log(
`Successfully queried from ${table1} using ${table1Role}. Result: `,
(await rs.json()).data,
await resultSet1.json(),
)

// Selecting from table2 is not allowed using table1Role
// Selecting from table2 is not allowed using table1Role,
// which is set by default in the client instance
await client
.query({ query: `select count(*) from ${table2}`, format })
.catch((e: ClickHouseError) => {
console.error(
`Failed to qeury from ${table2} due to error with type: ${e.type}. Message: ${e.message}`,
`Failed to query from ${table2}, as ${table1Role} does not have sufficient privileges. Expected error:`,
e,
)
})

// Override the client's role to table2Role, allowing a query to table2
rs = await client.query({
const resultSet2 = await client.query({
query: `select count(*) from ${table2}`,
format,
role: table2Role,
format,
})
console.log(
`Successfully queried from ${table2} using ${table2Role}. Result: `,
(await rs.json()).data,
`Successfully queried from ${table2} using ${table2Role}. Result:`,
await resultSet2.json(),
)

// Selecting from table1 is no longer allowed, since table2Role is being used
await client
.query({
query: `select count(*) from ${table1}`,
format,
role: table2Role,
format,
})
.catch((e: ClickHouseError) => {
console.error(
`Failed to qeury from ${table1} due to error with type: ${e.type}. Message: ${e.message}`,
`Failed to query from ${table1}, as ${table2Role} does not have sufficient privileges. Expected error:`,
e,
)
})

// Multiple roles can be specified to allowed querying from either table
rs = await client.query({
const resultSet3 = await client.query({
query: `select count(*) from ${table1}`,
format,
role: [table1Role, table2Role],
format,
})
console.log(
`Successfully queried from ${table1} using roles: [${table1Role}, ${table2Role}]. Result: `,
(await rs.json()).data,
`Successfully queried from ${table1} using roles: [${table1Role}, ${table2Role}]. Result:`,
await resultSet3.json(),
)

rs = await client.query({
const resultSet4 = await client.query({
query: `select count(*) from ${table2}`,
format,
role: [table1Role, table2Role],
format,
})
console.log(
`Successfully queried from ${table2} using roles: [${table1Role}, ${table2Role}]. Result: `,
(await rs.json()).data,
await resultSet4.json(),
)

await client.close()
Expand Down
Loading

0 comments on commit 9e6cc55

Please sign in to comment.