-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimestamp.js
74 lines (74 loc) · 2.03 KB
/
timestamp.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
module.exports = function(compound, Timestamp) {
Timestamp.updateTS = function(model, value, cb) {
var toSave;
if (typeof value === 'number') {
value = JSON.stringify(value);
} else if (value === null) {
value = JSON.stringify(new Date().valueOf());
}
toSave = {
id: model + "Timestamp",
v: value
};
return Timestamp.find(toSave.id, (function(_this) {
return function(err, myTimestamp) {
if (err) {
compound.models.Timestamp.create(toSave, function(err, doc) {});
} else {
myTimestamp.updateAttributes(toSave, function(err) {});
}
return cb();
};
})(this));
};
Timestamp.refreshCacheAfterCreate = function(ctrl, model, doc, cb) {
var newTS;
if (cb == null) {
cb = null;
}
newTS = new Date().valueOf();
doc = JSON.parse(JSON.stringify(doc));
doc['model'] = model;
compound.utils.cache.dataset.insert([doc]);
compound.utils.cache.setTimeStamp(model, newTS);
return compound.models.Timestamp.updateTS(ctrl, newTS, function() {
if (cb) {
return cb();
}
});
};
Timestamp.refreshCacheAfterUpdate = function(ctrl, model, doc, cb) {
var newTS;
if (cb == null) {
cb = null;
}
newTS = new Date().valueOf();
doc = JSON.parse(JSON.stringify(doc));
doc['model'] = model;
compound.utils.cache.dataset({
id: doc.id
}).update(doc);
compound.utils.cache.setTimeStamp(model, newTS);
return compound.models.Timestamp.updateTS(ctrl, newTS, function() {
if (cb) {
return cb();
}
});
};
return Timestamp.refreshCacheAfterDestroy = function(ctrl, model, doc, cb) {
var newTS;
if (cb == null) {
cb = null;
}
newTS = new Date().valueOf();
compound.utils.cache.dataset().filter({
id: doc.id
}).remove();
compound.utils.cache.setTimeStamp(model, newTS);
return compound.models.Timestamp.updateTS(ctrl, newTS, function() {
if (cb) {
return cb();
}
});
};
};