-
Notifications
You must be signed in to change notification settings - Fork 0
/
localcollections-persister.js
74 lines (66 loc) · 1.79 KB
/
localcollections-persister.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*global Reload, Mongo, LocalCollectionsPersister:true */
//closure for the singleton instance
var instance;
/**
*
* Singleton constructor
* @constructor
*/
LocalCollectionsPersister = function(){
"use strict";
if (!instance){
instance = this;
} else {
return instance;
}
this._collections = {};
};
/**
*
* @param name {string}
* @param options {{idGeneration:string, transform:function(Object)}}
* @returns {Mongo.Collection}
*/
LocalCollectionsPersister.prototype.createCollection = function(name, options){
"use strict";
if (this._collections[name]){
return this._collections[name];
}
/**
*
* @type {Mongo.Collection}
*/
var coll = new Mongo.Collection(null, options);
var collsToRestore = Reload._migrationData('localcollections-persister') && Reload._migrationData('localcollections-persister').collections;
var objectsToRestore = collsToRestore && collsToRestore[name];
if (Array.isArray(objectsToRestore)){
objectsToRestore.forEach(function(obj){
coll.insert(obj);
}, this);
}
/**
* @dict
* @type {Object<string, Mongo.Collection>}
*/
this._collections[name] = coll;
return coll;
};
/**
*
* @returns {Object<string, Mongo.Collection>}
*/
LocalCollectionsPersister.prototype.getCollections = function(){
"use strict";
return this._collections;
};
//Register the function responsible for saving the collection content before incoming hot code push
Reload._onMigrate('localcollections-persister', function () {
"use strict";
var LCPersister = new LocalCollectionsPersister();
var collsToMigrate = LCPersister.getCollections();
var dataToMigrate = {};
Object.keys(collsToMigrate).forEach(function(name){
dataToMigrate[name] = collsToMigrate[name].find().fetch();
});
return [true, {collections: dataToMigrate}];
});