Richard Allred
2019-08-03 99e73490b45f1f76040216418afeec9ff9770e57
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
var Sequelize = require("sequelize");
 
var Item = undefined;
 
module.exports.connect = function(params, callback) {
    var sequlz = new Sequelize(
        params.dbname, params.username, params.password,
        params.params);
    Item = sequlz.define('Item', {
        id: { type: Sequelize.BIGINT,
            primaryKey: true, unique: true, allowNull: false,
            autoIncrement: true },
        description: { type: Sequelize.STRING,
            allowNull: true },
        done: { type: Sequelize.BOOLEAN,
            allowNull: true }
    }, {
        timestamps: false,
        freezeTableName: true
    });
    
    if (process.env.DATABASE_INIT == 'true') {
        Item.sync({ force: true }).then(function() {
            callback();
        }).catch(function(err) {
            callback(err);
        });
    }
}
 
exports.disconnect = function(callback) {
    //XXX shouln'd to something to close or release the db connection?
    callback();
}
 
exports.create = function(description, done, callback) {
    Item.create({
        //id: id,
        description: description,
        done: (done) ? true : false
    }).then(function(item) {
        callback(null, item);
    }).catch(function(err) {
        callback(err);
    });
}
 
exports.update = function(key, description, done, callback) {
    Item.find({ where:{ id: key } }).then(function(item) {
        if (!item) {
            callback(new Error("Nothing found for key " + key));
        }
        else {
            item.updateAttributes({
                description: description,
                done: (done) ? true : false
            }).then(function() {
                callback(null, item);
            }).error(function(err) {
                callback(err);
            });
        }
    }).catch(function(err) {
        callback(err);
    });
}
 
 
exports.read = function(key, callback) {
    Item.find({ where:{ id: key } }).then(function(item) {
        if (!item) {
            callback(new Error("Nothing found for key " + key));
        }
        else {
            //XXX why recreating the item object?
            callback(null, {
                id: item.id,
                description: item.description,
                done: item.done
            });
        }
    }).catch(function(err) {
        callback(err);
    });
}
 
exports.destroy = function(key, callback) {
    Item.find({ where:{ id: key } }).then(function(item) {
        if (!item) {
            callback(new Error("Nothing found for " + key));
        }
        else {
            item.destroy().then(function() {
                callback(null, item);
            }).error(function(err) {
                callback(err);
            });
        }
    }).catch(function(err) {
        callback(err);
    });
}
 
exports.countAll = function(callback) {
    Item.findAll({
        attributes: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'no_items']]
    }).then(function(n) {
        callback(null, n[0].get('no_items'));
    }).catch(function(err) {
        callback(err);
    });
}
 
exports.listAll = function(page, sortField, sortDirection, callback) {
    Item.findAll({ offset: 10 * (page - 1), limit: 10,  order: [[sortField, sortDirection]] }).then(function(items) {
        var theitems = [];
        items.forEach(function(item) {
            //XXX why recreating the item objects for theitems?
            theitems.push({
                id: item.id, description: item.description, done: item.done });
        });
        callback(null, theitems);
    }).catch(function(err) {
        callback(err);
    });
}