diff --git a/lib/UniMethods.js b/lib/UniMethods.js index c5c895c..5532538 100644 --- a/lib/UniMethods.js +++ b/lib/UniMethods.js @@ -180,6 +180,7 @@ UniCollection.prototype.docMethods = function (methods, isOverride = false) { var launchedFromServerSide; if (Meteor.isServer) { launchedFromServerSide = new Meteor.EnvironmentVariable(); + UniCollection._lastMethod = launchedFromServerSide; } diff --git a/lib/UniMongoIndexes.js b/lib/UniMongoIndexes.js index c18d524..5b56081 100644 --- a/lib/UniMongoIndexes.js +++ b/lib/UniMongoIndexes.js @@ -30,5 +30,13 @@ UniCollection.prototype.dropMongoIndex = function (indexName) { var indexes = UniConfig.private.get(this.getCollectionName()+'_indexes') || {}; delete indexes[indexName]; UniConfig.private.set(this.getCollectionName()+'_indexes', indexes); - this._dropIndex(indexName); + try { + this._dropIndex(indexName); + } catch (e) { console.log('Cannot drop index:', e.message) } }; + +UniCollection.prototype.getMongoIndexFromUniverseRegistry = function (indexName) { + check(indexName, String); + var indexes = UniConfig.private.get(this.getCollectionName()+'_indexes') || {}; + return indexes[indexName]; +}; \ No newline at end of file diff --git a/mixins/BackupMixin.js b/mixins/BackupMixin.js index 7a1e5bc..ec6d150 100644 --- a/mixins/BackupMixin.js +++ b/mixins/BackupMixin.js @@ -48,8 +48,6 @@ class BackupMixin extends UniCollection.AbstractMixin { 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) { @@ -59,7 +57,9 @@ class BackupMixin extends UniCollection.AbstractMixin { expireAfterSeconds: this.expireAfter }); } else { - collection.backupCollection.dropMongoIndex('_backupDate'); + if (collection.backupCollection.getMongoIndexFromUniverseRegistry('_backupDate')){ + collection.backupCollection.dropMongoIndex('_backupDate'); + } } } @@ -91,21 +91,28 @@ class BackupMixin extends UniCollection.AbstractMixin { collection.call('restore', selector, options); }; - collection.onBeforeCall('remove', 'backup', (id) => { - if (this.backupOnRemove) { - collection.backup(id); - } - }); + if (Meteor.isServer) { + const removeIt = 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 removeIt.apply(self, arguments); + }; + } } backup (collection, selector = {}) { - collection.find(selector).forEach((document) => { - const object = document.toJSONValue(); + collection.find(selector, {transform: null}).forEach(document => { collection.backupCollection.upsert(document._id, { - ...object, + ...document, _backupDate: new Date() - }); + }, {validate: false}); }); } @@ -113,24 +120,30 @@ class BackupMixin extends UniCollection.AbstractMixin { removeOnRestore = this.removeOnRestore, upsertOnRestore = this.upsertOnRestore } = {}) { + if (Meteor.isClient) { + return true; + } + + const rawCollection = collection.rawCollection(); + const insert = Meteor.wrapAsync(rawCollection.insert, rawCollection); + const update = Meteor.wrapAsync(rawCollection.update, rawCollection); collection.backupCollection.find(selector, { fields: { _backupDate: false - } - }).forEach((document) => { - var object = document.toJSONValue(); - + }, + transform: null + }).forEach(document => { if (upsertOnRestore) { - collection.upsert(object._id, object); + update({_id: document._id}, {$set: document}, {upsert: true}); } else { - collection.insert(object); + insert(document); } if (removeOnRestore) { - document.remove(); + collection.backupCollection.remove({_id: document._id}); } }); } } -UniCollection.mixins.BackupMixin = BackupMixin; +UniCollection.mixins.BackupMixin = BackupMixin; \ No newline at end of file diff --git a/package.js b/package.js index be57c8a..25d4e1f 100644 --- a/package.js +++ b/package.js @@ -3,7 +3,7 @@ Package.describe({ summary: 'Remote method from doc & collection, helpers on document/user, own doc classes, Multischemas, Hooks', name: 'universe:collection', - version: '2.2.2', + version: '2.2.3', git: 'https://github.com/vazco/meteor-universe-collection' }); @@ -18,7 +18,7 @@ Package.onUse(function (api) { 'underscore', 'mongo', 'minimongo', - 'universe:utilities@2.2.2', + 'universe:utilities@2.3.2', 'aldeed:simple-schema@1.5.3' ]);