koa-mongo-rest
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -0,4 +1,11 @@ | ||
var api, app, koa, logger, mongoUrl, router, schema; | ||
koa = require('koa'); | ||
router = require('koa-router'); | ||
logger = require('koa-logger'); | ||
api = require('../lib/index'); | ||
schema = require('./schema'); | ||
@@ -10,2 +17,4 @@ | ||
app.use(logger()); | ||
app.use(router(app)); | ||
@@ -12,0 +21,0 @@ |
@@ -0,4 +1,8 @@ | ||
var parse; | ||
parse = require('co-body'); | ||
module.exports = function(model) { | ||
return { | ||
get: function*() { | ||
findAll: function*() { | ||
var error, result; | ||
@@ -13,3 +17,3 @@ try { | ||
}, | ||
getById: function*() { | ||
findById: function*() { | ||
var error, result; | ||
@@ -34,7 +38,13 @@ try { | ||
}, | ||
putToId: function*() { | ||
var error; | ||
replaceById: function*() { | ||
var body, error, newDocument, result; | ||
try { | ||
console.log("Putin"); | ||
return this.body = "Putin"; | ||
yield model.findByIdAndRemove(this.params.id).exec(); | ||
body = yield parse(this, { | ||
limit: '1kb' | ||
}); | ||
newDocument = body; | ||
newDocument._id = this.params.id; | ||
result = yield model.create(newDocument).exec(); | ||
return this.body = result; | ||
} catch (_error) { | ||
@@ -45,8 +55,32 @@ error = _error; | ||
}, | ||
replaceByIdWithQuery: function*() { | ||
var error, newDocument, result; | ||
try { | ||
yield model.findByIdAndRemove(this.params.id).exec(); | ||
newDocument = this.request.query; | ||
newDocument._id = this.params.id; | ||
result = yield model.create(newDocument).exec(); | ||
return this.body = result; | ||
} catch (_error) { | ||
error = _error; | ||
return this.body = error; | ||
} | ||
}, | ||
updateById: function*() { | ||
var body, error, result; | ||
try { | ||
body = yield parse(this, { | ||
limit: '1kb' | ||
}); | ||
result = yield model.findByIdAndUpdate(this.params.id, body).exec(); | ||
return this.body = result; | ||
} catch (_error) { | ||
error = _error; | ||
return this.body = error; | ||
} | ||
}, | ||
updateByIdWithQuery: function*() { | ||
var error, result; | ||
try { | ||
result = yield model.findByIdAndUpdate(this.params.id, { | ||
$set: this.request.query | ||
}).exec(); | ||
result = yield model.findByIdAndUpdate(this.params.id, this.request.query).exec(); | ||
return this.body = result; | ||
@@ -58,7 +92,10 @@ } catch (_error) { | ||
}, | ||
postToId: function*() { | ||
var error; | ||
create: function*() { | ||
var body, error, result; | ||
try { | ||
console.log("posting"); | ||
return this.body = "posting"; | ||
body = yield parse(this, { | ||
limit: '1kb' | ||
}); | ||
result = yield model.create(body).exec(); | ||
return this.body = result; | ||
} catch (_error) { | ||
@@ -68,4 +105,14 @@ error = _error; | ||
} | ||
}, | ||
createWithQuery: function*() { | ||
var error, result; | ||
try { | ||
result = yield model.create(this.request.query).exec(); | ||
return this.body = result; | ||
} catch (_error) { | ||
error = _error; | ||
return this.body = error; | ||
} | ||
} | ||
}; | ||
}; |
var routes; | ||
routes = function(app, schema, actions) { | ||
app.get("/" + schema.collectionName, actions.get); | ||
app.get("/" + schema.collectionName + "/:id", actions.getById); | ||
app.get("/" + schema.collectionName + "/find", actions.findAll); | ||
app.get("/" + schema.collectionName + "/find/:id", actions.findById); | ||
app.get("/" + schema.collectionName + "/create", actions.createWithQuery); | ||
app.get("/" + schema.collectionName + "/destroy/:id", actions.deleteById); | ||
app.get("/" + schema.collectionName + "/update/:id", actions.updateByIdWithQuery); | ||
app.get("/" + schema.collectionName + "/replace/:id", actions.replaceByIdWithQuery); | ||
app.get("/" + schema.collectionName, actions.findAll); | ||
app.get("/" + schema.collectionName + "/:id", actions.findById); | ||
app.post("/" + schema.collectionName, actions.create); | ||
app.post("/" + schema.collectionName + "/:id", actions.updateById); | ||
app["delete"]("/" + schema.collectionName + "/:id", actions.deleteById); | ||
app.put("/" + schema.collectionName + "/:id", actions.putToId); | ||
app.patch("/" + schema.collectionName + "/:id", actions.updateById); | ||
return app.post("/" + schema.collectionName, actions.postToId); | ||
app.put("/" + schema.collectionName + "/", actions.create); | ||
app.put("/" + schema.collectionName + "/:id", actions.replaceById); | ||
return app.patch("/" + schema.collectionName + "/:id", actions.updateById); | ||
}; | ||
module.exports = routes; |
{ | ||
"name": "koa-mongo-rest", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "generate REST API with koa and mongo", | ||
@@ -32,6 +32,7 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"mongoose": "~3.8.3" | ||
"mongoose": "~3.8.3", | ||
"co-body": "0.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.16.2", | ||
"mocha": "~1.17.0", | ||
"grunt": "~0.4.2", | ||
@@ -42,4 +43,6 @@ "grunt-contrib-coffee": "git://github.com/t3chnoboy/grunt-contrib-coffee.git", | ||
"grunt-concurrent": "~0.4.2", | ||
"koa-router": "~2.2.0" | ||
"koa-router": "~2.3.1", | ||
"koa": "~0.3.0", | ||
"koa-logger": ">=1.1.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11213
16
173
2
9
+ Addedco-body@0.0.1
+ Addedbytes@1.0.0(transitive)
+ Addedco-body@0.0.1(transitive)
+ Addedqs@0.6.6(transitive)
+ Addedraw-body@1.1.7(transitive)
+ Addedstring_decoder@0.10.31(transitive)