<commit_before>'use strict';

var mongodb = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var GenericAdapter = require('@back4app/back4app-entity').Adapter;

module.exports = MongoAdapter;

/**
 * Adapter for MongoDB, extended from entity project Adapter.
 * @constructor
 * @memberof module:back4app/entity-mongodb/models
 * @example
 * module.exports = MongoAdapter;
 */
function MongoAdapter(host, port) {
  var mongo = new GenericAdapter();
  mongo.config = {};
  mongo.config.host = host;
  mongo.config.port = port;

  mongo.db = null;

  mongo.openConnection = openConnection;
  mongo.closeConnection = closeConnection;

  /**
   * Connects the mongoose with the targeted Mongo database.
   * @name module:back4app/entity-mongodb/models/SchemaBuilder~openConnection
   * @function
   * @param {!module:back4app/entity/models/Entity}
   * entity Entity instance containing properties that shall become
   * the Schema properties.
   * @returns {!Promise} Promise that connects the mongoose to mongo database,
   * and returns it to be used in chained functions.
   * @example
   * var promise = openConnection().then(function (db) {
   *   mongo.db = db;
   *   mongo.entitySchema = Schema.buildModel(entity);
   *   return mongo.entitySchema;
   * }, function () {
   *   throw 'connection error';
   * });
   */
  function openConnection() {
    return new Promise(function (resolve, reject) {
      var uri = 'mongodb://';
      uri += mongo.config.host || 'localhost';
      uri += mongo.config.port ? ':' + mongo.config.port : '';
      uri += '/test';
      MongoClient.connect(uri, function(err, db) {
        if (err) {
          reject(db);
          return null;
        }
        mongo.db = db;
        resolve(db);
        return db;
      });
    });
  }

  /**
   * Closes the current connection.
   * @name module:back4app/entity-mongodb/models/SchemaBuilder~closeConnection
   * @function
   */
  function closeConnection() {
    mongo.db.close();
    mongo.db = null;
  }

  return mongo;
}
<commit_msg>Creates the first version for adapting the entity to be saved.<commit_after>'use strict';

var mongodb = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var GenericAdapter = require('@back4app/back4app-entity').Adapter;

module.exports = MongoAdapter;

/**
 * Adapter for MongoDB, extended from entity project Adapter.
 * @constructor
 * @memberof module:back4app/entity-mongodb/models
 * @example
 * module.exports = MongoAdapter;
 */
function MongoAdapter(host, port) {
  var mongo = new GenericAdapter();
  mongo.config = {};
  mongo.config.host = host;
  mongo.config.port = port;

  mongo.db = null;

  mongo.openConnection = openConnection;
  mongo.closeConnection = closeConnection;
  mongo.instanceToJSON = instanceToJSON;

  /**
   * Connects the mongoose with the targeted Mongo database.
   * @name module:back4app/entity-mongodb/models/SchemaBuilder~openConnection
   * @function
   * @param {!module:back4app/entity/models/Entity}
   * entity Entity instance containing properties that shall become
   * the Schema properties.
   * @returns {!Promise} Promise that connects the mongoose to mongo database,
   * and returns it to be used in chained functions.
   * @example
   * var promise = openConnection().then(function (db) {
   *   mongo.db = db;
   *   mongo.entitySchema = Schema.buildModel(entity);
   *   return mongo.entitySchema;
   * }, function () {
   *   throw 'connection error';
   * });
   */
  function openConnection() {
    return new Promise(function (resolve, reject) {
      var uri = 'mongodb://';
      uri += mongo.config.host || 'localhost';
      uri += mongo.config.port ? ':' + mongo.config.port : '';
      uri += '/test';
      MongoClient.connect(uri, function(err, db) {
        if (err) {
          reject(db);
          return null;
        }
        mongo.db = db;
        resolve(db);
        return db;
      });
    });
  }

  /**
   * Closes the current connection.
   * @name module:back4app/entity-mongodb/models/SchemaBuilder~closeConnection
   * @function
   */
  function closeConnection() {
    mongo.db.close();
    mongo.db = null;
  }

  function instanceToJSON(instance) {
    var json = {};
    for(var key in instance.Entity.attributes){
      json[key] = instance[key];
    }

    if (instance.id) {
      json['_id'] = instance.id;
    }

    return json;
  }

  return mongo;
}

{'fraction_non_alphanumeric': 0.12828054298642533, 'fraction_numerical': 0.0027149321266968325, 'mean_word_length': 3.011796733212341, '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': '5971916', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1341, 'n_words': 324}