I was using such a following code to connect to only 1 keyspace:
This is `/config/db.ts:
import cassandra from "cassandra-driver";
class Cpool {
static _pool : cassandra.Client;
static connect(options : any){
this._pool = new cassandra.Client(options);
}
static execute(query: string, params?: cassandra.ArrayOrObject | undefined, options?: cassandra.QueryOptions | undefined)
{
return this._pool.execute(query, params, options);
}
}
export { Cpool };
And the below code is the index.ts
:
const default_options = {
contactPoints: [process.env.CONTACT_POINTS],
localDataCenter: process.env.LOCAL_DATA_CENTER,
keyspace: process.env.KEYSPACE,
};
try {
await Cpool.connect(default_options);
console.log("Connected to Cassandra");
} catch (err) {
console.log(err);
}
Now I want to have 2 or more keyspaces but don't know should I create a new client in order to each of them or I can use only 1 client to connect to multiple keyspaces? How?
I found that it's not necessary to specify keyspace
property while generating a Cassandra client and connect it to the Cassandra Database, so I changed:
const default_options = {
contactPoints: [process.env.CONTACT_POINTS],
localDataCenter: process.env.LOCAL_DATA_CENTER,
keyspace: process.env.KEYSPACE,
};
To:
const default_options = {
contactPoints: [process.env.CONTACT_POINTS],
localDataCenter: process.env.LOCAL_DATA_CENTER,
};
And I specified keyspaces
inside my queries instead, like INSERT INTO keyspace1.table_name ...
in place of INSERT INTO table_name ....