Skip to content

Commit bc59f2b

Browse files
committed
Merge pull request typicode#101 from ryancrosser/master
Expanded Querying
2 parents 7af6e40 + c782e82 commit bc59f2b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
"errorhandler": "^1.2.0",
1515
"express": "^4.9.5",
1616
"got": "^1.2.2",
17+
"lodash": "^3.9.1",
1718
"lowdb": "^0.7.1",
1819
"method-override": "^2.1.2",
1920
"morgan": "^1.3.1",
2021
"node-uuid": "^1.4.2",
2122
"pluralize": "^1.1.2",
22-
"underscore": "^1.5.2",
23+
"underscore": "^1.8.3",
2324
"underscore-db": "^0.8.0",
2425
"update-notifier": "^0.2.2",
2526
"yargs": "^1.3.1"

src/router.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = function (source) {
7878
array = db(req.params.resource).filter(function (obj) {
7979
for (var key in obj) {
8080
var value = obj[key]
81-
if (_.isString(value) && value.toLowerCase().indexOf(q) !== -1) {
81+
if (utils.deepQuery(value, q)) {
8282
return true
8383
}
8484
}
@@ -101,7 +101,8 @@ module.exports = function (source) {
101101
filters[key] = utils.toNative(req.query[key])
102102
}
103103
}
104-
104+
console.log(filters)
105+
console.log()
105106
// Filter
106107
if (_(filters).isEmpty()) {
107108
array = db(req.params.resource).value()

src/utils.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var pluralize = require('pluralize')
99
function toNative (value) {
1010
if (typeof value === 'string') {
1111
if (value === ''
12-
|| value.trim() !== value
13-
|| (value.length > 1 && value[0] === '0')) {
12+
|| value.trim() !== value
13+
|| (value.length > 1 && value[0] === '0')) {
1414
return value
1515
} else if (value === 'true' || value === 'false') {
1616
return value === 'true'
@@ -44,7 +44,7 @@ function createId (coll) {
4444
// Example: a comment that references a post that doesn't exist
4545
function getRemovable (db) {
4646
var removable = []
47-
47+
console.log(db)
4848
_(db).each(function (coll, collName) {
4949
_(coll).each(function (doc) {
5050
_(doc).each(function (value, key) {
@@ -66,8 +66,29 @@ function getRemovable (db) {
6666
return removable
6767
}
6868

69+
function deepQuery (value, q) {
70+
if (value && q) {
71+
if (_.isArray(value)) {
72+
for (var i = 0; i < value.length; i++) {
73+
if (deepQuery(value[i], q)) {
74+
return true
75+
}
76+
}
77+
} else if (_.isObject(value) && !_.isArray(value)) {
78+
for (var k in value) {
79+
if (deepQuery(value[k], q)) {
80+
return true
81+
}
82+
}
83+
} else if (value.toString().toLowerCase().indexOf(q) !== -1) {
84+
return true
85+
}
86+
}
87+
}
88+
6989
module.exports = {
7090
toNative: toNative,
7191
createId: createId,
72-
getRemovable: getRemovable
92+
getRemovable: getRemovable,
93+
deepQuery: deepQuery
7394
}

0 commit comments

Comments
 (0)