clickhouse-js icon indicating copy to clipboard operation
clickhouse-js copied to clipboard

Change configured database after initial client creation

Open viveknair opened this issue 1 year ago • 1 comments

Describe the feature request

I want to change the configured database after the client has been created. For our tests, we use a client to create a variety of databases for integration tests. Ideally, we would only have a single connected client for these interactions rather than creating multiple clients (given the way our code is written).

Steps to reproduce

  1. Create a Clickhouse client instance

const client = createClient(options)

  1. Since there aren't any public setters/getters to change the database, you have to modify the client's private parameters with

client.config.database = newDbName;

Desired feature

client.setDatabase(newDbName);

viveknair avatar May 16 '23 10:05 viveknair

@viveknair

Since it's mostly for testing purposes, could the following work for you?

// somewhere in the setup
const testDatabase = ...
await client.exec({ query: `CREATE DATABASE ${testDatabase}` })

// in the test suite
await client
  .query({
    query: `SELECT * FROM ${testDatabase}.${table}`,
    format: 'JSONEachRow'
  })

i.e. explicitly write the database in the queries.

slvrtrn avatar May 16 '23 13:05 slvrtrn