Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 2a9ba185dfda51c5eb9f1f469ed77ca0b2c19282 0 parents
@zeekay authored
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules/
+npm-debug.log
+lib/
4 .npmignore
@@ -0,0 +1,4 @@
+node_modules/
+Cakefile
+test/
+src/
3  README.md
@@ -0,0 +1,3 @@
+# ex-mongoose
+
+Automagically generate Backbone-compatible RESTful API from Mongoose models.
13 package.json
@@ -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 src/index.coffee
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.