<commit_before>'use strict';
/**
 * BackupMixin adds backup to collection
 *
 * @example:
 * collection = new UniCollection('collection', {
 *     mixins: [
 *         new UniCollection.mixins.BackupMixin({
 *             name: 'Backup', // backup collecton suffix,
 *             expireAfter: false, // expire time of backup in seconds
 *
 *             backupOnRemove:  true, // if true, creates backup on remove
 *             removeOnRestore: true, // if true, removes backup on restore
 *             upsertOnRestore: false // if true, upserts backup on restore, inserts otherwise
 *         })
 *     ]
 * });
 *
 * collection.insert({number: 1});
 * collection.insert({number: 2});
 * collection.insert({number: 3});
 *
 * collection.find().count(); // 3
 * collection.remove();       // all documents are copied to collection.backupCollection
 * collection.find().count(); // 0
 * collection.restore();      // all documents are copied to collection
 * collection.find().count(); // 3
 */
class BackupMixin extends UniCollection.AbstractMixin {
    constructor ({
        name = 'Backup',
        expireAfter = false,

        backupOnRemove  = true,
        removeOnRestore = true,
        upsertOnRestore = false
    } = {}) {
        super(name);

        this.name = name;
        this.expireAfter = expireAfter;

        this.backupOnRemove  = backupOnRemove;
        this.removeOnRestore = removeOnRestore;
        this.upsertOnRestore = upsertOnRestore;
    }

    mount (collection) {
        collection.backupCollection = new UniCollection(collection.getCollectionName() + this.name);
        collection.backupCollection.create = collection.create.bind(collection);
        collection.backupCollection._validators = collection._validators;
        collection.backupCollection._universeValidators = collection._universeValidators;

        if (Meteor.isServer) {
            if (this.expireAfter) {
                collection.backupCollection.ensureMongoIndex('_backupDate', {
                    _backupDate: true
                }, {
                    expireAfterSeconds: this.expireAfter
                });
            } else {
                collection.backupCollection.dropMongoIndex('_backupDate');
            }
        }

        collection.methods({
            backup: (...args) => {
                this.backup(collection, ...args);
            },

            restore: (...args) => {
                this.restore(collection, ...args);
            }
        });

        collection.docHelpers({
            backup: function () {
                collection.backup(this._id);
            },

            restore: function (options = {}) {
                collection.restore(this._id, options);
            }
        });

        collection.backup = (selector = {}) => {
            collection.call('backup', selector);
        };

        collection.restore = (selector = {}, options = {}) => {
            collection.call('restore', selector, options);
        };

        if (Meteor.isServer) {
            const remove = collection._collection.remove;
            const self = this;
            collection._collection.remove = function () {
                if (self.backupOnRemove) {
                    collection.backup.apply(self, arguments);
                }
                return remove.apply(self, arguments);
            };
        }
    }

    backup (collection, selector = {}) {
        collection.find(selector, {transform: null}).forEach(document => {

            collection.backupCollection.upsert(document._id, {
                ...document,
                _backupDate: new Date()
            }, {validate: false});<commit_msg>fixed issue with doubling permission<commit_after>'use strict';
/**
 * BackupMixin adds backup to collection
 *
 * @example:
 * collection = new UniCollection('collection', {
 *     mixins: [
 *         new UniCollection.mixins.BackupMixin({
 *             name: 'Backup', // backup collecton suffix,
 *             expireAfter: false, // expire time of backup in seconds
 *
 *             backupOnRemove:  true, // if true, creates backup on remove
 *             removeOnRestore: true, // if true, removes backup on restore
 *             upsertOnRestore: false // if true, upserts backup on restore, inserts otherwise
 *         })
 *     ]
 * });
 *
 * collection.insert({number: 1});
 * collection.insert({number: 2});
 * collection.insert({number: 3});
 *
 * collection.find().count(); // 3
 * collection.remove();       // all documents are copied to collection.backupCollection
 * collection.find().count(); // 0
 * collection.restore();      // all documents are copied to collection
 * collection.find().count(); // 3
 */
class BackupMixin extends UniCollection.AbstractMixin {
    constructor ({
        name = 'Backup',
        expireAfter = false,

        backupOnRemove  = true,
        removeOnRestore = true,
        upsertOnRestore = false
    } = {}) {
        super(name);

        this.name = name;
        this.expireAfter = expireAfter;

        this.backupOnRemove  = backupOnRemove;
        this.removeOnRestore = removeOnRestore;
        this.upsertOnRestore = upsertOnRestore;
    }

    mount (collection) {
        collection.backupCollection = new UniCollection(collection.getCollectionName() + this.name);
        collection.backupCollection.create = collection.create.bind(collection);
        collection.backupCollection._validators = collection._validators;
        collection.backupCollection._universeValidators = collection._universeValidators;

        if (Meteor.isServer) {
            if (this.expireAfter) {
                collection.backupCollection.ensureMongoIndex('_backupDate', {
                    _backupDate: true
                }, {
                    expireAfterSeconds: this.expireAfter
                });
            } else {
                collection.backupCollection.dropMongoIndex('_backupDate');
            }
        }

        collection.methods({
            backup: (...args) => {
                this.backup(collection, ...args);
            },

            restore: (...args) => {
                this.restore(collection, ...args);
            }
        });

        collection.docHelpers({
            backup: function () {
                collection.backup(this._id);
            },

            restore: function (options = {}) {
                collection.restore(this._id, options);
            }
        });

        collection.backup = (selector = {}) => {
            collection.call('backup', selector);
        };

        collection.restore = (selector = {}, options = {}) => {
            collection.call('restore', selector, options);
        };

        if (Meteor.isServer) {
            const remove = collection._collection.remove;
            const self = this;
            collection._collection.remove = function () {
                if (self.backupOnRemove) {
                    const args = arguments;
                    UniCollection._lastMethod.withValue('remove', function () {
                        collection.backup.apply(self, args);
                    });
                }
                return remove.apply(self, arguments);
            };
        }
    }

    backup (collection, selector = {}) {
        collection.find(selector, {transform: null}).forEach(document => {

            collection.backupCollection.upsert(document._id, {
                ...document,
                _backupDate: new Date()
            }, {validate: false});
{'fraction_non_alphanumeric': 0.1190855927698033, 'fraction_numerical': 0.001594896331738437, 'mean_word_length': 1.8225806451612903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4081176', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1825, 'n_words': 380}