forked from greenpioneersolutions/express-query-parameters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
213 lines (194 loc) · 5.16 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict'
var _ = require('lodash')
var autoParse = require('auto-parse')
// var setOrGet = require('set-or-get')
var auto = require('run-auto')
function autoSync (tasks, cb) {
var result = {}
var error = null
_.forEach(tasks, function (taskFn, taskName) {
taskFn(function (err, data) {
error = err
result[taskName] = data
})
if (error) { return false }
})
if (cb) {
return cb(error, result)
} else if (error) {
throw error
}
return result
}
function Build (options) {
var self = this
self.options = _.merge(require('./lib/options.js'), options)
self.schema = self.options.settings.schema ? _.uniq(self.options.settings.schema) : []
self.schemaSort = []
_.forEach(self.schema, function (key) {
self.schemaSort.push(key, '-' + key)
})
self.tasks = function (query) {
return {
filter: function (cb) {
// { content: "test", limit: 42 }
var filter = {}
_.forEach(query, function (a, keys) {
if (_.includes(self.schema, keys)) {
// TODO orderId: "012345"
filter[keys] = autoParse(a)
}
})
cb(null, filter)
},
skip: function (cb) {
cb(null, query.skip ? autoParse(query.skip) : self.options.query.skip)
},
sort: function (cb) {
cb(null, query.sort ? sortCheck(query.sort) : self.options.query.sort)
},
limit: function (cb) {
cb(null, query.limit ? limitCheck(query.limit, self.options.query.limit) : self.options.query.limit)
},
select: function (cb) {
cb(null, query.select ? selectCheck(query.select, self.schema) : self.options.query.select)
}
}
}
self.mongoose = function (query) {
return {
lean: function (cb) {
cb(null, query.lean ? autoParse(query.lean) : self.options.query.lean)
}
}
}
self.sql = function (query) {
return {
sql: function (cb) {
cb(null, null)
}
}
}
}
Build.prototype.config = function (data) {
var self = this
self.options = _.merge(self.options, data)
}
Build.prototype.parse = function (parse, cb) {
var options = this.options
var self = this
if (_.isEmpty(parse)) {
if (cb) return cb(self.options.query)
return self.options.query
}
var tasks = self.tasks(parse)
if (this.options.settings.mongoose) _.merge(tasks, self.mongoose(parse))
if (this.options.settings.sql) _.merge(tasks, self.sql(parse))
var response = autoSync(
tasks
, function (err, result) {
if (err) console.log(err)
return _.merge(options, result)
})
if (cb) {
return cb(response)
} else {
return response
}
}
Build.prototype.query = function (params) {
var options = this.options
var self = this
if (params) options = _.merge(options, params)
return function (res, req, next) {
if (_.isEmpty(req.query)) {
req.queryParameters = options
return next()
}
var tasks = self.tasks(req.query)
if (this.options.settings.mongoose) _.merge(tasks, self.mongoose(req.query))
if (this.options.settings.sql) _.merge(tasks, self.sql(req.query))
auto(
tasks
, function (err, result) {
if (err) console.log(err)
req.queryParameters = _.merge(options, result)
return next()
})
} // END OF RETURN
} // END OF QUERY
function limitCheck (number, limit) {
number = number << 0
if (!number || number > limit || number === 0) {
number = limit
}
return number
}
function sortCheck (sort) {
// V Year ^ Title ^ ...
// 1995 Bob 2
// 1995 Bob 1
// 1995 Alice
// 1995 AAA
//
// 1993 Carol
// 1993 Bob
//
// 1992 Dave
//
// sort=year&sort=-title
//
// sort: ["year", "-title"]
// .sort([
// ["year", 1],
// ["title", -1]
// ])
if (_.isArray(sort)) {
return _.map(sort, function (current) {
var direction = 1
if (_.startsWith(current, '-')) {
direction = -1
current = current.slice(1)
}
return [current, direction]
})
}
return sort
}
function selectCheck (select, schema) {
var selected = {}
select = _.isArray(select) ? select : select.split(' ')
_.forEach(select, function (current) {
if (!_.includes(schema, current)) {
return
}
var enabled = 1
if (_.startsWith(current, '-')) {
enabled = 0
current = current.slice(1)
}
selected[current] = enabled
})
return selected
}
// function errorHandler (errValue, field, value, queryParameters, defaults) { // ERRORHANDLER
// setOrGet(queryParameters.error, field, []).push({
// message: defaults.errorMessage,
// value: errValue
// })
// return value
// }
// function warningHandler (warnValue, field, value, queryParameters, defaults) { // warningHandler
// setOrGet(queryParameters.warning, field, []).push({
// message: defaults.errorMessage,
// value: errValue
// })
// return value
// }
function build (options) {
if (options === undefined || (typeof options === 'object' && options !== null)) {
return new Build(options)
}
throw new TypeError('Expected object for argument options but got ' + options)
}
module.exports = build