-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
123 lines (106 loc) · 2.21 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
// Generated by CoffeeScript 1.8.0
var Sequelize, destroy, get, getRestTable, init, insert, list, restTable, sendObjResponse, update;
Sequelize = require("sequelize");
restTable = null;
init = function() {
var dbSql;
dbSql = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: "./rest.sqlite",
logging: false
});
return restTable = dbSql.define('Rest', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
text: Sequelize.STRING(200),
priority: Sequelize.INTEGER
});
};
getRestTable = function() {
return restTable;
};
sendObjResponse = function(rows, callback) {
var ar, retobj, row, _i, _len;
ar = [];
for (_i = 0, _len = rows.length; _i < _len; _i++) {
row = rows[_i];
ar.push(row.dataValues);
}
retobj = {
success: true,
total: ar.length,
data: ar
};
if (callback) {
callback(retobj);
}
};
list = function(callback) {
restTable.findAll({
order: 'id'
}).then(function(rows) {
sendObjResponse(rows, callback);
});
};
get = function(recid, callback) {
restTable.findAll({
where: {
id: recid
}
}).then(function(rows) {
sendObjResponse(rows, callback);
});
};
insert = function(insertObj, callback) {
restTable.max('id').then(function(retval) {
var nextid;
nextid = retval + 1;
insertObj.id = nextid;
restTable.create(insertObj).complete(function(err, results) {
var retobj;
retobj = {
success: true
};
if (err) {
retobj.success = false;
console.log('error: ', err);
}
callback(retobj);
});
});
};
update = function(updateObj, callback) {
restTable.update(updateObj, {
where: {
id: updateObj.id
}
}).then(function() {
var retobj;
retobj = {
success: true
};
callback(retobj);
});
};
destroy = function(recid, callback) {
restTable.destroy({
where: {
id: recid
}
}).then(function() {
var retobj;
retobj = {
success: true
};
callback(retobj);
});
};
exports.init = init;
exports.list = list;
exports.get = get;
exports.destroy = destroy;
exports.insert = insert;
exports.update = update;
exports.getRestTable = getRestTable;