Search code examples
cassandranestjsdatastaxdatastax-astradatastax-node-driver

How can I connect to my Datastax Cassandra database?


hope all is well with you. I'm currently working with a cassandra database and I'm almost done with it. Currently I am deploying my services and an error occurred. To work best with cassandra in my nestjs back-end I use the framework https://github.com/ifaim/nestjs-express-cassandra This works in the backend with the framework https://github.com/masumsoft/express-cassandra (which is actively updated and used a lot). But now the problem is that according to the documentation of datastax (where my cassandra db is hosted) I have to deposit a secureConnectBundle to be able to connect to my database.

My config

Console Error

Running Database

Example from Datastax

DataStax

However, I have found very little online about this and generally I find more examples of how to connect to your datastax database without secureConnectBundle than with. So far with me but without success and therefore I ask here. I really hope that I do not have to change the framework because that would probably mean the end of this project.

 export const cassandraOptions: ExpressCassandraModuleOptions = {
  clientOptions: {
    contactPoints: [
      'e68eb980-c5c3-47fa-a423-472652519805-europe-west1.db.astra.datastax.com',
    ],
    protocolOptions: { port: 9042 },
    keyspace: 'partii',
    queryOptions: {
      fetchSize: 100,
      consistency: 1,
    },
    authProvider: new auth.PlainTextAuthProvider('X', 'X'),
  },
  ormOptions: {
    createKeyspace: false,
    defaultReplicationStrategy: {
      class: 'SimpleStrategy',
      replication_factor: 1,
    },
    migration: 'safe',
  },
};

Solution

  • Its solved.

    import {
      auth,
      ExpressCassandraModuleOptions,
    } from '@iaminfinity/express-cassandra';
    // get fs to read the certificate
    import * as fs from 'fs';
    // get path to resolve the certificate path
    import * as path from 'path';
    
    export const cassandraOptions: () => ExpressCassandraModuleOptions = () => {
      const sslOptions = {
        rejectUnauthorized: true,
        cert: fs.readFileSync(path.resolve(__dirname, '../src/database/cert')),
        key: fs.readFileSync(path.resolve(__dirname, '../src/database/key.pem')),
        ca: fs.readFileSync(path.resolve(__dirname, '../src/database/ca.crt')),
      };
    
      return {
        clientOptions: {
          contactPoints: [
            'host',
          ],
          protocolOptions: { port: 29042 },
          keyspace: 'partii',
          queryOptions: {
            fetchSize: 100,
            consistency: 1,
          },
          sslOptions: {
            ...sslOptions,
            host: '34.79.236.16',
            checkServerIdentity: function (host, cert) {
              return undefined;
            },
          },
          authProvider: new auth.PlainTextAuthProvider(
            'id',
            'secret',
          ),
        },
        ormOptions: {
          createKeyspace: false,
          defaultReplicationStrategy: {
            class: 'SimpleStrategy',
            replication_factor: 1,
          },
          migration: 'alter',
        },
      };
    };