<commit_before>/*!
 * qwebs-mongo
 * Copyright(c) 2015 <NAME>
 * MIT Licensed
 */

//https://mongodb.github.io/node-mongodb-native

"use strict";

const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
const DataError = require("qwebs").DataError;

class MongoService {
    constructor($config) {
        if (!$config.mongo) throw new DataError({ message: "Mongo section is not defined in qwebs config." });
        if (!$config.mongo.connectionString) throw new DataError({ message: "Mongo connectionString is not defined in qwebs config." });
        this.$config = $config;
        this._db = null;

        RegExp.prototype.toJSON = RegExp.prototype.toString; //serialize Regex
        ObjectId.prototype.inspect = function() {
            return "ObjectId(\"" + this.toString() + "\")"; 
        }

        Object.defineProperty(this, "db", {
            enumerable: true,
            get: function() {
                return new Promise((resolve, reject) => {
                    try {
                        if (this._db) resolve(this._db);
                        else {
                            return MongoClient.connect(this.$config.mongo.connectionString).then(db => {
                                if (!db) throw new DataError({ message: "No database." });
                                if (!db.collection) throw new DataError({ message: "No collection." });
                                console.log("Listen mongo " + this.$config.mongo.connectionString);
                                this._db = db;
                                this._db.on("close", () => {
                                    this._db = null;
                                });
                                return this._db;
                            }).then(resolve).catch(reject);
                        }
                    }
                    catch(error) {
                        if (error.name != 'DataError') reject(new DataError({ message: error.message, stack: error.stack }));
                        else reject(error);
                    }
                });
            }
        })
    }

    close() {
        if (this._db) this._db.close();
    }
};

exports = module.exports = MongoService;<commit_msg>Update qwebs-mongo.js<commit_after>/*!
 * qwebs-mongo
 * Copyright(c) 2015 <NAME>
 * MIT Licensed
 */

//https://mongodb.github.io/node-mongodb-native

"use strict";

const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
const DataError = require("qwebs").DataError;

class MongoService {
    constructor($config) {
        if (!$config.mongo) throw new DataError({ message: "Mongo section is not defined in qwebs config." });
        if (!$config.mongo.connectionString) throw new DataError({ message: "Mongo connectionString is not defined in qwebs config." });
        this.$config = $config;
        this._db = null;

        RegExp.prototype.toJSON = RegExp.prototype.toString; //serialize Regex
        ObjectId.prototype.inspect = function() {
            return "ObjectId(\"" + this.toString() + "\")"; 
        }

        Object.defineProperty(this, "db", {
            enumerable: true,
            get: function() {
                return new Promise((resolve, reject) => {
                    try {
                        if (this._db) resolve(this._db);
                        else {
                            return MongoClient.connect(this.$config.mongo.connectionString).then(db => {
                                if (!db) throw new DataError({ message: "No database." });
                                if (!db.collection) throw new DataError({ message: "No collection." });
                                console.log("Listen mongo " + this.$config.mongo.connectionString);
                                this._db = db;
                                this._db.on("close", () => {
                                    this._db = null;
                                });
                                return this._db;
                            }).then(resolve).catch(reject);
                        }
                    }
                    catch(error) {
                        reject(new DataError({ message: error.message, stack: error.stack }));
                    }
                });
            }
        })
    }

    close() {
        if (this._db) this._db.close();
    }
};

exports = module.exports = MongoService;
{'fraction_non_alphanumeric': 0.1250837614473978, 'fraction_numerical': 0.0017869108778199687, 'mean_word_length': 1.4036500268384327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7085823', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1087, 'n_words': 247}