-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod.js
160 lines (152 loc) · 4.66 KB
/
method.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var functionForItem = function (item) {
if (typeof item !== 'function') {
return function (req) {
return item;
};
}
return item;
};
var dbErrorHandler = function(req, res, err) {
res.respondPlainText("Internal Server Error " + err, 501);
};
var Method = function() {
this.handler = function(req,res) {
res.respondPlainText("This is a new Method!");
};
this.name = "";
this.needsAuth = false;
this.authHandler = false;
this.errorHandler = false;
this.DBWrapper.parent = this;
};
Method.prototype.handle = function (f) {
this.handler = function (req, res) {
try {
f(req,res);
} catch (err) {
if (this.errorHandler) {
errorHandler(req, res, err);
} else {
throw err;
}
}
};
};
Method.prototype.auth = function (f) {
this.authHandler = f;
if (f) {
this.needsAuth = true;
}
};
Method.prototype.setHasAuth = function (b) {
this.needsAuth = b;
};
Method.prototype.onError = function (err) {
this.errorHandler = err;
};
Method.prototype.DBWrapper = {
findAll: function (table, query, map, status, headers, error) {
var DB = require('aeolus').DB;
map = map || function (a) { return a; };
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.findAll(t, q, function(items) {
res.respondJSON(items.map(map), status, headers);
}, function(err) {
error(req,res,err);
});
});
},
find: function (table, query, map, status, headers, error) {
var DB = require('aeolus').DB;
map = map || function (a) { return a; };
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.find(t, q, function(items) {
res.respondJSON(map(items), status, headers);
}, function(err) {
error(req,res,err);
});
});
},
insert: function (table, query, status, headers, error) {
var DB = require('aeolus').DB;
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.insert(t, q, function(already) {
if (already)
res.respondPlainText("Item was already in the DB", status, headers);
else
res.respondPlainText("Item succesfully added.", status, headers);
}, function(err) {
error(req,res,err);
});
});
},
edit: function (table, query, map, status, headers, error) {
var DB = require('aeolus').DB;
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.edit(t, q, function() {
res.respondPlainText("Items have been saved.");
}, function(err) {
error(req,res,err);
});
});
},
map: function (table, query, map, status, headers, error) {
var DB = require('aeolus').DB;
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.map(t, q, function() {
res.respondPlainText("Items have been saved.");
}, function(err) {
error(req,res,err);
});
});
},
reduce: function (table, query, reduce, initialValue, status, headers, error) {
var DB = require('aeolus').DB;
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.reduce(t, q, reduce, initialValue,function() {
res.respondPlainText("Items have been saved.");
}, function(err) {
error(req,res,err);
});
});
},
delete: function (table, query, status, headers, error) {
var DB = require('aeolus').DB;
table = functionForItem(table);
query = functionForItem(query);
error = error || this.parent.errorHandler || dbErrorHandler;
this.parent.handle(function(req, res) {
var t = table(req), q = query(req);
DB.delete(t, q,function() {
res.respondPlainText("Items has been deleted.");
}, function(err) {
error(req,res,err);
});
});
}
};
module.exports = Method;