Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeekay committed Jul 30, 2012
0 parents commit 2a9ba18
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
npm-debug.log
lib/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
Cakefile
test/
src/
3 changes: 3 additions & 0 deletions README.md
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.
13 changes: 13 additions & 0 deletions package.json
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"
}
}
48 changes: 48 additions & 0 deletions src/index.coffee
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

0 comments on commit 2a9ba18

Please sign in to comment.