mongoose-manager
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -11,2 +11,3 @@ var Admin = require('../lib/admin').Admin, | ||
model: require('./models/Artist').Artist, | ||
// the name of the property to be used as label when an Artist is displayed embedded in another document | ||
label: 'name' | ||
@@ -18,3 +19,5 @@ }, | ||
var admin = new Admin(models); | ||
var admin = new Admin(models, { | ||
pageSize: 20 | ||
}); | ||
@@ -21,0 +24,0 @@ // run the admin app |
@@ -6,9 +6,36 @@ [ | ||
"published": "1995-07-16", | ||
"artist": "000000000000000000000002" | ||
}, | ||
{ | ||
"_id": "100000000000000000000002", | ||
"name": "Record without artist", | ||
"artist": "000000000000000000000002" | ||
"artist": "000000000000000000000002", | ||
"songs": [ | ||
"Billie Jean", | ||
"The Way You Make Me Feel", | ||
"Black or White", | ||
"Rock with You", | ||
"She's Out of My Life", | ||
"Bad", | ||
"I Just Can't Stop Loving You", | ||
"Man in the Mirror", | ||
"Thriller", | ||
"Beat It", | ||
"The Girl Is Mine", | ||
"Remember the Time", | ||
"Don't Stop 'Til You Get Enough", | ||
"Wanna Be Startin' Somethin'", | ||
"Heal the World", | ||
"Scream", | ||
"They Don't Care About Us", | ||
"Stranger in Moscow", | ||
"This Time Around", | ||
"Earth Song", | ||
"D.S.", | ||
"Money", | ||
"Come Together", | ||
"You Are Not Alone", | ||
"Childhood", | ||
"Tabloid Junkie", | ||
"2 Bad", | ||
"HIStory", | ||
"Little Susie", | ||
"Smile" | ||
] | ||
} | ||
] |
@@ -6,3 +6,4 @@ var mongoose = require('mongoose'); | ||
published: {type: Date}, | ||
artist : { type: mongoose.Schema.Types.ObjectId, ref: 'Artist', required: true } | ||
artist : { type: mongoose.Schema.Types.ObjectId, ref: 'Artist', required: true }, | ||
songs: [String] | ||
}); | ||
@@ -9,0 +10,0 @@ |
@@ -15,3 +15,3 @@ var express = require('express'), | ||
this.models = _asObject(models); | ||
this.app = _createApp(); | ||
this.app = this._createApp(); | ||
this.options = _.extend(require('./defaults').defaults, options); | ||
@@ -58,5 +58,4 @@ routes.init(this); | ||
function _createApp() { | ||
var self = this, | ||
app = express(); | ||
Admin.prototype._createApp = function() { | ||
var app = express(); | ||
@@ -69,2 +68,3 @@ // all environments | ||
app.use(express.favicon()); | ||
app.use(express.static(__dirname + '/../public', {'maxAge': 0})); | ||
app.use(express.logger('dev')); | ||
@@ -75,3 +75,3 @@ app.use(express.bodyParser()); | ||
app.use(express.session()); | ||
app.use(require('../views/helpers').init()); | ||
app.use(require('../views/helpers').init(this)); | ||
app.use(app.router); | ||
@@ -78,0 +78,0 @@ |
@@ -1,2 +0,3 @@ | ||
var async = require('async'); | ||
var async = require('async'), | ||
utils = require('../utils'); | ||
@@ -13,3 +14,4 @@ exports.init = function(admin){ | ||
skip = (page - 1) * pageSize, | ||
q = req.query.q; | ||
q = req.query.q, | ||
fields = admin.getListFields(req.params.model); | ||
@@ -21,3 +23,14 @@ async.parallel([ | ||
function(cb){ | ||
model.find().limit(pageSize).skip(skip).exec(cb); | ||
model.find().limit(pageSize).skip(skip).exec(function(err, documents){ | ||
if (err) return cb(err); | ||
var fieldsToPopulate = fields.filter(function(f){ | ||
var field = utils.getField(model, f); | ||
return field.options && 'ref' in field.options; | ||
}); | ||
model.populate(documents, fieldsToPopulate, function(){ | ||
cb(err, documents); | ||
}); | ||
}); | ||
} | ||
@@ -30,3 +43,3 @@ ], function(err, results){ | ||
documents : results[1], | ||
fields : admin.getListFields(req.params.model), | ||
fields : fields, | ||
count : results[0], | ||
@@ -33,0 +46,0 @@ page : page, |
@@ -5,1 +5,5 @@ exports.url = function(req){ | ||
}; | ||
exports.getField = function(document, fieldName){ | ||
return document.schema.paths[fieldName] || document.schema.virtuals[fieldName]; | ||
}; |
{ | ||
"name": "mongoose-manager", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"scripts": { | ||
"example": "supervisor --force-watch -i public example/app.js" | ||
"example": "supervisor --force-watch -i public,views example/app.js" | ||
}, | ||
@@ -7,0 +7,0 @@ "dependencies": { |
@@ -1,5 +0,27 @@ | ||
A generic admin interface for applications that use mongoose. | ||
# mongoose-manager | ||
A generic admin interface for node.js applications that use mongoose. | ||
Warning: Development still in progress. | ||
Note: Thanks to @madhums for handing over the module name | ||
## Usage | ||
```js | ||
var Admin = require('mongoose-manager').Admin; | ||
var models = [ | ||
{ | ||
model: require('./models/Artist').Artist, | ||
label: function(artist){ return artist.name } | ||
}, | ||
require('./models/Label').Label, | ||
require('./models/Record').Record | ||
]; | ||
var admin = new Admin(models); | ||
// an express app is now available in admin.app | ||
admin.app.listen(8080, function(){ | ||
console.log('Example app running on %s', 8080); | ||
}); | ||
``` | ||
### Note | ||
Thanks to @madhums for handing over the module name |
var moment = require('moment'), | ||
_ = require('underscore'), | ||
ejs = require('ejs'), | ||
fs = require('fs'); | ||
fs = require('fs'), | ||
utils = require('../lib/utils'); | ||
@@ -11,3 +12,3 @@ var helpers = { | ||
module.exports.init = function(){ | ||
module.exports.init = function(admin){ | ||
@@ -23,8 +24,9 @@ return function(req, res, next){ | ||
res.locals.url = require('../lib/utils').url.bind(res.locals, req); | ||
res.locals.getField = require('../lib/utils').getField; | ||
res.locals.req = req; | ||
res.locals.tableFormat = function(fieldName, document){ | ||
res.locals.tableFormat = function(fieldName, document, maxLength){ | ||
var value = document[fieldName], | ||
field = document.schema.paths[fieldName] || document.schema.virtuals[fieldName]; | ||
field = utils.getField(document, fieldName); | ||
@@ -43,5 +45,27 @@ if (!field) { | ||
if (field.options && field.options.ref){ | ||
return res.locals.label(value, field.options.ref); | ||
} | ||
value = value.toString(); | ||
if (maxLength && value.length > maxLength){ | ||
value = value.substring(0, maxLength) + '…'; | ||
} | ||
return value; | ||
}; | ||
res.locals.label = function(document, modelName){ | ||
var labelField; | ||
if (labelField = admin.models[modelName].label){ | ||
return _.isFunction(labelField) ? labelField(document) : document[labelField]; | ||
} | ||
if ((labelField = utils.getField(document, 'label')) && labelField.options.type == String){ | ||
return document.label; | ||
} | ||
if ((labelField = utils.getField(document, 'name')) && labelField.options.type == String){ | ||
return document.name; | ||
} | ||
return document; | ||
}; | ||
res.locals = _.extend(res.locals, helpers); | ||
@@ -48,0 +72,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29190
34
483
27