forked from jonathanferreyra/meg-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.coffee
More file actions
66 lines (62 loc) · 2.23 KB
/
Copy pathtimestamp.coffee
File metadata and controls
66 lines (62 loc) · 2.23 KB
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
module.exports = (compound, Timespan) ->
# define Timespan here
Timespan.updateTS = (pluralmodel, value, cb) ->
if typeof value == 'number'
value = JSON.stringify(value)
else if value is null
value = JSON.stringify(new Date().valueOf())
toSave =
id: pluralmodel + "TimeSpan"
v: value
Timespan.find toSave.id, (err, mytimespan) =>
if err
compound.models.Timespan.create toSave, (err, timespan) =>
else
mytimespan.updateAttributes toSave, (err) =>
cb()
# agrega el nuevo doc a la cache sin
# necesidad de recargar desde la bd
Timespan.refreshCacheBeforeCreate = (ctrl, model, doc, cb=null) ->
if compound.app.compound.orm._schemas[0].name != 'memory'
newTS = new Date().valueOf()
# append the doc
doc = JSON.parse(JSON.stringify(doc))
doc['model'] = model
compound.meg.cache.dataset.insert([doc])
# update the ts in cache
compound.meg.cache.setTimeSpan(model, newTS)
# update the ts in bd
compound.models.Timespan.updateTS ctrl, newTS, () ->
cb() if cb
else
cb() if cb
# actualiza el doc en la cache sin
# necesidad de recargar desde la bd
Timespan.refreshCacheBeforeUpdate = (ctrl, model, doc, cb=null) ->
if compound.app.compound.orm._schemas[0].name != 'memory'
newTS = new Date().valueOf()
# update the doc
doc = JSON.parse(JSON.stringify(doc))
doc['model'] = model
compound.meg.cache.dataset({ id:doc.id }).update(doc)
# update the ts in cache
compound.meg.cache.setTimeSpan(model, newTS)
# update the ts in bd
compound.models.Timespan.updateTS ctrl, newTS, () ->
cb() if cb
else
cb() if cb
# elimina el doc de la cache sin
# necesidad de recargar desde la bd
Timespan.refreshCacheBeforeDestroy = (ctrl, model, doc, cb=null) ->
if compound.app.compound.orm._schemas[0].name != 'memory'
newTS = new Date().valueOf()
# remove the doc
compound.meg.cache.dataset().filter({ id:doc.id }).remove()
# update the ts in cache
compound.meg.cache.setTimeSpan(model, newTS)
# update the ts in bd
compound.models.Timespan.updateTS ctrl, newTS, () ->
cb() if cb
else
cb() if cb