{"size": 1916, "ext": "js", "max_stars_repo_path": "lib/MongoDBKeyValueStore.js", "max_stars_repo_name": "kenspirit/fabric-sdk-node-mongodb-kvs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/MongoDBKeyValueStore.js", "max_issues_repo_name": "kenspirit/fabric-sdk-node-mongodb-kvs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/MongoDBKeyValueStore.js", "max_forks_repo_name": "kenspirit/fabric-sdk-node-mongodb-kvs", "max_forks_repo_forks_event_min_datetime": "2018-07-20T07:48:31.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-20T07:48:31.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 26.2465753425, "max_line_length": 96, "alphanum_fraction": 0.6012526096}
const debug = require('debug')('MongoDBKVS')
const mongodb = require('mongodb')

const { MongoClient } = mongodb

module.exports = class MongoDBKVS {
  constructor(options) {
    debug('Constructor Options: %j', options)
    const { url, dbName, collectionName, cacheResult } = options

    if (!url) {
      throw new Error('Missing URL in options')
    }
    if (!collectionName) {
      throw new Error('Missing collectionName in options')
    }

    const self = this
    this.cacheResult = cacheResult || false
    this.cacheResults = {}

    return new MongoClient(url).connect()
      .then((client) => {
        const db = client.db(dbName)
        self.collection = db.collection(collectionName)
        return self
      })
  }

  getValue(name) {
    const nameString = String(name)
    debug('getValue: using %s', nameString)

    if (this.cacheResult && this.cacheResults[nameString]) {
      debug('Retrieved from cache item %s, Value %j', nameString, this.cacheResults[nameString])
      return Promise.resolve(this.cacheResults[nameString])
    }

    return this.collection.findOne({ _id: nameString })
      .then((record) => {
        debug('getValue: Got record %j', record)
        return record ? record.value : null
      })
      .catch((err) => {
        debug('getValue: Error %o', err)
        throw err
      })
  }

  setValue(name, value) {
    const nameString = String(name)
    debug('setValue: Name %s, Value %j', nameString, value)

    if (this.cacheResult && this.cacheResults[nameString]) {
      debug('Updated cache item %s, Value %j', nameString, value)
      this.cacheResults[nameString] = value
    }

    return this.collection.findOneAndUpdate(
      { _id: name },
      { $set: { value } },
      { upsert: true }
    )
      .then(() => {
        return value
      })
      .catch((err) => {
        debug('setValue: Error %o', err)
        throw err
      })
  }
}
