mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
Refactor
This commit is contained in:
@ -73,7 +73,13 @@ routes.update = function(req, res, next) {
|
||||
// DELETE /:resource/:id
|
||||
routes.destroy = function(req, res, next) {
|
||||
low(req.params.resource).remove(+req.params.id)
|
||||
utils.clean()
|
||||
|
||||
// Remove dependents documents
|
||||
var removable = utils.getRemovable(low.db)
|
||||
|
||||
_(removable).each(function(item) {
|
||||
low(item[0]).remove(item[1]);
|
||||
})
|
||||
|
||||
res.send(204)
|
||||
}
|
||||
|
26
src/utils.js
26
src/utils.js
@ -28,33 +28,31 @@ function createId(coll) {
|
||||
}
|
||||
}
|
||||
|
||||
// Removes empty relations
|
||||
function clean() {
|
||||
var toBeRemoved = []
|
||||
|
||||
_(low.db).each(function(coll, collName) {
|
||||
// Returns document that are have unsatisfied relations
|
||||
// Example: comment that references a post that doesn't exist
|
||||
function getRemovable(db) {
|
||||
var removable = []
|
||||
|
||||
_(db).each(function(coll, collName) {
|
||||
_(coll).each(function(doc) {
|
||||
_(doc).each(function(value, key) {
|
||||
if (/Id$/.test(key)) {
|
||||
var reference = _.pluralize(key.slice(0, - 2))
|
||||
if (!_.isUndefined(low(reference).get(doc[key]).value())) {
|
||||
toBeRemoved.push({
|
||||
collName: collName,
|
||||
id: doc.id
|
||||
})
|
||||
var refName = _.pluralize(key.slice(0, - 2))
|
||||
var ref = _.findWhere(db[refName], {id: value})
|
||||
if (_.isUndefined(ref)) {
|
||||
removable.push([collName, doc.id])
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
_(toBeRemoved).each(function(item) {
|
||||
low(item.collName).remove(item.id);
|
||||
})
|
||||
return removable
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
toNative: toNative,
|
||||
createId: createId,
|
||||
clean: clean
|
||||
getRemovable: getRemovable
|
||||
}
|
@ -2,7 +2,6 @@ var request = require('supertest')
|
||||
var assert = require('assert')
|
||||
var low = require('lowdb')
|
||||
var server = require('../src/server')
|
||||
var db
|
||||
|
||||
describe('Server', function() {
|
||||
|
||||
@ -19,6 +18,7 @@ describe('Server', function() {
|
||||
{id: 2, published: false, postId: 1},
|
||||
{id: 3, published: false, postId: 2},
|
||||
{id: 4, published: false, postId: 2},
|
||||
{id: 5, published: false, postId: 2},
|
||||
]
|
||||
})
|
||||
|
||||
@ -141,7 +141,7 @@ describe('Server', function() {
|
||||
.end(function(err, res){
|
||||
if (err) return done(err)
|
||||
assert.equal(low.db.posts.length, 1)
|
||||
assert.equal(low.db.comments.length, 2)
|
||||
assert.equal(low.db.comments.length, 3)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
31
test/utils.js
Normal file
31
test/utils.js
Normal file
@ -0,0 +1,31 @@
|
||||
var assert = require('assert')
|
||||
var utils = require('../src/utils')
|
||||
|
||||
describe('utils', function() {
|
||||
|
||||
describe('getRemovable', function() {
|
||||
|
||||
it('should return removable documents', function() {
|
||||
|
||||
var db = {
|
||||
posts: [
|
||||
{id: 1, comment: 1}
|
||||
],
|
||||
comments: [
|
||||
{id: 1, postId: 1},
|
||||
// Comments below references a post that doesn't exist
|
||||
{id: 2, postId: 2},
|
||||
{id: 3, postId: 2},
|
||||
]
|
||||
}
|
||||
|
||||
var expected = [
|
||||
['comments', 2],
|
||||
['comments', 3]
|
||||
]
|
||||
|
||||
assert.deepEqual(expected, utils.getRemovable(db))
|
||||
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user