-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
182 lines (148 loc) · 3.88 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*!
* Express-mongoose
* Copyright(c) 2011 LearnBoost <[email protected]>
* MIT Licenced
*/
/**
* Version.
*/
exports.version = JSON.parse(require('fs').readFileSync(__dirname + '/package.json')).version;
/**
* Module dependencies.
*/
var express = require('express')
, Promise = require('mongoose').Promise
, Query = require('mongoose').Query
, slice = require('sliced')
/**
* Wrap response.render with support for mongoose
* Queries and Promises.
*/
var render = express.response.render;
express.response.render = function expressmongoose_render (view, options, callback) {
if (!options || 'function' == typeof options) {
return render.call(this, view, options, callback);
}
var self = this;
return resolve(options, function (err, result) {
if (err) {
return 'function' == typeof callback
? callback(err)
: self.req.next(err);
}
// must return here so partials always work
return render.call(self, view, result, callback);
});
}
/**
* Add Promise/Query support to res.send.
*/
var send = express.response.send;
express.response.send = function expressmongoose_send () {
var args = slice(arguments);
var self = this;
function handleResult (err, result) {
if (err) return self.req.next(err);
args[0] = result;
send.apply(self, args);
}
if (args[0] instanceof Promise) {
return args[0].addBack(handleResult);
}
if (args[0] instanceof Query) {
return args[0].exec(handleResult);
}
if (args[0] && 'Object' == args[0].constructor.name) {
return resolve(args[0], handleResult);
}
send.apply(this, args);
};
/**
* Extends res.redirect with mongoose Promise support.
*
* Does not accept Queries since those return documents.
* Instead, manually handle the result of your query first,
* then resolve your promise with the url and optional status.
*
* var promise = new mongoose.Promise;
* res.redirect(promise);
*
* // later...
* promise.complete(url [, status]);
*
* The promise may pass an optional status code as the
* first argument.
*
* promise.complete(301, '/elsewhere');
*/
var redirect = express.response.redirect;
express.response.redirect = function expressmongoose_redirect () {
var self = this;
var args = slice(arguments);
function handleResult (err, url, code) {
if (err) return self.req.next(err);
if ('string' != typeof url) {
return self.req.next(new Error('URL Expected'));
}
args[0] = url;
if (code) args[1] = code;
redirect.apply(self, args);
}
if (args[0] instanceof Promise) {
return args[0].addBack(handleResult);
}
redirect.apply(this, args);
}
// TODO res.json
// TODO res.jsonp
/**
* Resolves any Queries and Promises within the passed options.
* @api private
*/
function resolve (options, callback, nested) {
var keys = Object.keys(options)
, i = keys.length
, remaining = []
, pending
, item
, key;
while (i--) {
key = keys[i];
item = options[key];
if (item instanceof Query || item instanceof Promise) {
item.key = key;
remaining.push(item);
}
}
pending = remaining.length;
if (options.locals) ++pending;
if (!pending) {
return callback(null, options);
}
function error (err) {
if (error.ran) return;
callback(error.ran = err);
}
remaining.forEach(function (item) {
function handleResult (err, result) {
if (err) return error(err);
options[item.key] = result;
--pending || callback(null, options);
}
if (item instanceof Query) {
item.exec(handleResult);
} else {
item.addBack(handleResult);
}
});
if (nested) return;
// locals support
if (options.locals) {
return resolve(options.locals, function (err, resolved) {
if (err) return error(err);
options.locals = resolved;
if (--pending) return;
return callback(null, options);
}, true);
}
}