Permalink
Browse files

Initial commit

Moving to another computer, hope to have a functioning version this
evening.
  • Loading branch information...
1 parent 535d6a4 commit 5e5c020778504798a247b428ca330ce412f16046 @hkjels committed May 14, 2012
Showing with 132 additions and 4 deletions.
  1. 0 History.md
  2. +0 −4 README.md
  3. +21 −0 Readme.md
  4. 0 Taskpool.md
  5. +86 −0 crud.js
  6. +25 −0 package.json
View
No changes.
View
@@ -1,4 +0,0 @@
-crud
-====
-
-Crud functionality for mongoose-models. Fits well with express.js
View
@@ -0,0 +1,21 @@
+# crud
+
+__Crud functionality for mongoose-models. Fits well with express.js__
+
+
+## Install
+
+ λ npm install crud
+
+
+## With express.js
+
+ var express = require('express')
+ , app = module.exports = express.createServer()
+ , mongoose = require('mongoose')
+ , Model = require('./Model')
+ , crud = require('crud')
+
+ app.get('/crud/:model', crud, function (req, res) {
+ res.render('crud')
+ })
View
No changes.
View
@@ -0,0 +1,86 @@
+
+/**
+ * Module dependencies
+ */
+
+var mongoose = require('mongoose')
+ , cycle = require('cycle')
+
+/**
+ * Simplify
+ *
+ * Normalizes a mongoose Schema to be used with a template
+ * @param {String} model Name of the mongoose model
+ * @param {Function} cb Callback
+ */
+
+var simplify = exports.simplify = function (model, cb) {
+ var err
+ , Model
+ , Schema
+
+ /**
+ * Retrieve model from mongoose
+ */
+
+ try {
+ Model = mongoose.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) {
+ var fieldtype = field.match(/(number|date|email|password|url|tlf)/gi)
+
+ // Required
+
+ Schema[field]['required'] = typeof Schema[field]['isRequired'] !== 'undefined'
+ ? Schema[field]['isRequired']
+ : null
+
+ // Field type
+ // TODO #crud Add number, date etc
+
+ fieldtype = fieldtype || 'text'
+ Schema[field]['type'] = typeof Schema[field]['options']['override'] !== 'undefined'
+ ? Schema[field]['options']['override']
+ : fieldtype
+
+ // Field name
+
+ Schema[field]['name'] = field.substring(field.lastIndexOf('.') + 1)
+
+ for (var prop in Schema[field]) {
+ if (prop === 'selected' && !field[prop]) delete Schema[field]
+ }
+ }
+}
+
+/**
+ * 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, model) {
+ simplify(model, function (err, form) {
+ if (err) return next(err)
+ req.locals('form', form)
+ return next()
+ })
+}
+
View
@@ -0,0 +1,25 @@
+{
+ "name": "crud"
+ , "version": "0.0.1"
+ , "author": "Henrik Kjelsberg <henrik@kjelsberg.net>"
+ , "description": "Crud functionality for mongoose-models. Fits well with express.js"
+ , "keywords": ["mongoose", "express", "crud", "scaffold"]
+ , "main": "crud.js"
+ , "scripts": {
+ "test": "make mocha"
+ , "prepublish": "make mocha lint docs"
+ }
+ , "dependencies": {
+ "cycle": "1.0.0"
+ , "mongoose": ">= 2.0.0"
+ }
+ , "devDependencies": {
+ "express": "2.5.9"
+ , "jade": "0.26.0"
+ , "jshint": ">= 0.7.0"
+ , "mocha": "1.0.3"
+ , "nib": "0.4.1"
+ , "should": "0.6.3"
+ , "stylus": "0.27.0"
+ }
+}

0 comments on commit 5e5c020

Please sign in to comment.