-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_storeage.js
67 lines (61 loc) · 1.81 KB
/
simple_storeage.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
var Store = require('jfs');
module.exports = function(config) {
if (!config) {
config = {
path: './',
};
}
var teams_db = new Store(config.path + '/teams', {saveId: 'id'});
var users_db = new Store(config.path + '/users', {saveId: 'id'});
var channels_db = new Store(config.path + '/channels', {saveId: 'id'});
var objectsToList = function(cb) {
return function(err, data) {
if (err) {
cb(err, data);
} else {
cb(err, Object.keys(data).map(function(key) {
return data[key];
}));
}
};
};
var storage = {
teams: {
get: function(team_id, cb) {
teams_db.get(team_id, cb);
},
save: function(team_data, cb) {
console.log(team_data);
teams_db.save(team_data.id, team_data, cb);
},
all: function(cb) {
teams_db.all(objectsToList(cb));
}
},
users: {
get: function(user_id, cb) {
users_db.get(user_id, cb);
},
save: function(user, cb) {
console.log(user);
users_db.save(user.id, user, cb);
},
all: function(cb) {
users_db.all(objectsToList(cb));
}
},
channels: {
get: function(channel_id, cb) {
channels_db.get(channel_id, cb);
},
save: function(channel, cb) {
console.log(channel);
channels_db.save(channel.id, channel, cb);
},
all: function(cb) {
channels_db.all(objectsToList(cb));
}
}
};
return storage;
};