Richard Allred
2019-08-03 99e73490b45f1f76040216418afeec9ff9770e57
commit | author | age
e5e367 1 var Sequelize = require("sequelize");
SU 2
3 var Item = undefined;
4
5 module.exports.connect = function(params, callback) {
6     var sequlz = new Sequelize(
7         params.dbname, params.username, params.password,
8         params.params);
9     Item = sequlz.define('Item', {
10         id: { type: Sequelize.BIGINT,
11             primaryKey: true, unique: true, allowNull: false,
12             autoIncrement: true },
13         description: { type: Sequelize.STRING,
14             allowNull: true },
15         done: { type: Sequelize.BOOLEAN,
16             allowNull: true }
17     }, {
18         timestamps: false,
19         freezeTableName: true
20     });
21     
22     if (process.env.DATABASE_INIT == 'true') {
23         Item.sync({ force: true }).then(function() {
24             callback();
25         }).catch(function(err) {
26             callback(err);
27         });
28     }
29 }
30
31 exports.disconnect = function(callback) {
32     //XXX shouln'd to something to close or release the db connection?
33     callback();
34 }
35
36 exports.create = function(description, done, callback) {
37     Item.create({
38         //id: id,
39         description: description,
40         done: (done) ? true : false
41     }).then(function(item) {
42         callback(null, item);
43     }).catch(function(err) {
44         callback(err);
45     });
46 }
47
48 exports.update = function(key, description, done, callback) {
49     Item.find({ where:{ id: key } }).then(function(item) {
50         if (!item) {
51             callback(new Error("Nothing found for key " + key));
52         }
53         else {
54             item.updateAttributes({
55                 description: description,
56                 done: (done) ? true : false
57             }).then(function() {
58                 callback(null, item);
59             }).error(function(err) {
60                 callback(err);
61             });
62         }
63     }).catch(function(err) {
64         callback(err);
65     });
66 }
67
68
69 exports.read = function(key, callback) {
70     Item.find({ where:{ id: key } }).then(function(item) {
71         if (!item) {
72             callback(new Error("Nothing found for key " + key));
73         }
74         else {
75             //XXX why recreating the item object?
76             callback(null, {
77                 id: item.id,
78                 description: item.description,
79                 done: item.done
80             });
81         }
82     }).catch(function(err) {
83         callback(err);
84     });
85 }
86
87 exports.destroy = function(key, callback) {
88     Item.find({ where:{ id: key } }).then(function(item) {
89         if (!item) {
90             callback(new Error("Nothing found for " + key));
91         }
92         else {
93             item.destroy().then(function() {
94                 callback(null, item);
95             }).error(function(err) {
96                 callback(err);
97             });
98         }
99     }).catch(function(err) {
100         callback(err);
101     });
102 }
103
104 exports.countAll = function(callback) {
105     Item.findAll({
106         attributes: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'no_items']]
107     }).then(function(n) {
108         callback(null, n[0].get('no_items'));
109     }).catch(function(err) {
110         callback(err);
111     });
112 }
113
114 exports.listAll = function(page, sortField, sortDirection, callback) {
115     Item.findAll({ offset: 10 * (page - 1), limit: 10,  order: [[sortField, sortDirection]] }).then(function(items) {
116         var theitems = [];
117         items.forEach(function(item) {
118             //XXX why recreating the item objects for theitems?
119             theitems.push({
120                 id: item.id, description: item.description, done: item.done });
121         });
122         callback(null, theitems);
123     }).catch(function(err) {
124         callback(err);
125     });
126 }
127