Use restify

This commit is contained in:
Typicode
2014-03-18 20:25:13 +01:00
parent 0dfe6b539d
commit 25b2e43648
13 changed files with 283 additions and 679 deletions

60
src/utils.js Normal file
View File

@ -0,0 +1,60 @@
var low = require('low')
var _ = require('underscore')
_.mixin(require('underscore.inflections'))
// Turns string to native.
// Example:
// 'true' -> true
// '1' -> 1
function toNative(value) {
if (value === 'true' || value === 'false') {
return value === 'true'
} else if (!isNaN(+value)) {
return +value
} else {
return value
}
}
// Creates incremental id.
function createId(coll) {
if (_.isEmpty(coll)) {
return 1
} else {
return _.max(coll, function(doc) {
return doc.id
}).id + 1
}
}
// Removes empty relations
function clean() {
var toBeRemoved = []
_(low.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
})
}
}
})
})
})
_(toBeRemoved).each(function(item) {
low(item.collName).remove(item.id);
})
}
module.exports = {
toNative: toNative,
createId: createId,
clean: clean
}