<commit_before>const debug = require('debug')('persistency-module');
var MongoClient = require('mongodb').MongoClient;

function persistencyModule() {

    const NO_DATABASE = 'none';

    function executeAction(database, collection, data, callback) {
        if (database === NO_DATABASE) {
            return Promise.resolve(data);
        }
        if (!data || data.length <= 0) {
            debug('No data to be changed');
            return Promise.resolve([]);
        }
        let connection = MongoClient.connect('mongodb://' + database);
        return connection.then((db) => {
            debug('Successfully connected to ' + database);
            return callback(db, collection, data);
        }).catch((err) => {
            debug('Failed to connect to ' + database);
            return err;
        });
    }

    function insertData(db, collection, docs) {
        let insertion = db.collection(collection).insertMany(docs);
        return insertion.then((res) => {
            debug('Persisted ' + res.insertedIds.length + ' results');
            db.close();
            return res.insertedIds;
        });
    }

    function removeData(db, collection, ids) {
        let target = {
            '_id': {
                '$in': ids
            }
        };
        let removal = db.collection(collection).deleteMany(target);
        return removal.then((res) => {
            db.close();
            return res.result.n;
        });
    }

    return {
        NO_DATABASE,
        insertFlights: function(database, collection, docs) {
            return executeAction(database, collection, docs, insertData);
        },
        removeFlights: function(database, collection, ids) {
            return executeAction(database, collection, ids, removeData);
        }
    };
}

module.exports = persistencyModule();<commit_msg>use shorthand<commit_after>const debug = require('debug')('persistency-module');
var MongoClient = require('mongodb').MongoClient;

function persistencyModule() {

    const NO_DATABASE = 'none';

    function executeAction(database, collection, data, callback) {
        if (database === NO_DATABASE) {
            return Promise.resolve(data);
        }
        if (!data || data.length <= 0) {
            debug('No data to be changed');
            return Promise.resolve([]);
        }
        let connection = MongoClient.connect('mongodb://' + database);
        return connection.then((db) => {
            debug('Successfully connected to ' + database);
            return callback(db, collection, data);
        }).catch((err) => {
            debug('Failed to connect to ' + database);
            return err;
        });
    }

    function insertData(db, collection, docs) {
        let insertion = db.collection(collection).insertMany(docs);
        return insertion.then((res) => {
            debug('Persisted ' + res.insertedIds.length + ' results');
            db.close();
            return res.insertedIds;
        });
    }

    function removeData(db, collection, ids) {
        let target = {
            '_id': {
                '$in': ids
            }
        };
        let removal = db.collection(collection).deleteMany(target);
        return removal.then((res) => {
            db.close();
            return res.result.n;
        });
    }

    return {
        NO_DATABASE,
        insertFlights(database, collection, docs) {
            return executeAction(database, collection, docs, insertData);
        },
        removeFlights(database, collection, ids) {
            return executeAction(database, collection, ids, removeData);
        }
    };
}

module.exports = persistencyModule();
{'fraction_non_alphanumeric': 0.12861035422343325, 'fraction_numerical': 0.0005449591280653951, 'mean_word_length': 2.0439469320066337, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5254727', 'n_tokens_mistral': 978, 'n_tokens_neox': 940, 'n_words': 214}