All files / lib/storage simple_storage.js

7.41% Statements 2/27
0% Branches 0/4
0% Functions 0/16
7.41% Lines 2/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                4x   4x                                                                                                                                              
/*
Storage module for bots.
 
Supports storage of data on a team-by-team, user-by-user, and chnnel-by-channel basis.
 
save can be used to store arbitrary object.
These objects must include an id by which they can be looked up.
It is recommended to use the team/user/channel id for this purpose.
Example usage of save:
controller.storage.teams.save({id: message.team, foo:"bar"}, function(err){
  if (err)
    console.log(err)
});
 
get looks up an object by id.
Example usage of get:
controller.storage.teams.get(message.team, function(err, team_data){
  if (err)
    console.log(err)
  else
    console.log(team_data)
});
*/
 
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) {
                teams_db.save(team_data.id, team_data, cb);
            },
            delete: function(team_id, cb) {
                teams_db.delete(team_id, 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) {
                users_db.save(user.id, user, cb);
            },
            delete: function(user_id, cb) {
                users_db.delete(user_id, 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) {
                channels_db.save(channel.id, channel, cb);
            },
            delete: function(channel_id, cb) {
                channels_db.delete(channel_id, cb);
            },
            all: function(cb) {
                channels_db.all(objectsToList(cb));
            }
        }
    };
 
    return storage;
};