{"size": 2377, "ext": "js", "max_stars_repo_path": "mongoDB.js", "max_stars_repo_name": "theTechie/nosql-eval", "max_stars_repo_stars_event_min_datetime": "2019-06-03T17:45:55.000Z", "max_stars_repo_stars_event_max_datetime": "2019-06-03T17:45:55.000Z", "max_issues_repo_path": "mongoDB.js", "max_issues_repo_name": "theTechie/nosql-eval", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "mongoDB.js", "max_forks_repo_name": "theTechie/nosql-eval", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.6380952381, "max_line_length": 68, "alphanum_fraction": 0.6251577619}
var Q = require('q'),
    MongoClient = require('mongodb').MongoClient,
    uri = 'mongodb://',
    port = '27017',
    db_name = 'nosql-eval',
    TABLE_NAME = 'CS550',
    db_obj;

function init(ip, callback) {
  var deferred = Q.defer();
  var url = uri + ip + ':' + port + '/' + db_name;
  
  MongoClient.connect(url, function(err, db) {
    if (err) {
      deferred.reject(err);
    } else {
      db_obj = db;
      createTable().then(function (result) {
        deferred.resolve(result);
      }, function (err) {
        deferred.reject(err);
      });
    }
  });
  
  return deferred.promise.nodeify(callback);
}

function createTable(callback) {
  var deferred = Q.defer();
  
  db_obj.createCollection(TABLE_NAME, function (err, result) {
    if (err) deferred.reject(false);
    else deferred.resolve(true);
  });
  
  return deferred.promise.nodeify(callback);
}

// NOTE: Add key-value to table (key : 'key' value: 'value')
function putValue(key, value, callback) {
    var deferred = Q.defer();

    db_obj.collection(TABLE_NAME).insertOne({
      key: key,
      value: value
    }, function (err, result) {
      if (err) deferred.reject(false);
      else deferred.resolve(true);
    });

    return deferred.promise.nodeify(callback);
}

// NOTE: Get value from table (key : 'key')
function getValue(key, callback) {
    var deferred = Q.defer();

    db_obj.collection(TABLE_NAME).find({
      key: key
    }, function (err, result) {
      if (err) deferred.reject(false);
      else deferred.resolve(result);
    });

    return deferred.promise.nodeify(callback);
}

// NOTE: Get value from table (key : 'key')
function deleteKey(key, callback) {
    var deferred = Q.defer();

    db_obj.collection(TABLE_NAME).deleteOne({
      key: key
    }, function (err, result) {
      if (err) deferred.reject(false);
      else deferred.resolve(true);
    });

    return deferred.promise.nodeify(callback);
}

function getSize(callback) {
  var deferred = Q.defer();
  
  db_obj.collection(TABLE_NAME).find().count(function (err, count) {
      if (err) deferred.reject(false);
      else deferred.resolve(count);
  });
  
  return deferred.promise.nodeify(callback);
}

function tearDown() {
  db_obj.close();
}

module.exports = {
  init: init,
  putValue: putValue,
  getValue: getValue,
  deleteKey: deleteKey,
  getSize: getSize,
  tearDown: tearDown
};