All files / src/builders udt.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 541x   1x   1x 1x     1x                                           2x         2x 2x 2x         2x 2x 2x               1x  
const util = require('util');
 
const debug = require('debug')('express-cassandra');
 
const UdtBuilder = function f(client) {
  this._client = client;
};
 
UdtBuilder.prototype = {
  create_udt(typeName, typeDef, callback) {
    const udtFields = [];
    Object.keys(typeDef).forEach((field) => {
      udtFields.push(util.format(
        '"%s" %s',
        field,
        typeDef[field],
      ));
    });
    const query = util.format(
      'CREATE TYPE IF NOT EXISTS "%s" (%s);',
      typeName,
      udtFields.toString(),
    );
    debug('executing query: %s', query);
    this._client.execute(query, (err) => {
      callback(err);
    });
  },
 
  get_udt(typeName, keyspaceName, callback) {
    const query = util.format(
      "SELECT * FROM system_schema.types WHERE keyspace_name = '%s' AND type_name = '%s';",
      keyspaceName,
      typeName,
    );
    debug('executing query: %s', query);
    this._client.execute(query, (err, result) => {
      Iif (err) {
        callback(err);
        return;
      }
 
      Eif (result.rows && result.rows.length > 0) {
        callback(null, result.rows[0]);
        return;
      }
 
      callback();
    });
  },
};
 
module.exports = UdtBuilder;