<commit_before>var Db = require("mongodb").Db,
    Server = require("mongodb").Server,
    Promise = global.Packages.Promise;


function initialize(params) {
    
    var server = {
        host: params.host,
        port: params.port,
        db: params.db,
    };

    var _pools = {};
    
    // TODO: Mongo supports sharding and replica logic, we should compline this class to support sharding and read replica
    function getClient(callback) {
        var poolkey = server.host + ":" + server.port;
        var pool = _pools[poolkey];

        if (!pool) {
            var db = new Db(server.db, new Server(server.host, server.port, {
                auto_reconnect: true
            }), {
                native_parser: true,
                w: 1
            });

            db.open(function(err, mongoclient) {
                if (err) {
                    callback(err);
                    return;
                }
                _pools[poolkey] = mongoclient;
                callback(err, _pools[poolkey]); //return Promise
            });
        }
        else {

            callback(null, pool);
        }

    }

    function extend(attributes) {

        if (!attributes.tableName) {
            throw new Error("tableName must be defined");
        }

        function Model(obj) {

            //if model creation is not with new keyword then return it with new 
            if (!(this instanceof Model)) {
                return new Model(obj);
            }

            this.tableName = attributes.tableName;

            if (attributes.idAttribute) {
                obj._id = obj[attributes.idAttribute];
                obj[attributes.idAttribute] = undefined;
            }

            this.input = obj;
        }

        Model.prototype.update = function update(where_clause, set_fields) {
            var self = this;
            return new Promise(function(resolve, reject) {
                getClient(function(err, client) {
                    if (err) {
                        reject(err);
                        return;
                    }

                    client.collection(self.tableName).update(where_clause, {
                        $set: set_fields
                    }, function(err, output) {
                        if (err) {
                            reject(err);
                            return;
                        }
                        resolve(output);
                        return;
                    });
                });
            });
        };

        Model.prototype.save = function save() {
            var self = this;
            return new Promise(function(resolve, reject) {
                getClient(function(err, client) {
                    if (err) {
                        reject(err);
                        return;
                    }

                    client.collection(self.tableName).save(self.getMongoDataObject(self.input), function(err, output) {
                        if (err) {
                            reject(err);
                            return;
                        }
                        resolve(output);
                        return;
                    });
                });
            });
        };

        Model.prototype.fetch = function fetch(select_clause) {
            var self = this;
            if (!select_clause) {
                select_clause = {};
            }

            return new Promise(function(resolve, reject) {
                getClient(function(err, client) {
                    if (err) {
                        reject(err);
                        return;
                    }

                    client.collection(self.tableName).find(
                    self.getMongoDataObject(self.input), select_clause, {
                        limit: 1
                    }).toArray(function(err, output) {
                        if (err) {
                            reject(err);
                            return;
                        }

                        if (output.length > 0) {
                            resolve(self.getObjectFromMongoDataObject(output.shift()));
                            return;
                        }
                        else {<commit_msg>mongo model delete all properties while converting to mongo object who are undefined<commit_after>var Db = require("mongodb").Db,
    Server = require("mongodb").Server,
    Promise = global.Packages.Promise;


function initialize(params) {
    
    var server = {
        host: params.host,
        port: params.port,
        db: params.db,
    };

    var _pools = {};
    
    // TODO: Mongo supports sharding and replica logic, we should compline this class to support sharding and read replica
    function getClient(callback) {
        var poolkey = server.host + ":" + server.port;
        var pool = _pools[poolkey];

        if (!pool) {
            var db = new Db(server.db, new Server(server.host, server.port, {
                auto_reconnect: true
            }), {
                native_parser: true,
                w: 1
            });

            db.open(function(err, mongoclient) {
                if (err) {
                    callback(err);
                    return;
                }
                _pools[poolkey] = mongoclient;
                callback(err, _pools[poolkey]); //return Promise
            });
        }
        else {

            callback(null, pool);
        }

    }
    
    function deleteAllPropertiesWhoAreUndefined(object){
        for(var k in object){
            if(object[k] === undefined){
                delete object[k];
            }
        }
    }
    
    function extend(attributes) {

        if (!attributes.tableName) {
            throw new Error("tableName must be defined");
        }

        function Model(obj) {

            //if model creation is not with new keyword then return it with new 
            if (!(this instanceof Model)) {
                return new Model(obj);
            }

            this.tableName = attributes.tableName;

            if (attributes.idAttribute) {
                obj._id = obj[attributes.idAttribute];
                obj[attributes.idAttribute] = undefined;
            }

            this.input = obj;
        }

        Model.prototype.update = function update(where_clause, set_fields) {
            var self = this;
            return new Promise(function(resolve, reject) {
                getClient(function(err, client) {
                    if (err) {
                        reject(err);
                        return;
                    }

                    client.collection(self.tableName).update(where_clause, {
                        $set: set_fields
                    }, function(err, output) {
                        if (err) {
                            reject(err);
                            return;
                        }
                        resolve(output);
                        return;
                    });
                });
            });
        };

        Model.prototype.save = function save() {
            var self = this;
            return new Promise(function(resolve, reject) {
                getClient(function(err, client) {
                    if (err) {
                        reject(err);
                        return;
                    }

                    client.collection(self.tableName).save(self.getMongoDataObject(self.input), function(err, output) {
                        if (err) {
                            reject(err);
                            return;
                        }
                        resolve(output);
                        return;
                    });
                });
            });
        };

        Model.prototype.fetch = function fetch(select_clause) {
            var self = this;
            if (!select_clause) {
                select_clause = {};
            }

            return new Promise(function(resolve, reject) {
                getClient(function(err, client) {
                    if (err) {
                        reject(err);
                        return;
                    }

                    client.collection(self.tableName).find(
                    deleteAllPropertiesWhoAreUndefined(self.getMongoDataObject(self.input)), select_clause, {
                        limit: 1
                    }).toArray(function(err, output) {
                        if (err) {
                            reject(err);
                            return;
                        }

                        if (output.length > 0) {
                            resolve(self.getObjectFromMongoDataObject(output.shift()));
                            return;
                        }
                        else {
{'fraction_non_alphanumeric': 0.09041468388851122, 'fraction_numerical': 0.0006798096532970768, 'mean_word_length': 1.0024954627949183, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '308919', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1959, 'n_words': 436}