text: // generator: /wizzi-labs/wizzi-lab-site/ittf/lib/artifacts/js/module/gen/main.js, utc time: Wed, 01 Feb 2017 14:56:33 GMT
  'use strict';
  // generated by js.module.es2015.module
  function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  
  var _ = require('./utils');
  var crud = require('./arraycrud');
  var Collection = (function () {
      function Collection(dbContext,collectionName,keyNames) {
          _classCallCheck(this, Collection);
          this.crud = dbContext.crud;
          this.db = dbContext.db;
          this.q = dbContext.q;
          this.collectionName = collectionName;
          this.keyNames = keyNames;
      }
      Collection.prototype.insert = function(obj,callback) {
          var self = this;
          var objkey = _.extractKey(this.keyNames, obj)
          ;
          this.q.add(this.insertUnique_job, this, function(err,obj) {
              if (err) {
                  console.log('testInsert' + self.collectionName + '.failed', JSON.stringify(err));
              }
              else {
                  console.log('testInsert' + self.collectionName + '.inserted ', obj.name, obj._id);
              }
              callback(err, obj);
          }, objkey, obj);
      }
      Collection.prototype.update = function(obj,callback) {
          var self = this;
          var objkey = _.extractKey(this.keyNames, obj)
          ;
          this.q.add(this.update_job, this, function(err,obj) {
              if (err) {
                  console.log('testUpdate' + self.collectionName + '.failed', JSON.stringify(err));
              }
              else {
                  console.log('testUpdate' + self.collectionName + '.updated ', obj.name, obj._id);
              }
              callback(err, obj);
          }, objkey, obj);
      }
      Collection.prototype.delete = function(obj,callback) {
          var self = this;
          var objkey = _.extractKey(this.keyNames, obj)
          ;
          this.q.add(this.delete_job, this, function(err,obj) {
              if (err) {
                  console.log('testDelete' + self.collectionName + '.failed', JSON.stringify(err));
              }
              else {
                  console.log('testDelete' + self.collectionName + '.deleted ', obj.name, obj._id);
              }
              callback(err, obj);
          }, objkey);
      }
      Collection.prototype.findOne = function(objkey,callback) {
          var self = this;
          this.q.add(this.findOne_job, this, function(err,obj) {
              if (err) {
                  console.log('testFindOne' + self.collectionName + '.failed', JSON.stringify(err));
              }
              else {
                  console.log('testFindOne' + self.collectionName + '.found ', JSON.stringify(obj, null, 0));
              }
              callback(err, obj);
          }, objkey);
      }
      Collection.prototype.find = function(objkey,callback) {
          if (!callback) {
              callback = objkey;
          }
          this.q.add(this.find_job, this, function(err,list) {
              if (err) {
                  console.log('testFind.failed', JSON.stringify(err));
              }
              else {
                  console.log('testFind.found ', JSON.stringify(list, null, 0));
              }
              callback(err, list);
          }, objkey);
      }
      Collection.prototype.insertUnique_job = function(objkey,obj,callback) {
          var self = this;
          self.db.load(self.collectionName, function(err,list) {
              console.log('insertUnique. db.load. err : ', err);
              if (err) {
                  return callback(err);
              }
              self.crud.insertUnique(list, objkey, obj, function(err,insertedObj) {
                  console.log('insertUnique. crud.insertUnique. err : ', err);
                  if (err) {
                      return callback(err);
                  }
                  self.db.save(self.collectionName, list, function(err) {
                      console.log('insertUnique. db.save. err : ', err);
                      if (err) {
                          return callback(err);
                      }
                      callback(null, insertedObj);
                  });
              });
          });
      }
      Collection.prototype.update_job = function(objkey,obj,callback) {
          var self = this;
          self.db.load(self.collectionName, function(err,list) {
              console.log('update. db.load. err : ', err);
              if (err) {
                  return callback(err);
              }
              self.crud.update(list, objkey, obj, function(err,updatedObj) {
                  console.log('update. crud.update. err : ', err);
                  if (err) {
                      return callback(err);
                  }
                  self.db.save(self.collectionName, list, function(err) {
                      console.log('update. db.save. err : ', err);
                      if (err) {
                          return callback(err);
                      }
                      callback(null, updatedObj);
                  });
              });
          });
      }
      Collection.prototype.delete_job = function(objkey,callback) {
          var self = this;
          self.db.load(self.collectionName, function(err,list) {
              console.log('delete. db.load. err : ', err);
              if (err) {
                  return callback(err);
              }
              self.crud.delete(list, objkey, function(err,deletedObj) {
                  console.log('delete. crud.delete. err : ', err);
                  if (err) {
                      return callback(err);
                  }
                  self.db.save(self.collectionName, list, function(err) {
                      console.log('delete. db.save. err : ', err);
                      if (err) {
                          return callback(err);
                      }
                      callback(null, deletedObj);
                  });
              });
          });
      }
      Collection.prototype.findOne_job = function(objkey,callback) {
          var self = this;
          self.db.load(self.collectionName, function(err,list) {
              if (err) {
                  return callback(err);
              }
              self.crud.findOne(list, objkey, function(err,foundObj) {
                  if (err) {
                      return callback(err);
                  }
                  callback(null, foundObj);
              });
          });
      }
      Collection.prototype.find_job = function(objkey,callback) {
          var self = this;
          self.db.load(self.collectionName, function(err,list) {
              if (err) {
                  return callback(err);
              }
              self.crud.find(list, objkey, function(err,foundList) {
                  if (err) {
                      return callback(err);
                  }
                  callback(null, foundList);
              });
          });
      }
      return Collection;
  })();
  
  
  module.exports = Collection;
  