<commit_before>'use strict';
/**
 * mongodb socket
 */
export default class extends think.adapter.socket {
  /**
   * init
   * @param  {Object} config []
   * @return {}        []
   */
  init(config){
    this.config = think.extend({}, {
      host: '127.0.0.1',
      port: 27017
    }, config);

    this.connection = null;
    this.deferred = null;
  }
  /**
   * get connection
   * @return {Promise} []
   */
  async getConnection(){
    let deferred = think.defer();
    let mongo = await think.npm('mongodb');
    let client = mongo.MongoClient;
    let auth = '';
    let config = this.config;
    if(this.config.user){
      auth = `${config.user}:${config.pwd}@`;
    }
    let url = `mongodb://${auth}${config.host}:${config.port}/${config.name}`;
    client.connect(url, this.config, (err, connection) => {
      if(err){
        deferred.reject(err);
        this.close();
      }else{
        this.connection = connection;
        deferred.resolve(this.connection);
      }
    });<commit_msg>update mongo socket<commit_after>'use strict';

import querystring from 'querystring';

/**
 * mongodb socket
 */
export default class extends think.adapter.socket {
  /**
   * init
   * @param  {Object} config []
   * @return {}        []
   */
  init(config){
    this.config = think.extend({}, {
      host: '127.0.0.1',
      port: 27017
    }, config);

    this.connection = null;
    this.deferred = null;
  }
  /**
   * get connection
   * @return {Promise} []
   */
  async getConnection(){
    let deferred = think.defer();
    let mongo = await think.npm('mongodb');
    let client = mongo.MongoClient;
    let Logger = mongo.Logger;
    let auth = '';
    let config = this.config;
    if(this.config.user){
      auth = `${config.user}:${config.pwd}@`;
    }
    // connection options
    // http://mongodb.github.io/node-mongodb-native/2.0/tutorials/urls/
    let options = '';
    if(config.options){
      options = '?' + querystring.stringify(config.options);
    }
    let url = `mongodb://${auth}${config.host}:${config.port}/${config.name}${options}`;
    client.connect(url, this.config, (err, connection) => {
      if(err){
        deferred.reject(err);
        this.close();
      }else{
        //set logger level
        if(config.log_level){
          Logger.setLevel(config.log_level);
        }
        this.connection = connection;
        deferred.resolve(this.connection);
      }
    });
{'fraction_non_alphanumeric': 0.17167558666117744, 'fraction_numerical': 0.009880609304240428, 'mean_word_length': 2.337912087912088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4794653', 'n_tokens_mistral': 811, 'n_tokens_neox': 785, 'n_words': 150}