{"size": 1005, "ext": "js", "max_stars_repo_path": "mongo-counter.js", "max_stars_repo_name": "guns2410/meteor-mongo-counter", "max_stars_repo_stars_event_min_datetime": "2016-07-24T16:57:22.000Z", "max_stars_repo_stars_event_max_datetime": "2016-07-24T16:57:22.000Z", "max_issues_repo_path": "mongo-counter.js", "max_issues_repo_name": "guns2410/meteor-mongo-counter", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "mongo-counter.js", "max_forks_repo_name": "guns2410/meteor-mongo-counter", "max_forks_repo_forks_event_min_datetime": "2018-05-08T23:26:36.000Z", "max_forks_repo_forks_event_max_datetime": "2018-05-08T23:26:36.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 29.5588235294, "max_line_length": 73, "alphanum_fraction": 0.6746268657}
const callCounters = async function (collectionName, method, arguments) {
  let mongo = MongoInternals.defaultRemoteCollectionDriver().mongo;
  let collection = mongo.rawCollection(collectionName);
  return await collection[method](...arguments);
};

export const deleteCounters = (collectionName) => (
  callCounters(collectionName, 'remove', {})
);

export const incrementCounter = (collectionName, name, amount = 1) => {
  let doc = callCounters(collectionName, 'findAndModify', [
    { _id: name },
    null,
    { $inc: { next_val: amount } },
    { new: true, upsert: true },
  ]).await();
  return doc.value.next_val;
};

export const decrementCounter = (collectionName, name, amount = 1) => (
  incrementCounter(collectionName, name, -amount)
);

export const setCounter = (collectionName, name, value) => {
  let doc = callCounters(collectionName, 'findAndModify', [
    { _id: name },
    null,
    { $set: { next_val: value } },
    { new: true, upsert: true },
  ]);
  return doc.next_val;
};
