Permalink
Browse files

Decided to go with express-resource

Made the example abit more ellaborate
  • Loading branch information...
1 parent c4b2232 commit 5b289da5f26e936cf05f029a78edc9ffeba2c1a1 @hkjels committed May 30, 2012
View
@@ -1,47 +1,36 @@
# crud
-__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
+__Crud-functionality for mongoose and express__
## Install
λ npm install crud
-## With express.js magic
+## Usage
var express = require('express')
- , app = module.exports = express.createServer()
+ , app = express.createServer()
, models = require('./models')
- , crud = require('crud')
-
- app.get('/crud/:model', crud.middleware, function (req, res) {
- res.render('crud')
- })
-
-
-## Without express.js magic
- var mongoose = require('mongoose')
- , Model = mongoose.model('Model')
- , crud = require('crud').form
+ require('crud')
- crud(Model, function (err, form) {
- if (err) return console.err(err)
- console.dir(form)
- })
+ app.crud('modelname')
+## REST-api
-## Note
+The action-mapping is inherited from
+[express-resource](https://github.com/visionmedia/express-resource)
+and is as follows
-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.
+ GET /forums -> index
+ GET /forums/new -> new
+ POST /forums -> create
+ GET /forums/:forum -> show
+ GET /forums/:forum/edit -> edit
+ PUT /forums/:forum -> update
+ DELETE /forums/:forum -> destroy
## Constributors
View
@@ -9,198 +9,5 @@
* Module dependencies
*/
-var cycle = require('cycle')
- , lingo = require('lingo')
- , en = lingo.en
- , mongoose = require('mongoose')
-
-/**
- * Form
- *
- * Normalizes a mongoose Schema to be used with a form-template
- * @param {Mixed} model Model or name of a model
- * @param {Function} cb Callback
- */
-
-var form = exports.form = function (model, cb) {
- var err = false
- , form = {}
- , Model
- , Schema
- , copy = {
- 'isRequired': 'required'
- }
-
- /**
- * Retrieve model from mongoose
- */
-
- try {
- Model = typeof model === 'string' ? mongoose.model(model) : model
- Schema = cycle.decycle(Model.prototype._schema.paths)
- }
-
- /**
- * Incorrect modelname or faulty model
- */
-
- catch (err) {
- return cb(err)
- }
-
- /**
- * Process Schema
- *
- * Normalization of the Schema-model to a more humanfriendly JSON format
- */
-
- for (var field in Schema) {
-
- /**
- * Ignored fields
- */
-
- for (var prop in Schema[field]) {
- if (prop === 'selected' && !field[prop]) continue
- }
-
- form[field] = {}
-
- /**
- * Copy a bunch of parameters
- *
- * The value from the copy-object is used as the key to our new form-object
- * TODO Need a smarter way of pushing values from Schema to form
- */
-
- for (var key in copy) {
- if (typeof Schema[field][key] !== 'undefined') {
- form[field][copy[key]] = Schema[field][key]
- }
- }
-
- /**
- * Field name
- */
-
- form[field]['name'] = field.substring(field.lastIndexOf('.') + 1)
-
- /**
- * Field type
- */
-
- var fieldtype = field.match(/(number|date|email|password|url|tlf)/gi) || 'text'
- form[field]['type'] = Schema[field].instance === 'boolean' ? 'checkbox' : fieldtype
- form[field]['type'] = typeof Schema[field]['options']['override'] !== 'undefined'
- ? Schema[field]['options']['override']
- : fieldtype
- form[field]['type'] = form[field]['type'].toString()
- form[field]['type'] = field[0] === '_' ? 'hidden' : form[field]['type']
- if (typeof Schema[field]['options']['min'] !== 'undefined'
- && Schema[field]['options']['min'] >= 50) {
- form[field]['type'] = 'textarea'
- }
- if (typeof Schema[field]['options']['max'] !== 'undefined') {
- form[field]['max'] = Schema[field]['options']['max']
- }
- }
-
- return cb(err, form)
-}
-
-/**
- * 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 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
- , instance: instance
- , model: model
- })
- return next()
- })
-}
+module.exports = require('./lib/crud')
@@ -39,7 +39,7 @@
line-height: 0;
letter-spacing: -0.05em;
}
- p, pre {
+ strong, pre {
display: block;
margin-bottom: 50px;
}
@@ -52,6 +52,7 @@
background: white !important;
}
blockquote {
+ font-size: 0.8em;
margin: 0;
padding: 30px 50px;
}
@@ -5,11 +5,10 @@
var express = require('express')
, app = module.exports = express.createServer()
- , Resource = require('express-resource')
, models = require('./models')
, stylus = require('stylus')
, nib = require('nib')
- , crud = require('../../crud')
+ , crud = require('../../lib/crud')(app)
require('express-namespace')
@@ -52,12 +51,12 @@ app.helpers({
/**
* API
*
- * CRUD supports express-namespace & express-resource
- * This creates a one to one connection between your routes and models
+ * CRUD supports express-namespace
+ * This means you can add authentication as middleware for all sub-routes
*/
app.namespace('/api', function () {
- app.resource('users', crud.resource)
+ app.crud('users')
})
/**
@@ -20,7 +20,7 @@ var Users = new Schema({
}
, 'email': {
'home': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/, required: true}
- , 'work': {type: String, match: /[\w]+@[\w]+\.[\w]{2,3}/}
+ , 'work': {type: String}
}
, 'phone': {type: String, match: /[\d]{8}/, override: 'tlf'}
, 'born': Date
@@ -6,6 +6,7 @@ form
legend
font-size: 3em
+ margin-bottom: 1em
fieldset
border: none
@@ -44,3 +45,17 @@ textarea
min-width: 340px
max-width: 420px
+button
+ background: #ccc linear-gradient(top, #eee, #ccc)
+ border: none
+ border-radius: 4px
+ box-shadow: 0 1px 3px rgba(black, 0.4)
+ font-size: 16px
+ padding: 0.5em
+ text-transform: capitalize
+ &:active
+ background: #ccc linear-gradient(top, #666, #eee)
+ box-shadow: inset 0 1px 3px rgba(black, 0.4)
+
+input[type=button], input[type=reset], input[type=submit]
+ @extends button
Oops, something went wrong.
@@ -32,6 +32,9 @@ h1
line-height: 2em
p
+ margin-bottom: 1.5em
+
+.boxed
background: white
box-shadow: 0 1px 3px rgba(black, 0.3)
font-size: 1.3em
@@ -40,3 +43,13 @@ p
for pos in block inline inline-block
.{pos}
display: pos
+
+table
+ @extends .boxed
+ width: 100%
+ th
+ font-size: 1.1em
+ padding-bottom: 1em
+ tfoot
+ td
+ padding-top: 1em
Oops, something went wrong.

0 comments on commit 5b289da

Please sign in to comment.