-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.js
153 lines (125 loc) · 4.17 KB
/
operations.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
var model = require('./model');
var templates = require('./templates');
var ObjectId = require('mongodb').ObjectID;
var METHODS = [
'create',
'read',
'update',
'delete'
];
for (var i in METHODS) {
(function(method) {
// operations
exports[method] = function (link) {
model(createRequest(method, link), createResponseHandler(method, link));
};
// listeners
var serverEvent = 'crud.' + method;
M.on(serverEvent, function (request, callback) {
request.method = method;
request.options = request.options || {};
request.templateId = typeof request.templateId === 'string' ? ObjectId(request.templateId) : request.templateId;
if (!callback) {
callback = function(err) {
if (err) {
console.error('Error executing server operation: ' + serverEvent);
console.error('******************')
console.error(err);
console.error('------------------')
console.error(request);
console.error('******************')
}
};
}
model(request, function(err, data) {
if (request.noCursor && data && typeof data.toArray === 'function') {
data.toArray(function(err, data) {
callback(err, data);
});
return;
}
callback(err, data);
});
});
})(METHODS[i]);
}
// init operation
exports.init = function (link) {
// init templates
templates.init(link.params, function (err) {
if (err) {
return link.send(err.statusCode, err);
}
return link.send(200);
});
}
// private functions
function createRequest (method, link) {
var data = link.data || {};
// template id is mandatory
if (!data.t) {
return null;
}
// Get role or set the public role
var role = link.session.crudRole;
if (link.session._rid === M.config.app.publicRole && !link.session._uid) {
link.session.crudRole = role = ObjectId('000000000000000000000001');
}
var request = {
role: role,
options: {},
templateId: typeof data.t === 'string' ? ObjectId(data.t) : data.t,
method: method,
session: link.session,
// TODO remove this when updates on linked fields are possible
noJoins: data.noJoins,
onlyCount: data.onlyCount
};
// query
request.query = data.q || {};
request.query._tp = data.t;
// update
if (data.d && data.d.constructor.name === 'Object') {
// set type
if (method === 'create') {
data.d._tp = data.t;
}
request.data = data.d;
}
// options
if (data.o && data.o.constructor.name === 'Object') {
request.options = data.o;
} else {
request.options = {};
}
return request;
}
function createResponseHandler (method, link) {
return function(err, results) {
if (err) {
return link.send(err.statusCode || 500, err.message || err.toString() || err);
}
link.res.headers['content-type'] = 'application/json; charset=utf-8';
// TODO How can this be fixed using a better way?
var constructorNameOfResults = results.constructor.name;
if (results && constructorNameOfResults === 'Object' && typeof results.toArray === 'function') {
constructorNameOfResults = 'Cursor';
}
if (method === 'read' && constructorNameOfResults === 'Cursor') {
// stream result
var stream = results.stream();
link.stream.start(200);
stream.on('end', function() {
link.stream.end();
});
stream.on('error', function(err) {
link.stream.error(500, err.toString());
});
stream.on('data', function(data) {
link.stream.data(data);
});
} else {
link.send(200, results);
}
};
}