提交 b12171b9f5ce044e97e47eb1c620c8b68ef4a71c
1 个父辈
961c1943
added model common plugin, added components to readme
正在显示
2 个修改的文件
包含
69 行增加
和
0 行删除
| @@ -25,6 +25,19 @@ eskimo --help | @@ -25,6 +25,19 @@ eskimo --help | ||
| 25 | ``` | 25 | ``` |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | +## Components | ||
| 29 | + | ||
| 30 | +Using `electrolyte`, you an inject the following dependencies into your app with `eskimo`: | ||
| 31 | + | ||
| 32 | +* `app` - returns an instance of `express()` | ||
| 33 | +* `error-handler` - returns an error handler that can be used via `app.use` | ||
| 34 | +* `db` - returns connection to MongoDB | ||
| 35 | +* `logger` - returns a Winston logger instance | ||
| 36 | +* `model-common-plugin` - returns a Mongoose plugin for common schema paths | ||
| 37 | +* `sessions` - returns connection to Redis | ||
| 38 | +* `settings` - returns config | ||
| 39 | + | ||
| 40 | + | ||
| 28 | ## Contributors | 41 | ## Contributors |
| 29 | 42 | ||
| 30 | * Nick Baugh <niftylettuce@gmail.com> | 43 | * Nick Baugh <niftylettuce@gmail.com> |
boot/model-common-plugin.js
0 → 100644
| 1 | + | ||
| 2 | +// # boot - model common plugin | ||
| 3 | + | ||
| 4 | +var _ = require('underscore') | ||
| 5 | +var jsonSelect = require('mongoose-json-select') | ||
| 6 | + | ||
| 7 | +exports = module.exports = function(settings) { | ||
| 8 | + | ||
| 9 | + return function(Schema) { | ||
| 10 | + | ||
| 11 | + // NOTE: To allow `.sort('-created_at')` to work | ||
| 12 | + // we need to have these as actual paths | ||
| 13 | + Schema.add({ | ||
| 14 | + updated_at: Date, | ||
| 15 | + created_at: Date | ||
| 16 | + }) | ||
| 17 | + | ||
| 18 | + Schema.pre('save', function(next) { | ||
| 19 | + | ||
| 20 | + var that = this | ||
| 21 | + | ||
| 22 | + that.updated_at = (!that.created_at) ? Date.now() : that._id.getTimestamp() | ||
| 23 | + | ||
| 24 | + if (!that.created_at) | ||
| 25 | + that.created_at = that._id.getTimestamp() | ||
| 26 | + | ||
| 27 | + // version history (alternative to `mongoose-version` and `mongoose-history`) | ||
| 28 | + if (settings.mongooseVersioning) { | ||
| 29 | + var version = that.toObject() | ||
| 30 | + version.__v = _.isNumber(version.__v) ? version.__v + 1 : 0 | ||
| 31 | + delete version.__versions | ||
| 32 | + that.__versions.push(version) | ||
| 33 | + } | ||
| 34 | + next() | ||
| 35 | + }) | ||
| 36 | + | ||
| 37 | + Schema.set('toObject', { | ||
| 38 | + virtuals: true, | ||
| 39 | + getters: true | ||
| 40 | + }) | ||
| 41 | + | ||
| 42 | + Schema.set('toJSON', { | ||
| 43 | + virtuals: true, | ||
| 44 | + getters: true | ||
| 45 | + }) | ||
| 46 | + | ||
| 47 | + Schema.plugin(jsonSelect, '-_id -__v') | ||
| 48 | + | ||
| 49 | + return Schema | ||
| 50 | + | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +exports['@singleton'] = true | ||
| 56 | +exports['@require'] = [ 'settings' ] |
请
注册
或
登录
后发表评论