mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
replaced underscore with lodash, expanded querying capability to search through nested objects and arrays
This commit is contained in:
@ -14,12 +14,12 @@
|
||||
"errorhandler": "^1.2.0",
|
||||
"express": "^4.9.5",
|
||||
"got": "^1.2.2",
|
||||
"lodash": "^3.9.1",
|
||||
"lowdb": "^0.7.1",
|
||||
"method-override": "^2.1.2",
|
||||
"morgan": "^1.3.1",
|
||||
"node-uuid": "^1.4.2",
|
||||
"pluralize": "^1.1.2",
|
||||
"underscore": "^1.5.2",
|
||||
"underscore-db": "^0.8.0",
|
||||
"update-notifier": "^0.2.2",
|
||||
"yargs": "^1.3.1"
|
||||
|
@ -1,7 +1,7 @@
|
||||
var express = require('express')
|
||||
var methodOverride = require('method-override')
|
||||
var bodyParser = require('body-parser')
|
||||
var _ = require('underscore')
|
||||
var _ = require('lodash')
|
||||
var low = require('lowdb')
|
||||
var pluralize = require('pluralize')
|
||||
var utils = require('./utils')
|
||||
@ -78,7 +78,7 @@ module.exports = function (source) {
|
||||
array = db(req.params.resource).filter(function (obj) {
|
||||
for (var key in obj) {
|
||||
var value = obj[key]
|
||||
if (_.isString(value) && value.toLowerCase().indexOf(q) !== -1) {
|
||||
if(utils.deepQuery(value, q)){
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
61
src/utils.js
61
src/utils.js
@ -1,4 +1,4 @@
|
||||
var _ = require('underscore')
|
||||
var _ = require('lodash')
|
||||
var uuid = require('node-uuid')
|
||||
var pluralize = require('pluralize')
|
||||
|
||||
@ -6,15 +6,15 @@ var pluralize = require('pluralize')
|
||||
// Example:
|
||||
// 'true' -> true
|
||||
// '1' -> 1
|
||||
function toNative (value) {
|
||||
if (typeof value === 'string') {
|
||||
if (value === ''
|
||||
|| value.trim() !== value
|
||||
|| (value.length > 1 && value[0] === '0')) {
|
||||
function toNative(value) {
|
||||
if(typeof value === 'string') {
|
||||
if(value === ''
|
||||
|| value.trim() !== value
|
||||
|| (value.length > 1 && value[0] === '0')) {
|
||||
return value
|
||||
} else if (value === 'true' || value === 'false') {
|
||||
} else if(value === 'true' || value === 'false') {
|
||||
return value === 'true'
|
||||
} else if (!isNaN(+value)) {
|
||||
} else if(!isNaN(+value)) {
|
||||
return +value
|
||||
}
|
||||
}
|
||||
@ -22,15 +22,15 @@ function toNative (value) {
|
||||
}
|
||||
|
||||
// Return incremented id or uuid
|
||||
function createId (coll) {
|
||||
if (_.isEmpty(coll)) {
|
||||
function createId(coll) {
|
||||
if(_.isEmpty(coll)) {
|
||||
return 1
|
||||
} else {
|
||||
var id = _.max(coll, function (doc) {
|
||||
var id = _.max(coll, function(doc) {
|
||||
return doc.id
|
||||
}).id
|
||||
|
||||
if (_.isFinite(id)) {
|
||||
if(_.isFinite(id)) {
|
||||
// Increment integer id
|
||||
return ++id
|
||||
} else {
|
||||
@ -42,19 +42,19 @@ function createId (coll) {
|
||||
|
||||
// Returns document ids that have unsatisfied relations
|
||||
// Example: a comment that references a post that doesn't exist
|
||||
function getRemovable (db) {
|
||||
function getRemovable(db) {
|
||||
var removable = []
|
||||
|
||||
_(db).each(function (coll, collName) {
|
||||
_(coll).each(function (doc) {
|
||||
_(doc).each(function (value, key) {
|
||||
if (/Id$/.test(key)) {
|
||||
_(db).each(function(coll, collName) {
|
||||
_(coll).each(function(doc) {
|
||||
_(doc).each(function(value, key) {
|
||||
if(/Id$/.test(key)) {
|
||||
var refName = pluralize.plural(key.slice(0, -2))
|
||||
// Test if table exists
|
||||
if (db[refName]) {
|
||||
if(db[refName]) {
|
||||
// Test if references is defined in table
|
||||
var ref = _.findWhere(db[refName], {id: value})
|
||||
if (_.isUndefined(ref)) {
|
||||
if(_.isUndefined(ref)) {
|
||||
removable.push({name: collName, id: doc.id})
|
||||
}
|
||||
}
|
||||
@ -66,8 +66,29 @@ function getRemovable (db) {
|
||||
return removable
|
||||
}
|
||||
|
||||
function deepQuery(value, q) {
|
||||
if(value) {
|
||||
if(_.isArray(value)) {
|
||||
for(var i = 0; i < value.length; i++) {
|
||||
if(deepQuery(value[i], q)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else if(_.isPlainObject(value)) {
|
||||
for(var k in value) {
|
||||
if(deepQuery(value[k], q)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else if(value.toString().toLowerCase().indexOf(q) !== -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
toNative: toNative,
|
||||
createId: createId,
|
||||
getRemovable: getRemovable
|
||||
getRemovable: getRemovable,
|
||||
deepQuery: deepQuery
|
||||
}
|
||||
|
Reference in New Issue
Block a user