All files / happn-service-mongo-2/lib datastore.js

19.75% Statements 16/81
0% Branches 0/30
0% Functions 0/17
22.86% Lines 16/70

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                       1x                        
const mongodb = require('mongodb'),
  mongoclient = mongodb.MongoClient,
  util = require('util');
 
MongoDataStore.prototype.ObjectID = mongodb.ObjectId;
 
MongoDataStore.prototype.initialize = util.promisify(initialize);
MongoDataStore.prototype.findOne = util.promisify(findOne);
MongoDataStore.prototype.count = util.promisify(count);
MongoDataStore.prototype.aggregate = util.promisify(aggregate);
MongoDataStore.prototype.find = util.promisify(find);
MongoDataStore.prototype.increment = util.promisify(increment);
MongoDataStore.prototype.insert = util.promisify(insert);
MongoDataStore.prototype.update = util.promisify(update);
MongoDataStore.prototype.findAndModify = util.promisify(findAndModify);
MongoDataStore.prototype.remove = util.promisify(remove);
MongoDataStore.prototype.disconnect = util.promisify(disconnect);
 
function MongoDataStore(config) {
  if (!config.database) config.database = 'happn';
  if (!config.collection) config.collection = 'happn';
  if (!config.policy) config.policy = {};
  if (!config.opts) config.opts = {};
 
  config.opts.useNewUrlParser = true;
  config.opts.useUnifiedTopology = true;
 
  Object.defineProperty(this, 'config', {
    value: config
  });
}
 
function initialize(callback) {
  mongoclient.connect(this.config.url, this.config.opts, (err, client) => {
    if (err) return callback(err);
    let db = client.db(this.config.database);
    let collection = db.collection(this.config.collection);
 
    Object.defineProperty(this, 'data', {
      value: collection
    });
 
    Object.defineProperty(this, 'connection', {
      value: client
    });
 
    return callback();
  });
}
 
function findOne(criteria, fields, callback) {
  return this.data.findOne(criteria, fields, callback);
}
function count(criteria, options, callback) {
  return this.data.countDocuments(criteria, options, callback);
}
 
function aggregate(criteria, pipeline, options, callback) {
  pipeline.unshift({ $match: criteria });
  return this.data.aggregate(pipeline, options).toArray(callback);
}
 
function find(criteria, searchOptions, sortOptions, callback) {
  if (searchOptions.projection) {
    //always return the path and _id for _meta
    searchOptions.projection.path = 1;
    searchOptions.projection._id = 1;
  }
  let maxTimeMS = searchOptions.maxTimeMS || 0;
  delete searchOptions.maxTimeMS;
 
  if (!sortOptions)
    return this.data
      .find(criteria, searchOptions)
      .maxTimeMS(maxTimeMS)
      .toArray(callback);
 
  this.data
    .find(criteria, searchOptions)
    .maxTimeMS(maxTimeMS)
    .sort(sortOptions)
    .toArray(callback);
}
 
function increment(path, counterName, increment, callback) {
  let setParameters = {
    $inc: {}
  };
 
  setParameters.$inc['data.' + counterName + '.value'] = increment;
 
  this.update(
    {
      path: path
    },
    setParameters,
    (e, updated) => {
      if (e) return callback(e);
      callback(null, updated.data[counterName].value);
    }
  );
}
 
function insert(data, options, callback) {
  return this.data.insertOne(data, options, callback);
}
 
function update(criteria, data, options, callback) {
  if (typeof options == 'function') {
    callback = options;
    options = {};
  }
  if (!options) options = {};
  options.upsert = true;
  options.returnDocument = 'after';
 
  return this.data.findOneAndUpdate(criteria, data, options, function(e, item) {
    if (e) return callback(e);
    if (item) return callback(null, item.value);
    callback(null, null);
  });
}
 
function findAndModify(criteria, data, callback) {
  return this.update(criteria, data, callback);
}
 
function remove(criteria, callback) {
  return this.data.deleteMany(criteria, callback);
}
 
function disconnect(callback) {
  try {
    if (this.connection) return this.connection.close(callback);
  } catch (e) {
    console.warn('failed disconnecting mongo client', e);
  }
  callback();
}
 
module.exports.create = util.promisify(function(config, callback) {
  try {
    let store = new MongoDataStore(config);
 
    store.initialize(function(e) {
      if (e) return callback(e);
      callback(null, store);
    });
  } catch (e) {
    callback(e);
  }
});