All files / src/builders keyspace.js

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x   1x   1x 1x     1x                                                                                               1x 1x       1x 1x 1x             1x 1x 1x                 1x  
const util = require('util');
 
const debug = require('debug')('express-cassandra');
 
const KeyspaceBuilder = function f(client) {
  this._client = client;
};
 
KeyspaceBuilder.prototype = {
  generate_replication_text(replicationOptions) {
    if (typeof replicationOptions === 'string') {
      return replicationOptions;
    }
 
    const properties = [];
    Object.keys(replicationOptions).forEach((k) => {
      properties.push(util.format("'%s': '%s'", k, replicationOptions[k]));
    });
 
    return util.format('{%s}', properties.join(','));
  },
 
  create_keyspace(keyspaceName, defaultReplicationStrategy, callback) {
    const replicationText = this.generate_replication_text(defaultReplicationStrategy);
 
    const query = util.format(
      'CREATE KEYSPACE IF NOT EXISTS "%s" WITH REPLICATION = %s;',
      keyspaceName,
      replicationText,
    );
    debug('executing query: %s', query);
    this._client.execute(query, (err) => {
      this._client.shutdown(() => {
        callback(err);
      });
    });
  },
 
  alter_keyspace(keyspaceName, defaultReplicationStrategy, callback) {
    const replicationText = this.generate_replication_text(defaultReplicationStrategy);
 
    const query = util.format(
      'ALTER KEYSPACE "%s" WITH REPLICATION = %s;',
      keyspaceName,
      replicationText,
    );
    debug('executing query: %s', query);
    this._client.execute(query, (err) => {
      this._client.shutdown(() => {
        // eslint-disable-next-line no-console
        console.warn('WARN: KEYSPACE ALTERED! Run the `nodetool repair` command on each affected node.');
        callback(err);
      });
    });
  },
 
  get_keyspace(keyspaceName, callback) {
    const query = util.format(
      "SELECT * FROM system_schema.keyspaces WHERE keyspace_name = '%s';",
      keyspaceName,
    );
    debug('executing query: %s', query);
    this._client.execute(query, (err, result) => {
      Iif (err) {
        this._client.shutdown(() => {
          callback(err);
        });
        return;
      }
 
      Eif (result.rows && result.rows.length > 0) {
        callback(null, result.rows[0]);
        return;
      }
 
      callback();
    });
  },
 
};
 
module.exports = KeyspaceBuilder;