-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathdataStore.js
More file actions
138 lines (124 loc) · 3.7 KB
/
dataStore.js
File metadata and controls
138 lines (124 loc) · 3.7 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
*
* Copyright (C) 2011, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/
var IJOD = require('ijod').IJOD;
var lstate = require('lstate');
var ijodFiles = {};
var mongo;
var mongoID = 'id';
exports.init = function(mongoid, _mongo) {
mongo = _mongo;
mongoID = mongoid;
for (var i in mongo.collections) {
// MAYBETODO: do .count() for each and reset lstate.set("field",val) here?
if (!ijodFiles[i]) {
ijodFiles[i] = new IJOD(i);
}
}
}
exports.addCollection = function(name) {
if(!mongo.collections[name])
mongo.addCollection(name);
}
function now() {
return Date.now();
}
// arguments: type should match up to one of the mongo collection fields
// object will be the object to persist
// options is optional, but if it exists, available options are: strip + timestamp
// strip is an array of properties to strip off before persisting the object.
// options = {strip: ['person','checkins']}, for example
// timeStamp will be the timestamp stored w/ the record if it exists, otherwise, just use now.
//
exports.addObject = function(type, object, options, callback) {
lstate.up(type);
var timeStamp = now();
if (arguments.length == 3) callback = options;
if (typeof options == 'object') {
for (var i in options['strip']) {
object[options['strip'][i]].delete
}
if (options['timeStamp']) {
timeStamp = options['timeStamp'];
}
}
ijodFiles[type].addRecord(timeStamp, object, function(err) {
if (err)
callback(err);
setCurrent(type, object, callback);
})
}
// same deal, except no strip option, just timestamp is available currently
exports.removeObject = function(type, id, options, callback) {
lstate.down(type);
var timeStamp = now();
if (arguments.length == 3) callback = options;
if (typeof options == 'object') {
if (options['timeStamp']) {
timeStamp = options['timeStamp'];
}
}
var record = {deleted: timeStamp};
record[mongoID] = id;
ijodFiles[type].addRecord(timeStamp, record, function(err) {
if (err)
callback(err);
removeCurrent(type, id, callback);
})
}
// mongos
function getMongo(type, id, callback) {
var m = mongo.collections[type];
if(!m)
callback(new Error('invalid type:' + type), null);
else if(!(id && (typeof id === 'string' || typeof id === 'number')))
callback(new Error('bad id:' + id), null);
else
return m;
}
exports.queryCurrent = function(type, query, options) {
query = query || {};
options = options || {};
var m = mongo.collections[type];
if(!m)
callback(new Error('invalid type:' + type), null);
else
return m.find(query, options);
}
exports.getAllCurrent = function(type, callback, options) {
options = options || {};
var m = mongo.collections[type];
if(!m)
callback(new Error('invalid type:' + type), null);
else
m.find({}, options).toArray(callback);
}
exports.getCurrent = function(type, id, callback) {
var m = getMongo(type, id, callback);
if(m) {
var query = {};
query[mongoID] = id;
m.findOne(query, callback);
}
}
function setCurrent(type, object, callback) {
var m = getMongo(type, object[mongoID], callback);
if(m) {
var query = {};
query[mongoID] = object[mongoID];
m.update(query, object, {upsert:true, safe:true}, callback);
}
}
function removeCurrent(type, id, callback) {
var m = getMongo(type, id, callback);
if(m) {
var query = {};
query[mongoID] = id;
m.remove(query, callback);
}
}