Skip to content

Commit

Permalink
fix Backup mixin and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
cristo-rabani committed Mar 31, 2016
1 parent 3460fab commit da1685c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/UniMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ UniCollection.prototype.docMethods = function (methods, isOverride = false) {
var launchedFromServerSide;
if (Meteor.isServer) {
launchedFromServerSide = new Meteor.EnvironmentVariable();
UniCollection._lastMethod = launchedFromServerSide;
}


Expand Down
10 changes: 9 additions & 1 deletion lib/UniMongoIndexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
53 changes: 33 additions & 20 deletions mixins/BackupMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
}
}
}

Expand Down Expand Up @@ -91,46 +91,59 @@ 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});
});
}

restore (collection, selector = {}, {
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;
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});

Expand All @@ -18,7 +18,7 @@ Package.onUse(function (api) {
'underscore',
'mongo',
'minimongo',
'universe:utilities@2.2.2',
'universe:utilities@2.3.2',
'aldeed:[email protected]'
]);

Expand Down

0 comments on commit da1685c

Please sign in to comment.