-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2a9ba18
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
npm-debug.log | ||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
Cakefile | ||
test/ | ||
src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ex-mongoose | ||
|
||
Automagically generate Backbone-compatible RESTful API from Mongoose models. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "modelist", | ||
"version": "0.0.1", | ||
"main": "lib/", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"license": "BSD", | ||
"description": "Automagically generate Backbone-compatible RESTful API from Mongoose models.", | ||
"dependencies": { | ||
"mongoose": "~2.7.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
mongoose = require 'mongoose' | ||
|
||
module.exports = (name, schema = {}) -> | ||
lower = name.toLowerCase() | ||
upper = name.charAt(0).toUpperCase() + lower.substr 1 | ||
|
||
# pluralize lowerCase | ||
plural = lower | ||
if plural.charAt(plural.length-1) != 's' | ||
plural += 's' | ||
|
||
_Model = new mongoose.Schema schema | ||
|
||
Model = mongoose.model upper, _Model | ||
Model.routes = -> | ||
# Generate Restful API | ||
|
||
# Create | ||
@post "/api/#{plural}", -> | ||
instance = @body | ||
new Model(model).save (err) => | ||
if not err | ||
@json model, 201 | ||
|
||
# List | ||
@get "/api/#{plural}", -> | ||
Model.find {}, (err, model) => | ||
@json model | ||
|
||
# Get individual | ||
@get "/api/#{plural}/:id", (id) -> | ||
Model.findOne {id: id}, (err, model) => | ||
@json model | ||
|
||
# Update | ||
@put "/api/#{plural}/:id", (id) -> | ||
model = @body | ||
Model.update {id: id}, model, {}, (err, num) => | ||
@json (if err then 404 else 200) | ||
|
||
# Delete | ||
@del "/api/#{plural}/:id", (id) -> | ||
Model.findOne {id: id}, (err, model) => | ||
if not err | ||
model.remove() | ||
@json (if err then 404 else 204) | ||
|
||
Model |