Permalink
Browse files

Resourcefull routing - REST

  • Loading branch information...
1 parent 78e2ba6 commit c4b22329de91aa7329d476b60140c2e3db3ec904 @hkjels committed May 27, 2012
View
@@ -3,6 +3,12 @@
__Crud-functionality for mongoose-models. Fits well with express.js__
+## PS
+
+This module is under heavy development @ the moment is not ready for production!!
+Have a look in a day or two
+
+
## Install
λ npm install crud
@@ -32,6 +38,12 @@ __Crud-functionality for mongoose-models. Fits well with express.js__
})
+## Note
+
+To utilize CRUD, you'll have to follow the naming-conventions of mongoose to
+the point. Modelnames should be in plural and they should be capitalized.
+
+
## Constributors
* Henrik Kjelsberg <hkjels@me.com>
View
90 crud.js
@@ -9,8 +9,10 @@
* Module dependencies
*/
-var mongoose = require('mongoose')
- , cycle = require('cycle')
+var cycle = require('cycle')
+ , lingo = require('lingo')
+ , en = lingo.en
+ , mongoose = require('mongoose')
/**
* Form
@@ -107,17 +109,97 @@ var form = exports.form = function (model, cb) {
}
/**
+ * Resource for use with express-resources
+ */
+
+exports.resource = {
+ index: function (req, res) {}
+
+ , new: function (req, res, next) {
+ var instance = 'user'
+ , model = lingo.capitalize(en.pluralize(instance))
+
+ console.log(req.route)
+
+ form(model, function (err, form) {
+ if (err) return next(err)
+ res.locals({
+ form: form
+ , instance: instance
+ , model: model
+ })
+ res.render('form')
+ })
+ }
+
+ /**
+ * Create
+ */
+
+ , create: function (req, res) {}
+
+ /**
+ * ≈Read
+ */
+
+ , show: function (req, res) {}
+
+ /**
+ * Edit
+ *
+ * Normalizes mongoose-model. The normalized data is found in the local named
+ * "form" and can easily be converted into a bunch of form-elements with some
+ * clever templating.
+ * @see /examples
+ */
+
+ , edit: function (req, res, next) {
+ var instance = Object.keys(req.route.params)[0]
+ , model = lingo.capitalize(en.pluralize(instance))
+
+ form(model, function (err, form) {
+ if (err) return next(err)
+ res.locals({
+ form: form
+ , instance: instance
+ , model: model
+ })
+ res.render('form')
+ })
+ }
+
+ /**
+ * Update
+ */
+
+ , update: function (req, res) {}
+
+ /**
+ * Delete
+ */
+
+ , destroy: function (req, res) {}
+
+
+}
+
+/**
* Crud express-middleware
*
* A middleware route for express that populates res.form with the fields from
* the specified model.
*/
exports.middleware = function (req, res, next) {
- var model = req.params.model
+ var instance = req.params.model.toLowerCase()
+ , model = lingo.capitalize(en.pluralize(instance))
form(model, function (err, form) {
if (err) return next(err)
- res.locals({form: form, model: model})
+ res.locals({
+ form: form
+ , instance: instance
+ , model: model
+ })
return next()
})
}
View
@@ -39,7 +39,7 @@
line-height: 0;
letter-spacing: -0.05em;
}
- p strong, pre {
+ p, pre {
display: block;
margin-bottom: 50px;
}
View
@@ -5,12 +5,13 @@
var express = require('express')
, app = module.exports = express.createServer()
- , routes = require('./routes')
+ , Resource = require('express-resource')
, models = require('./models')
, stylus = require('stylus')
, nib = require('nib')
, crud = require('../../crud')
+require('express-namespace')
/**
* Compile stylus with nib
@@ -43,17 +44,27 @@ app.configure(function(){
*/
app.helpers({
- environment: process.env.NODE_ENV
+ environment: process.env.NODE_ENV || 'development'
, title: 'Crud example'
, values: null
})
/**
+ * API
+ *
+ * CRUD supports express-namespace & express-resource
+ * This creates a one to one connection between your routes and models
+ */
+
+app.namespace('/api', function () {
+ app.resource('users', crud.resource)
+})
+
+/**
* Routes
*/
-app.get('/', routes.explain)
-app.get('/:model', crud.middleware, routes.form)
+app.get('/', function (req, res) { res.render('explain') })
/**
* Start listening
@@ -13,13 +13,13 @@ var mongoose = require('mongoose')
* the JSON-object to something fairly human-friendly.
*/
-var User = new Schema({
+var Users = new Schema({
'name': {
'first': {type: String, required: true}
, 'last': String
}
, 'email': {
- 'home': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/}
+ 'home': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/, required: true}
, 'work': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/}
}
, 'phone': {type: String, match: /[\d]{8}/, override: 'tlf'}
@@ -31,5 +31,5 @@ var User = new Schema({
* Expose User to mongoose
*/
-mongoose.model('User', User)
+mongoose.model('Users', Users)
@@ -15,5 +15,5 @@ mongoose.connect('mongodb://localhost/crud')
* UserModel
*/
-require('./User')
+require('./Users')
@@ -1,42 +1,46 @@
form
- .block
- padding: 5px 0
+ .inline-block
+ margin-right: 6px
+ padding: 8px 0
legend
- display: block
- font-size: 1.5em
- padding: 6px 0
- text-indent: 10px
- width: 100%
+ font-size: 3em
fieldset
border: none
margin-top: 18px
label
- display: inline-block
- text-align: right
- padding: 5px 10px 0 10px
- vertical-align: top
- width: 70px
+ display: block
+ margin-bottom: 3px
&:first-letter
text-transform: uppercase
+ &.required:after
+ content: ' *'
+ display: inline
input[type=text]
+ background: rgba(black, 0.5)
border: none
border-radius: 4px
- box-shadow: inset 0 1px 3px rgba(black, 0.2), 0 1px 0 rgba(black, 0.1), 0 2px 0 rgba(white, 0.5)
- font-size: 1em
+ box-shadow: inset 0 1px 3px rgba(black, 0.5), 0 1px 0 rgba(white, 0.2)
+ color: white
+ font-size: 16px
+ min-width: 240px
outline: none
- padding: 0.3em
+ padding: 0.5em
transition: box-shadow .2s ease-in-out
&:focus
- box-shadow: inset 0 1px 4px rgba(black, 0.3), 0 0 5px darken(ident, 20)
+ box-shadow: inset 0 1px 3px rgba(black, 0.5), 0 1px 0 rgba(white, 0.2), 0 0 6px rgba(white, 0.8)
+ &:required:invalid
+ background: lightCoral
input[type=email], input[type=tlf], input[type=url]
@extends input[type=text]
textarea
@extends input[type=text]
+ min-width: 340px
+ max-width: 420px

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -6,8 +6,6 @@
global-reset()
reset-html5()
-ident = rgb(220, 240, 250)
-
/**
* Form
*/
@@ -19,14 +17,26 @@ body
background: #81878a
color: #222
font: 14px 'Open sans', Helvetica, Arial
+ line-height: 1.5em
padding: 50px
text-align: center
+ text-shadow: 0 1px 0 rgba(white, 0.2)
#wrapper
margin: 0 auto
max-width: 540px
text-align: left
+h1
+ font-size: 3em
+ line-height: 2em
+
+p
+ background: white
+ box-shadow: 0 1px 3px rgba(black, 0.3)
+ font-size: 1.3em
+ padding: 30px
+
for pos in block inline inline-block
.{pos}
display: pos
@@ -1,17 +0,0 @@
-
-/**
- * GET crud form
- */
-
-exports.form = function(req, res) {
- res.render('form')
-}
-
-/**
- * GET explain usage
- */
-
-exports.explain = function (req, res) {
- res.render('explain')
-}
-
@@ -1,3 +1,4 @@
-p You should try to open '/User'
+h1 Welcome
+p You should open '/User'
Oops, something went wrong.

0 comments on commit c4b2232

Please sign in to comment.