forked from Meteor-Community-Packages/ground-db
-
Notifications
You must be signed in to change notification settings - Fork 1
/
groundDB.server.js
113 lines (96 loc) · 3.22 KB
/
groundDB.server.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
TODO:
`Meteor.default_server` - `Meteor.server`
*/
///////////////////////////////// TEST SCOPE ///////////////////////////////////
Meteor.server = Meteor.server || Meteor.default_server;
//////////////////////////////// GROUND DATABASE ///////////////////////////////
// @export GroundDB
GroundDB = function(name, options) {
// Inheritance Meteor Collection can be set by options.collection
// Accepts smart collections by Arunoda Susiripala
var self;
// Either name is a Meteor collection or we create a new Meteor collection
if (name instanceof Meteor.Collection) {
self = name;
} else {
self = new Meteor.Collection(name, options);
}
// Is this an offline client only database?
self.offlineDatabase = !!(self._connection === null);
//console.log(self._connection);
// Initialize collection name
self.name = (self._name)? self._name : 'null';
// This is the basic interface allowing users easily access for handling
// method calls, this.super() is the super and this.collection is self
// TODO: Remove this section to the README
self.conflictHandlers = (options && options.conflictHandlers)?
options.conflictHandlers: {
'insert': function(doc) {
//console.log('insert');
//console.log(doc);
this.super(doc);
},
'update': function(id, modifier) {
//console.log('update');
//console.log(id);
//console.log(modifier);
this.super(id, modifier);
},
'remove': function(id) {
//console.log('remove');
//console.log(id);
this.super(id);
}
};
// Create overwrite interface
_.each(['insert', 'update', 'remove'], function(name) {
// TODO: init default conflict handlers
//self.conflictHandlers[name] = function() {
// this.super.apply(this, arguments);
//};
// Save super
var _super = Meteor.default_server.method_handlers['/'+self.name+'/'+name];
// Overwrite
Meteor.default_server.method_handlers['/'+self.name+'/'+name] = function() {
var _this = this;
_this.collection = self;
_this.super = _super;
// Call the conflicthandlers
self.conflictHandlers[name].apply(_this, arguments);
};
});
return self;
};
////////////////////////// GET SERVER TIME DIFFERENCE //////////////////////////
Meteor.methods({
'getServerTime': function() {
return Date.now();
}
});
// Unify client / server api
GroundDB.now = function() {
return Date.now();
};
////////////////////////// TIMESTAMP CONFLICTHANDLER ///////////////////////////
// TODO:
// When clients make changes the server should track the documents from the
// clients to see if the changes are new or old changes.
// This could be done in several ways.
// Either by versions or server timestamps - both could work.
//
// Conflicting overview:
// We could cut it down to comparing two documents and keep / broadcast the
// winning document.
//
// conflictHandler = function(clientDoc, serverDoc) { return serverDoc; }
//
//
// There should be found a way of registrating deleted documents - eg. by having
// a flag set 'active' all nonactive documents should then be removed from
// published documents.
//
// This could be a standalone package since it would introduce conflict
// handling in generel
//
// Regz. RaiX