<commit_before>/* بسم الله الرحمن الرحيم */

module.exports = (function () {
  
  var MongoClient = require('mongodb').MongoClient
    , config      = {};
  
  function exec (callBack) {
    MongoClient.connect(config.url, function(err, db) {
      if(err) return callBack(err);
      callBack(null, db);
    });
  }
  
  function formatResult (rst, affectedKey, data) {
    var results = {
      result : {
          success       : !!rst.ok
        , count         : rst.n
        , affectedCount : rst[affectedKey] || rst.n
      }
    }
    data && (results.data = data);
    return results;
  }
  
  function dataManager () {
    
    this.type = 'mongodb';
    
    this.connect = function (conf, callBack) {
      config = conf;
      MongoClient.connect(conf.url, function(err, db) {
        db.close();
        callBack(err);
      });
      return this;
    }
    
    this.config = function (conf) {
      config = conf;
      return this;
    }
    
    this.insert = function (collectionName, data, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .insert(data, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result, null, result.ops));
          });
        } catch(ex) {
          db.close();
          callBack(ex);
        }
      });
      return this;
    }
    
    this.findAll = function (collectionName, callBack) {
      this.find(collectionName, {}, callBack);
    }
    
    this.find = function (collectionName, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .find(condition)
            .toArray(function(err, result) {
              db.close();
              callBack(err, result);
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }    
    
    this.findOne = function (collectionName, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .findOne(condition, function(err, result) {
              db.close();
              callBack(err, result);
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
    
    this.update = function (collectionName, data, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .updateOne(condition, { $set : data }, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result, 'nModified'));
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
    
    this.updateAll = function (collectionName, data, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .update(condition, { $set : data }, { w:1, multi: true }, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result, 'nModified'));
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
 
    this.delete = function (collectionName, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .deleteOne(condition, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result));
          })
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }<commit_msg>Fix update : delete ._id to avoid immutable error<commit_after>/* بسم الله الرحمن الرحيم */

module.exports = (function () {
  
  var MongoClient = require('mongodb').MongoClient
    , config      = {};
  
  function exec (callBack) {
    MongoClient.connect(config.url, function(err, db) {
      if(err) return callBack(err);
      callBack(null, db);
    });
  }
  
  function formatResult (rst, affectedKey, data) {
    var results = {
      result : {
          success       : !!rst.ok
        , count         : rst.n
        , affectedCount : rst[affectedKey] || rst.n
      }
    }
    data && (results.data = data);
    return results;
  }
  
  function dataManager () {
    
    this.type = 'mongodb';
    
    this.connect = function (conf, callBack) {
      config = conf;
      MongoClient.connect(conf.url, function(err, db) {
        db.close();
        callBack(err);
      });
      return this;
    }
    
    this.config = function (conf) {
      config = conf;
      return this;
    }
    
    this.insert = function (collectionName, data, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .insert(data, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result, null, result.ops));
          });
        } catch(ex) {
          db.close();
          callBack(ex);
        }
      });
      return this;
    }
    
    this.findAll = function (collectionName, callBack) {
      this.find(collectionName, {}, callBack);
    }
    
    this.find = function (collectionName, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .find(condition)
            .toArray(function(err, result) {
              db.close();
              callBack(err, result);
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }    
    
    this.findOne = function (collectionName, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .findOne(condition, function(err, result) {
              db.close();
              callBack(err, result);
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
    
    this.update = function (collectionName, data, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          delete data._id;
          db.collection(collectionName)
            .updateOne(condition, { $set : data }, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result, 'nModified'));
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
    
    this.updateAll = function (collectionName, data, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          delete data._id;
          db.collection(collectionName)
            .update(condition, { $set : data }, { w:1, multi: true }, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result, 'nModified'));
          });
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
 
    this.delete = function (collectionName, condition, callBack) {
      exec(function (err, db) {
        if(err) return callBack(err);
        try {
          db.collection(collectionName)
            .deleteOne(condition, function (err, result) {
              db.close();
              callBack(err, err ? null : formatResult(result.result));
          })
        } catch (ex) {
          db.close();
          callBack(ex);
        }
      });
    }
{'fraction_non_alphanumeric': 0.1325240263024785, 'fraction_numerical': 0.00025290844714213456, 'mean_word_length': 1.6310711909514304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1825242', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2310, 'n_words': 483}