-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra.js
38 lines (30 loc) · 969 Bytes
/
cassandra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const cassandra = require('cassandra-driver');
const cassandraDb = global.cassandraClient || createCassandraClient();
if (process.env.NODE_ENV == "production") {
global.cassandraClient = cassandraDb;
}
module.exports = cassandraDb;
function createCassandraClient() {
const newClient = new cassandra.Client({
cloud: {
secureConnectBundle: './secure-connect.zip'
},
credentials: {
username: process.env.CASSANDRA_CLIENT_ID,
password: process.env.CASSANDRA_CLIENT_SECRET
},
keyspace: process.env.CASSANDRA_KEYSPACE
});
connectCassandra(newClient);
return newClient;
}
function connectCassandra(client) {
if (client.hosts.length) {
client.connect()
.then(() => console.log("[SERVER] Connected to AstraDB"))
.catch((err) => console.log(`[SERVER] AstraDB connection FAILURE - ${err.message}`));
} else {
console.log("[SERVER] Already connected to AstraDB");
}
}
connectCassandra(cassandraDb);