-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
248 lines (226 loc) · 5.97 KB
/
db.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
module.exports = function (options) {
/**
* Module options
*/
var redis = require('redis');
var client = redis.createClient()
, namespace = 'bonjour';
//redis.debug_mode = true;
//client.on('error', console.log);
if ('undefined' !== typeof options) { _set_options_(options); }
/**
* Privates
*/
// Get bonjour key name
function _key_ (id) {
return namespace + ':' + id + ':json';
}
function _key_date_(date) {
return namespace + ':' + date + ':id';
}
// Get sequence key name
function _seq_ () {
return namespace + '::sequence';
}
// Update internal options
function _set_options_ (options) {
if ('undefined' !== typeof options.database) { client.select(options.database); }
if ('undefined' !== typeof options.namespace) { namespace = options.namespace; }
return this;
}
/*
* gets an id by date.
* callback is called with callback(err, id). id is undefined if
* not found.
*/
function _fetchIndex(date, callback) {
client.get(_key_date_(date), function(err, value) {
if(!err && !value) {
return callback(undefined, undefined);
}
if(err) { return callback(err); }
var id = value;
return callback(undefined, id);
});
}
/**
* Stores an index entry (date -> id).
* returns callback(err).
*/
function _index(date, id, callback) {
client.set(_key_date_(date), id, function(err) {
if(err) {
return callback(err);
}
});
}
/**
* Deletes an index entry.
* returns callback(err, deleted).
*/
function _deleteIndex(date, callback) {
client.del(_key_date_(date), function(err, deleted) {
if(!err && deleted === 0) {
err = {
"message": "index not found",
"type": "ENOTFOUND"
};
}
return callback(err, deleted > 0);
});
}
return {
/**
* Update options
*/
"configure": _set_options_,
/**
* Allow disconnection
*/
"close": function disconnect (callback) {
if (callback) { client.on('close', callback); }
if (client.connected) { client.quit(); }
},
"private_fetchIndex": _fetchIndex,
"private_index": _index,
"private_deleteIndex": _deleteIndex,
/**
* Save a new bonjour
* if bonjour has no id, it's an insertion, an update otherwise
* callback is called with (err, bonjour, created)
*/
"save": function save (bonjour, callback) {
var created = ('undefined' == typeof bonjour.id);
var self = this;
var onIdReady = function (bonjour) {
client.set(_key_(bonjour.id), JSON.stringify(bonjour), function (err) {
if(!err) {
_index(bonjour.date, bonjour.id, callback);
} else {
return callback(err);
}
return callback(err, bonjour, created);
});
};
if(created) {
client.incr(_seq_(), function(err, id) {
if(err) { return callback(err); }
bonjour.id = id;
onIdReady(bonjour);
});
} else {
this.fetchOne(bonjour.id, function(err, old) {
if(err) { return callback(err); }
for(var attr in bonjour) {
old[attr] = bonjour[attr];
}
bonjour = old;
onIdReady(bonjour);
});
}
},
/**
* Retrieve a bonjour
* callback is called with (err, bonjour)
* if no bonjour is found, an error is raised with type=ENOTFOUND
*/
"fetchOne": function fetchOne (id, callback) {
client.get(_key_(id), function(err, value) {
if(!err && !value) {
err = {
"message": "Bonjour not found",
"type": "ENOTFOUND"
};
}
if(err) { return callback(err); }
var bonjour = null;
try {
bonjour = JSON.parse(value);
} catch(e) {
return callback(e);
}
return callback(undefined, bonjour);
});
},
"fetchByDate": function fetchByDate(date, callback) {
var self = this;
_fetchIndex(date, function(err, id) {
if(!err && id) {
self.fetchOne(id, callback);
} else {
err = {
"message": "Bonjour not indexed",
"type": "ENOTFOUND"
};
return callback(err);
}
});
},
/**
* Retrieve all IDs
* callback is called with (err, bonjours)
*/
"fetchAll": function fetchAll (callback) {
client.keys(_key_('*'), function(err, keys) {
if(err) { return callback(err); }
callback(undefined, keys.map(function(key) {
return parseInt(key.substring(namespace.length + 1));
}));
return;
});
},
/**
* delete a bonjour
* callback is called with (err, deleted)
*/
"deleteOne": function deleteOne (bonjour, callback) {
_deleteIndex(bonjour.date, function(err, idx_deleted) {
if(!err && idx_deleted ) {
client.del(_key_(bonjour.id), function(err, deleted) {
if(!err && deleted == 0) {
err = {
"message": "Bonjour not found",
"type": "ENOTFOUND"
}
}
return callback(err, deleted > 0);
});
} else {
return callback(err);
}
});
},
/**
* /!\ deletes ALL bonjours /!\
*
* note it doesn't call "flushAll" so only "bonjour" entries will be removed
* callback is called with (err, bonjour)
*/
"deleteAll": function deleteAll (callback) {
var self = this;
client.keys(_key_('*'), function (err, keys) {
if(err) { return callback(err); }
var deleteSequence = function deleteSequence (err, deleted) {
if(err) { return callback(err); }
client.del(_seq_(), function (err, seq_deleted) {
callback(err, deleted > 0 || seq_deleted > 0);
});
};
if(keys.length) {
client.del(keys, deleteSequence);
} else {
deleteSequence(undefined, 0);
}
});
client.keys(_key_date_('*'), function (err, keys) {
if(err) { return callback(err); }
if(keys.length) {
client.del(keys, function(err, idx_deleted) {
if(err) { return callback(err); }
callback(err, idx_deleted > 0);
});
}
});
}
}
};