Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #14 from sorensen/gh-pages

gh-pages
  • Loading branch information...
commit 11436cefc113a9795f983db188a2a7356978e86d 2 parents b08d67a + 199cfb5
@tblobaum authored
View
43 .gitignore
@@ -0,0 +1,43 @@
+# Numerous always-ignore extensions
+*.diff
+*.err
+*.orig
+*.log
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+
+# OS or Editor folders
+.DS_Store
+.cache
+.project
+.settings
+nbproject
+thumbs.db
+
+# Logs
+.log
+.pid
+.sock
+
+# Dreamweaver added files
+_notes
+dwsync.xml
+
+# Komodo
+*.komodoproject
+.komodotools
+
+# Folders to ignore
+node_modules
+.hg
+.svn
+publish
+.idea
+_dev
+
+# build script local files
+build/buildinfo.properties
+build/config/buildinfo.properties
View
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2011-2012 Tom Blobaum <tblobaum@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
673 README.md
@@ -0,0 +1,673 @@
+
+# Mongoose Troop [![Build Status](https://secure.travis-ci.org/tblobaum/mongoose-troop.png)](http://travis-ci.org/tblobaum/mongoose-troop)
+
+A collection of handy plugins for mongoose
+
+## Contents
+
+* [acl](#Troop.acl) (simple access control list)
+* [basicAuth](#Troop.basicAuth) (simple authentication and registration)
+* [timestamp](#Troop.timestamp) (automatic created and modified timestamps)
+* [slugify](#Troop.slugify) (url-friendly copies of string properties)
+* [keywords](#Troop.keywords) (search-friendly array of stemmed words from string properties)
+* [pubsub](#Troop.pubsub) (message passing)
+* [pagination](#Troop.pagination) (query pagination)
+* [rest](#Troop.rest) (http or rpc controller)
+* [obfuscate](#Troop.obfuscate) (objectID encryption / decryption)
+* [merge](#Troop.merge) (merge a document into another)
+* [removeDefaults](#Troop.removeDefaults) (remove default values from a document)
+* [getdbrefs](#Troop.getdbrefs) (find all document DBRefs)
+
+The annotated source can be found [here](http://tblobaum.github.com/mongoose-troop/docs/)
+
+***
+
+## <span id="#Troop.acl">acl</a>
+Simple access control list
+
+### Methods
+
+#### instance.addAccess(key)
+
+Add `key` access to a Model instance
+
+#### instance.removeAccess(key)
+
+Remove `key` access to a Model instance
+
+#### instance.access(key [, callback])
+
+Return or callback a boolean
+
+
+***
+
+## <span id="#Troop.basicAuth">basicAuth</a>
+
+Simple authentication plugin
+
+### Options
+
+* `loginPath` schema path for username/login (optional, default `username`)
+* `hashPath` schema path to hashed password (optional, default `hash`)
+* `workFactor` bcrypt work factor (optional, default `10`)
+
+### Methods
+
+#### instance.authenticate(password, callback)
+
+Authenticate a mongoose document
+
+#### instance.setPassword(password, callback)
+
+Set the password for a mongoose document
+
+#### model.authenticate(username, password, callback)
+
+Authenticate a user on the model level
+
+#### model.register(attributes, callback)
+
+Create a new user with given attributes
+
+### Example
+
+```javascript
+var mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , db = mongoose.connect()
+ , UserSchema = new mongoose.Schema()
+
+UserSchema.plugin(troop.basicAuth)
+
+var User = mongoose.model('user', UserSchema)
+
+User.register({
+ username: 'foo'
+, password: 'bar'
+}, function() {
+ // ...
+})
+
+User.authenticate('foo', 'bar', function(err, doc) {
+ // ...
+})
+
+User.findOne({ username: 'foo'}, function(err, doc) {
+ if (err || !doc) return
+ doc.setPassword('foobar', function(err) {
+ if (err) return
+ doc.authenticate('foobar', function() {
+ // ...
+ })
+ })
+})
+````
+
+
+***
+
+## <span id="#Troop.timestamp">timestamp</a>
+
+Adds a `created` and `modified` property to the schema, updating the timestamps as expected.
+
+### Options
+
+* `createdPath` schema path for created timestamp (optional, default `created`)
+* `modifiedPath` schema path for modified timestamp (optional, default `modified`)
+* `useVirtual` use a virtual path for created timestamp based on ObjectId (optional, default `true`)
+
+### Example
+
+```javascript
+var mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , FooSchema = new mongoose.Schema()
+
+FooSchema.plugin(troop.timestamp)
+````
+
+### Note
+
+Using the virtual `created` timestamp you will lose the ability to run queries against it,
+as well as a loss in precision, as it will return a timestamp in seconds.
+
+
+***
+
+## <span id="#Troop.slugify">slugify</a>
+
+Turn a string based field into a url friendly slug
+
+Converts `this is a title` to `this-is-a-title`
+
+### Options
+
+* `target` schema path for slug destination (optional, default `slug`)
+* `source` schema path for slug content (optional, default `title`)
+* `maxLength` maximum slug length (optional, default `50`)
+* `spaceChar` space replacement character (optional, default `-`)
+* `invalidChar` invalid character replacement (optional, default ``)
+* `override` override slug field on source path change (optional, default `false`)
+
+### Methods
+
+#### instance.slugify(string)
+
+#### model.slugify(string)
+
+### Example
+
+```javascript
+var mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , FooSchema = new mongoose.Schema()
+
+FooSchema.plugin(troop.slugify)
+
+var instance = new FooSchema({title: 'well hello there!'})
+
+instance.save(function(err, doc) {
+ console.log(doc.slug) // `well-hello-there`
+})
+````
+
+### Note
+
+This plugin does not currently support nested paths
+
+
+***
+
+## <span id="#Troop.keywords">keywords</a>
+
+Keyword extraction/creation plugin, can be used as a simple substitute of a full
+search indexing package.
+
+Turns `fooed bars` into `['foo', 'bar']`
+
+### Options
+
+* `target` schema path for keyword destination (optional, default `keywords`)
+* `source` schema path for extracting keywords, can be an array to specify multiple paths
+* `minLength` minimum string length to be used as a keyword (optional, default `2`)
+* `invalidChar` replacement char for invalid chars (optional, default ``)
+* `naturalize` specifies whether to use a porter stemmer for keywords (optional, default `false`)
+
+### Methods
+
+#### instance.extractKeywords(str)
+
+#### model.extractKeywords(str)
+
+Manually calculate a keyword array with a given string
+
+### Example
+
+```javascript
+var mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , db = mongoose.connect()
+
+var FooSchema = new mongoose.Schema({
+ text: String
+})
+
+FooSchema.plugin(troop.keywords, {
+ source: 'text'
+})
+
+var fooModel = mongoose.model('foo', FooSchema)
+ , instance = new FooSchema({ text: 'i am the batman' })
+
+console.log(instance.keywords) // `['am', 'the', 'batman']`
+
+fooModel.find({
+ keywords: { $in: fooModel.extractKeywords('batman') }
+}, function(docs) {
+ // ...
+})
+````
+
+### Note
+
+This plugin does not currently support nested paths
+
+
+***
+
+## <span id="#Troop.publish">publish</a>
+
+Plugin to publish/subscribe from a model or instance level, also enabling a model
+to automatically publish changes on `init`, `save`, and `remove` methods. Both models
+and instances can be published/subscribed to.
+
+### Options
+
+* `auto` attach middleware based on the `hook` for `init`, `save`, and `remove` methods (optional, default `false`)
+* `hook` middleware method to attach auto middleware to (optional, default `post`)
+* `seperator` redis channel seperator (optional, default `:`)
+* `prefix` redis channel prefix, can be a string or function (optional, default ``)
+* `channel` channel for schema to publish/subscribe to, can be a string or function (optional, default `schema.constructor.modelName`)
+* `publish` redis instance to be used for publishing
+* `subscribe` redis instance to be used for subscribing
+
+### Methods
+
+#### instance.publish(doc, options, callback)
+
+#### instance.subscribe(callback)
+
+#### instance.unsubscribe(callback)
+
+#### instance.getChannel()
+
+#### instance.on(event, callback)
+
+#### model.subscribe(callback)
+
+#### model.unsubscribe(callback)
+
+#### model.getChannel()
+
+#### model.on(event, callback)
+
+### Example
+
+```javascript
+var redis = require('redis')
+ , publish = redis.createClient()
+ , subscribe = redis.createClient()
+ , mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , db = mongoose.connect()
+
+var FooSchema = new mongoose.Schema({
+ name: String
+})
+
+FooSchema.plugin(troop.publish, {
+ publish: redis
+, subscribe: subscribe
+})
+
+var FooModel = mongoose.model('foo', FooSchema)
+
+FooModel.subscribe() // channel: 'foos'
+
+FooModel.findOne({name: 'bar'}, function(err, instance) {
+ // ...
+})
+````
+
+Once you have a mongoose instance you can now publish it, by default, a model or
+instance will publish to it's own channel
+
+```javascript
+instance.publish(null, {
+ method: 'save'
+}, function(err, count) {
+ // publishes to 'foos:4d6e5acebcd1b3fac9000007'
+})
+````
+
+You can also publish other documents to other models or instances
+
+```javascript
+FooModel.publish(instance, function(err, count) {
+ // publishes to 'foos'
+})
+````
+
+or, if you have enabled `hooks`
+
+```javascript
+instance.save()
+````
+
+You can also subscribe on the instance level
+
+```javascript
+instance.subscribe() // channel: 'foos:4d6e5acebcd1b3fac9000007'
+````
+
+
+***
+
+## <span id="#Troop.pagination">pagination</a>
+
+Simple query pagination routines.
+
+### Options
+
+* `defaultQuery` Query to use if not specified (optional, default `{}`)
+* `defaultLimit` Results per page to use if not specified (optional, default `10`)
+* `defaultFields` Fields to use if not specified (optional, default `[]`)
+* `remember` Remember the last options used for `query`, `limit`, and `fields` (optional, default `false`)
+
+### Methods
+
+#### model.paginate(options, callback)
+
+#### model.firstPage(options, callback)
+
+#### model.lastPage(options, callback)
+
+### Example
+
+Assume that we have a collection with 55 records in it for the following example,
+where the `count` field is incremented by 1 for each record, starting at 1.
+
+```javascript
+var mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , db = mongoose.connect()
+
+var FooSchema = new mongoose.Schema({
+ name: String
+, count: Number
+})
+
+FooSchema.plugin(troop.pagination)
+
+var FooModel = mongoose.model('foo', FooSchema)
+
+FooModel.paginate({ page: 1 }, function (err, docs, count, pages, current) {
+
+ // docs.length = 10
+ // count = 55
+ // pages = 6
+ // current = 1
+
+})
+````
+
+Which, since using the default options, can also be written as:
+
+```javascript
+FooModel.firstPage(function (err, docs, count, pages, current) {
+ // ...
+})
+````
+
+Or, if you wanted the last page:
+
+```javascript
+FooModel.lastPage(function (err, docs, count, pages, current) {
+ // docs.length = 5
+ // current = 6
+})
+````
+
+A more verbose pagination call
+
+```javascript
+FooModel.paginate({
+ page: 2
+, query: { count: { $gt: 25 } }
+, limit: 25
+, fields: ['name']
+}, function(err, docs, count, pages, current) {
+
+ // docs.length = 5
+ // count = 30
+ // pages = 2
+ // current = 2
+
+})
+````
+
+### Note
+
+If using the `remember` option, the plugin will cache all of the options you give it
+each time you pass them in (except for the page), this can be handy if the params are
+going to be the same each time, if they are different you should not use this option.
+
+Also, when on the last page, the plugin will return the trailing number of documents,
+in the example above the `lastPage` method returned 5 documents, it will never return
+a full set specified by the `limit` when this is the case.
+
+
+***
+
+## <span id="#Troop.rest">rest</a>
+
+### Options
+
+* `pagination` options to send to the pagination plugin above (optional, see plugin defaults above)
+
+Create a REST-ful controller for your models for use with flatiron/director, express, dnode or socket.io
+
+
+***
+
+## <span id="#Troop.obfuscate">obfuscate</a>
+
+ObjectID encrypt/decryption. Recursively traverses a document, encrypting or decrypting
+any ObjectID that is found to prevent leaking any server information contained in the ID, will
+work with embedded documents as well as DBRefs.
+
+### Options
+
+* `encryptPath` Getter path for returning encrypted document (optional, default `obfuscate`)
+* `decryptPath` Setter path for decrypting an object and assigning it to the document (optional, default `deobfuscate`)
+* `algorithm` Encryption algorithm to use (optional, default `aes-256-cbc`)
+* `key` Encryption key to be used (optional, default `secret`)
+* `from` Encoding of the field to be encrypted (optional, default `utf8`)
+* `to` Encoding of the encrypted field (optional, default `hex`)
+
+### Methods
+
+####model.encrypt(string)
+
+####model.decrypt(string)
+
+####model.encode(object, boolean)
+
+####instance.encrypt(string)
+
+####instance.decrypt(string)
+
+### Example
+
+```javascript
+var mongoose = require('mongoose')
+ , troop = require('mongoose-troop')
+ , db = mongoose.connect()
+
+var BarSchema = new mongoose.Schema()
+ , UserSchema = new mongoose.Schema()
+ , SessionSchema = new mongoose.Schema()
+
+// A complicated schema
+var FooSchema = new mongoose.Schema({
+ dbref: { type: ObjectId, ref: BarSchema }
+, dbrefArray: [{ type: ObjectId, ref: BarSchema }]
+, nested: {
+ dbref: { type: ObjectId, ref: BarSchema }
+ , dbrefArray: [{ type: ObjectId, ref: BarSchema }]
+ , embedded: [FooSchema]
+}
+, embedded: [FooSchema]
+, user: {
+ id: { type: Schema.ObjectId, ref: 'user' }
+ , session: {
+ sid: { type: Schema.ObjectId, ref: 'session' }
+ }
+ }
+})
+
+FooSchema.plugin(troop.obfuscate)
+
+var FooModel = mongoose.model('foo', FooSchema)
+ , BarModel = mongoose.model('bar', BarSchema)
+ , UserModel = mongoose.model('user', UserSchema)
+ , SessionSchema = mongoose.model('session', SessionSchema)
+
+var bar = new BarModel()
+ , user = new UserModel()
+ , session = new SessionModel()
+
+var foo = new FooModel({
+ dbref: bar
+, dbrefArray: [foo2, foo3]
+, embeddedArray: [foo]
+, nested: {
+ dbref: foo
+ , dbrefArray: [foo2, foo3]
+ , nested: [foo]
+}
+, embedded: {
+ id: user._id
+ , session: { sid: session._id }
+ }
+})
+
+var obfuscated = foo.obfuscate
+````
+
+Now we should have an obfuscated object like so
+
+```json
+{
+ _id: '0edaf91b2b5fa8c06413cdbf9ebed72a90a2c5ae4fe9b837d24865bd92c56ab2'
+, dbref: '0edaf91b2b5fa8c06413cdbf9ebed72a4735e5707b8423055431a1fe65adad6b'
+, dbrefArray: [
+ '0edaf91b2b5fa8c06413cdbf9ebed72a59ea2f1567c4ba640c02b02bb73f36d7'
+ , '0edaf91b2b5fa8c06413cdbf9ebed72aec369726048f7aa6cae9e8d20d7b2344'
+ ]
+, embedded: {
+ id: '0edaf91b2b5fa8c06413cdbf9ebed72a340f055306b64aeececd8835755008fc'
+ , session: {
+ sid: '0edaf91b2b5fa8c06413cdbf9ebed72a9f324d66d3a0e0d1c2fdd12d65efa3ea'
+ }
+ }
+, embeddedArray: [{
+ _id: '0edaf91b2b5fa8c06413cdbf9ebed72a4735e5707b8423055431a1fe65adad6b'
+ }]
+, nested: {
+ dbref: '0edaf91b2b5fa8c06413cdbf9ebed72a4735e5707b8423055431a1fe65adad6b'
+ , dbrefArray: [
+ '0edaf91b2b5fa8c06413cdbf9ebed72a59ea2f1567c4ba640c02b02bb73f36d7'
+ , '0edaf91b2b5fa8c06413cdbf9ebed72aec369726048f7aa6cae9e8d20d7b2344'
+ ]
+ , embeddedArray: [{
+ _id: '0edaf91b2b5fa8c06413cdbf9ebed72a4735e5707b8423055431a1fe65adad6b'
+ }]
+ }
+}
+````
+
+To deobfuscate the object, we can assign it back to the original model, or to another.
+
+```javascript
+var emptyFoo = new FooModel()
+
+emptyFoo.deobfuscate = obfuscated
+````
+
+Which should give us back the original object
+
+```json
+{
+ _id: 4f1b234afe789543a3000008
+, dbref: 4f1b234afe789543a3000003
+, dbrefArray: [ 4f1b234afe789543a3000004, 4f1b234afe789543a3000005 ]
+, embedded: {
+ id: 4f1b234afe789543a3000007
+ , session: {
+ sid: 4f1b234afe789543a3000006
+ }
+ }
+, embeddedArray: [{
+ _id: 4f1b234afe789543a3000003
+ }]
+, nested: {
+ dbref: 4f1b234afe789543a3000003
+ , dbrefArray: [ 4f1b234afe789543a3000004, 4f1b234afe789543a3000005 ]
+ , embeddedArray: [{
+ _id: 4f1b234afe789543a3000003
+ }]
+ }
+}
+````
+
+### Note
+
+This plugin will not work with `Mixed` type schema paths, you will have to obfuscate
+those manually
+
+
+***
+
+## <span id="#Troop.merge">merge</a>
+
+Merge JSON into your object more easily.
+
+```javascript
+instance.merge({title:'A new title', description:'A new description'}).save()
+````
+
+
+***
+
+## <span id="#Troop.getdbrefs">getdbrefs</a>
+
+Get the dbrefs from a schema
+
+```javascript
+instance.getdbrefs(function (refs) {
+ // ...
+})
+```
+
+### Note
+
+This plugin does not currently support nested paths
+
+
+***
+
+## <span id="#Troop.removeDefaults">removeDefaults</a>
+
+Remove all of the default values from your model instance.
+
+`instance.removeDefaults().save()`
+
+
+### Note
+
+This plugin does not currently support nested paths
+
+
+***
+
+## Contributing
+
+This project is a work in progress and subject to API changes, please feel free to contribute
+
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011-2012 Tom Blobaum <tblobaum@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
39 docs/acl.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html> <html> <head> <title>acl.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> acl.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Tom Blobaum
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>acl</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Plugin</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">acl</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">aclPath</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">aclPath</span> <span class="o">||</span> <span class="s1">&#39;acl&#39;</span>
+ <span class="p">,</span> <span class="nx">fields</span> <span class="o">=</span> <span class="p">{}</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">schema</span><span class="p">.</span><span class="nx">paths</span><span class="p">[</span><span class="nx">aclPath</span><span class="p">])</span> <span class="p">{</span>
+ <span class="nx">fields</span><span class="p">[</span><span class="nx">aclPath</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
+ <span class="nx">type</span><span class="o">:</span> <span class="p">[</span><span class="nb">String</span><span class="p">]</span>
+ <span class="p">,</span> <span class="k">default</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;public&#39;</span><span class="p">]</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">fields</span><span class="p">)</span>
+
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;addAccess&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">access</span><span class="p">(</span><span class="nx">key</span><span class="p">))</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">acl</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">markModified</span><span class="p">(</span><span class="s1">&#39;acl&#39;</span><span class="p">)</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;removeAccess&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">access</span><span class="p">(</span><span class="nx">key</span><span class="p">))</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">acl</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">acl</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">key</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">markModified</span><span class="p">(</span><span class="s1">&#39;acl&#39;</span><span class="p">)</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;access&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">bool</span> <span class="o">=</span> <span class="o">!!~</span><span class="k">this</span><span class="p">.</span><span class="nx">acl</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span>
+ <span class="nx">next</span> <span class="o">&amp;&amp;</span> <span class="nx">next</span><span class="p">(</span><span class="nx">bool</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">bool</span>
+ <span class="p">})</span>
+<span class="p">}</span>
+
+<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">acl</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
85 docs/basicAuth.html
@@ -0,0 +1,85 @@
+<!DOCTYPE html> <html> <head> <title>basicAuth.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> basicAuth.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Beau Sorensen
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>basicAuth</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Dependencies</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">bcrypt</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;bcrypt&#39;</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Plugin</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">auth</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">||</span> <span class="p">(</span><span class="nx">options</span> <span class="o">=</span> <span class="p">{})</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">loginPath</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">loginPath</span> <span class="o">||</span> <span class="s1">&#39;username&#39;</span>
+ <span class="p">,</span> <span class="nx">hashPath</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">hashPath</span> <span class="o">||</span> <span class="s1">&#39;hash&#39;</span>
+ <span class="p">,</span> <span class="nx">workFactor</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">workFactor</span> <span class="o">||</span> <span class="mi">10</span>
+ <span class="p">,</span> <span class="nx">query</span> <span class="o">=</span> <span class="p">{}</span>
+ <span class="p">,</span> <span class="nx">fields</span> <span class="o">=</span> <span class="p">{}</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Add paths to schema if not present</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">schema</span><span class="p">.</span><span class="nx">paths</span><span class="p">[</span><span class="nx">loginPath</span><span class="p">])</span> <span class="p">{</span>
+ <span class="nx">fields</span><span class="p">[</span><span class="nx">loginPath</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
+ <span class="nx">type</span><span class="o">:</span> <span class="nb">String</span>
+ <span class="p">,</span> <span class="nx">lowercase</span><span class="o">:</span> <span class="kc">true</span>
+ <span class="p">,</span> <span class="nx">required</span><span class="o">:</span> <span class="kc">true</span>
+ <span class="p">,</span> <span class="nx">index</span><span class="o">:</span> <span class="p">{</span> <span class="nx">unique</span><span class="o">:</span> <span class="kc">true</span> <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">schema</span><span class="p">.</span><span class="nx">paths</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">])</span> <span class="p">{</span>
+ <span class="nx">fields</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="nx">type</span><span class="o">:</span> <span class="nb">String</span> <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">fields</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Main authentication method, compare the given password
+against the current instances stored hash</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;authenticate&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">password</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">password</span> <span class="o">||</span> <span class="o">!</span><span class="k">this</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">])</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="s1">&#39;missing parameters&#39;</span><span class="p">)</span>
+ <span class="p">}</span>
+ <span class="nx">bcrypt</span><span class="p">.</span><span class="nx">compare</span><span class="p">(</span><span class="nx">password</span><span class="p">,</span> <span class="k">this</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">],</span> <span class="nx">next</span><span class="p">)</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Set and encrypt the password of the current model</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;setPassword&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">password</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
+ <span class="nx">bcrypt</span><span class="p">.</span><span class="nx">genSalt</span><span class="p">(</span><span class="nx">workFactor</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">salt</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
+ <span class="nx">bcrypt</span><span class="p">.</span><span class="nx">hash</span><span class="p">(</span><span class="nx">password</span><span class="p">,</span> <span class="nx">salt</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">hash</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
+ <span class="nx">self</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">]</span> <span class="o">=</span> <span class="nx">hash</span>
+ <span class="nx">next</span><span class="p">(</span><span class="kc">null</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="p">})</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Authenticate with the configured login path and password on
+the model layer, passing the authenticated instance into the callback</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="kr">static</span><span class="p">(</span><span class="s1">&#39;authenticate&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">username</span><span class="p">,</span> <span class="nx">password</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">query</span><span class="p">[</span><span class="nx">loginPath</span><span class="p">]</span> <span class="o">=</span> <span class="nx">username</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">findOne</span><span class="p">(</span><span class="nx">query</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">model</span><span class="p">)</span> <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="s1">&#39;model does not exist&#39;</span><span class="p">)</span>
+
+ <span class="nx">model</span><span class="p">.</span><span class="nx">authenticate</span><span class="p">(</span><span class="nx">password</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">valid</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">valid</span><span class="p">)</span> <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="s1">&#39;invalid password&#39;</span><span class="p">,</span> <span class="kc">null</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="p">})</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>Register a new user instance with the supplied attributes, passing
+the new instance into the callback if no errors were found</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="kr">static</span><span class="p">(</span><span class="s1">&#39;register&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">attr</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">attr</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="sr">/duplicate key/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">err</span><span class="p">))</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">loginPath</span> <span class="o">+</span> <span class="s1">&#39; taken&#39;</span><span class="p">)</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Create a virtual path for the password path, storing a temporary
+unencrypted password that will not be persisted, and returning the
+encrypted hash upon request</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span>
+ <span class="p">.</span><span class="nx">virtual</span><span class="p">(</span><span class="s1">&#39;password&#39;</span><span class="p">)</span>
+ <span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">]</span>
+ <span class="p">})</span>
+ <span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">password</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">_password</span> <span class="o">=</span> <span class="nx">password</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Create a hash of the password if one has not already been made, this
+should only be called the first time a password is set, the <code>setPassword</code>
+method will need to be used after it has been created</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="nx">pre</span><span class="p">(</span><span class="s1">&#39;save&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_password</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="k">this</span><span class="p">[</span><span class="nx">hashPath</span><span class="p">])</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">setPassword</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_password</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
+ <span class="nx">next</span><span class="p">()</span>
+ <span class="p">})</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+ <span class="nx">next</span><span class="p">()</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+<span class="p">}</span>
+
+<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">auth</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
186 docs/docco.css
@@ -0,0 +1,186 @@
+/*--------------------- Layout and Typography ----------------------------*/
+body {
+ font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
+ font-size: 15px;
+ line-height: 22px;
+ color: #252519;
+ margin: 0; padding: 0;
+}
+a {
+ color: #261a3b;
+}
+ a:visited {
+ color: #261a3b;
+ }
+p {
+ margin: 0 0 15px 0;
+}
+h1, h2, h3, h4, h5, h6 {
+ margin: 0px 0 15px 0;
+}
+ h1 {
+ margin-top: 40px;
+ }
+#container {
+ position: relative;
+}
+#background {
+ position: fixed;
+ top: 0; left: 525px; right: 0; bottom: 0;
+ background: #f5f5ff;
+ border-left: 1px solid #e5e5ee;
+ z-index: -1;
+}
+#jump_to, #jump_page {
+ background: white;
+ -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
+ -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px;
+ font: 10px Arial;
+ text-transform: uppercase;
+ cursor: pointer;
+ text-align: right;
+}
+#jump_to, #jump_wrapper {
+ position: fixed;
+ right: 0; top: 0;
+ padding: 5px 10px;
+}
+ #jump_wrapper {
+ padding: 0;
+ display: none;
+ }
+ #jump_to:hover #jump_wrapper {
+ display: block;
+ }
+ #jump_page {
+ padding: 5px 0 3px;
+ margin: 0 0 25px 25px;
+ }
+ #jump_page .source {
+ display: block;
+ padding: 5px 10px;
+ text-decoration: none;
+ border-top: 1px solid #eee;
+ }
+ #jump_page .source:hover {
+ background: #f5f5ff;
+ }
+ #jump_page .source:first-child {
+ }
+table td {
+ border: 0;
+ outline: 0;
+}
+ td.docs, th.docs {
+ max-width: 450px;
+ min-width: 450px;
+ min-height: 5px;
+ padding: 10px 25px 1px 50px;
+ overflow-x: hidden;
+ vertical-align: top;
+ text-align: left;
+ }
+ .docs pre {
+ margin: 15px 0 15px;
+ padding-left: 15px;
+ }
+ .docs p tt, .docs p code {
+ background: #f8f8ff;
+ border: 1px solid #dedede;
+ font-size: 12px;
+ padding: 0 0.2em;
+ }
+ .pilwrap {
+ position: relative;
+ }
+ .pilcrow {
+ font: 12px Arial;
+ text-decoration: none;
+ color: #454545;
+ position: absolute;
+ top: 3px; left: -20px;
+ padding: 1px 2px;
+ opacity: 0;
+ -webkit-transition: opacity 0.2s linear;
+ }
+ td.docs:hover .pilcrow {
+ opacity: 1;
+ }
+ td.code, th.code {
+ padding: 14px 15px 16px 25px;
+ width: 100%;
+ vertical-align: top;
+ background: #f5f5ff;
+ border-left: 1px solid #e5e5ee;
+ }
+ pre, tt, code {
+ font-size: 12px; line-height: 18px;
+ font-family: Monaco, Consolas, "Lucida Console", monospace;
+ margin: 0; padding: 0;
+ }
+
+
+/*---------------------- Syntax Highlighting -----------------------------*/
+td.linenos { background-color: #f0f0f0; padding-right: 10px; }
+span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
+body .hll { background-color: #ffffcc }
+body .c { color: #408080; font-style: italic } /* Comment */
+body .err { border: 1px solid #FF0000 } /* Error */
+body .k { color: #954121 } /* Keyword */
+body .o { color: #666666 } /* Operator */
+body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
+body .cp { color: #BC7A00 } /* Comment.Preproc */
+body .c1 { color: #408080; font-style: italic } /* Comment.Single */
+body .cs { color: #408080; font-style: italic } /* Comment.Special */
+body .gd { color: #A00000 } /* Generic.Deleted */
+body .ge { font-style: italic } /* Generic.Emph */
+body .gr { color: #FF0000 } /* Generic.Error */
+body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
+body .gi { color: #00A000 } /* Generic.Inserted */
+body .go { color: #808080 } /* Generic.Output */
+body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
+body .gs { font-weight: bold } /* Generic.Strong */
+body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
+body .gt { color: #0040D0 } /* Generic.Traceback */
+body .kc { color: #954121 } /* Keyword.Constant */
+body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */
+body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */
+body .kp { color: #954121 } /* Keyword.Pseudo */
+body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */
+body .kt { color: #B00040 } /* Keyword.Type */
+body .m { color: #666666 } /* Literal.Number */
+body .s { color: #219161 } /* Literal.String */
+body .na { color: #7D9029 } /* Name.Attribute */
+body .nb { color: #954121 } /* Name.Builtin */
+body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
+body .no { color: #880000 } /* Name.Constant */
+body .nd { color: #AA22FF } /* Name.Decorator */
+body .ni { color: #999999; font-weight: bold } /* Name.Entity */
+body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
+body .nf { color: #0000FF } /* Name.Function */
+body .nl { color: #A0A000 } /* Name.Label */
+body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
+body .nt { color: #954121; font-weight: bold } /* Name.Tag */
+body .nv { color: #19469D } /* Name.Variable */
+body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
+body .w { color: #bbbbbb } /* Text.Whitespace */
+body .mf { color: #666666 } /* Literal.Number.Float */
+body .mh { color: #666666 } /* Literal.Number.Hex */
+body .mi { color: #666666 } /* Literal.Number.Integer */
+body .mo { color: #666666 } /* Literal.Number.Oct */
+body .sb { color: #219161 } /* Literal.String.Backtick */
+body .sc { color: #219161 } /* Literal.String.Char */
+body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */
+body .s2 { color: #219161 } /* Literal.String.Double */
+body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
+body .sh { color: #219161 } /* Literal.String.Heredoc */
+body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
+body .sx { color: #954121 } /* Literal.String.Other */
+body .sr { color: #BB6688 } /* Literal.String.Regex */
+body .s1 { color: #219161 } /* Literal.String.Single */
+body .ss { color: #19469D } /* Literal.String.Symbol */
+body .bp { color: #954121 } /* Name.Builtin.Pseudo */
+body .vc { color: #19469D } /* Name.Variable.Class */
+body .vg { color: #19469D } /* Name.Variable.Global */
+body .vi { color: #19469D } /* Name.Variable.Instance */
+body .il { color: #666666 } /* Literal.Number.Integer.Long */
View
28 docs/getdbrefs.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html> <html> <head> <title>getdbrefs.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> getdbrefs.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Tom Blobaum
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>Get DBRefs</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Plugin</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">getdbrefs</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{}</span>
+
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;getdbrefs&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">refs</span> <span class="o">=</span> <span class="p">{}</span>
+ <span class="p">,</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
+
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">eachPath</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">path</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">caster</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">caster</span>
+ <span class="p">,</span> <span class="nx">opt</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">options</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="nx">caster</span> <span class="o">&amp;&amp;</span> <span class="nx">caster</span><span class="p">.</span><span class="nx">options</span> <span class="o">&amp;&amp;</span> <span class="nx">caster</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">ref</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">refs</span><span class="p">[</span><span class="nx">caster</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">self</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">opt</span> <span class="o">&amp;&amp;</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">ref</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">refs</span><span class="p">[</span><span class="nx">opt</span><span class="p">.</span><span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">self</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+ <span class="nx">fn</span> <span class="o">&amp;&amp;</span> <span class="nx">fn</span><span class="p">(</span><span class="nx">refs</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">refs</span>
+ <span class="p">})</span>
+<span class="p">}</span>
+
+<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">getdbrefs</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
59 docs/index.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html> <html> <head> <title>index.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> index.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Beau Sorensen
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>Helpers</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Setter to ensure sparse fields are undefined if empty</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">emptyToSparse</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="p">(</span><span class="o">!</span><span class="nx">str</span> <span class="o">||</span> <span class="o">!</span><span class="nx">str</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">?</span> <span class="kc">undefined</span> <span class="o">:</span> <span class="nx">str</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Validator to ensure that a property exists</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">validatePresenceOf</span> <span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">value</span> <span class="o">&amp;&amp;</span> <span class="nx">value</span><span class="p">.</span><span class="nx">length</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Create an object out of a nested path</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">nestedPath</span> <span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">obj</span><span class="p">)</span> <span class="k">return</span> <span class="nx">obj</span>
+ <span class="kd">var</span> <span class="nx">keys</span> <span class="o">=</span> <span class="nx">key</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">keys</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">key</span> <span class="o">=</span> <span class="nx">keys</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span>
+ <span class="k">return</span> <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">],</span> <span class="nx">keys</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">),</span> <span class="nx">val</span><span class="p">)</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>if (arguments.length > 2) obj[key] = val</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">val</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Convert a document or an array of documents to objects</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">dataToObjects</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">data</span><span class="p">)</span> <span class="k">return</span> <span class="kc">null</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">data</span> <span class="k">instanceof</span> <span class="nb">Array</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">data</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">doc</span><span class="p">.</span><span class="nx">toObject</span><span class="p">()</span>
+ <span class="p">})</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="nx">data</span><span class="p">.</span><span class="nx">toObject</span><span class="p">()</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Execute if function or return object</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">objectOrFunction</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">toString</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;[object Function]&#39;</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">obj</span><span class="p">()</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="nx">obj</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Destroy a database and disconnect</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">dropDatabase</span> <span class="p">(</span><span class="nx">db</span><span class="p">,</span> <span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">db</span><span class="p">.</span><span class="nx">connection</span><span class="p">.</span><span class="nx">db</span><span class="p">.</span><span class="nx">executeDbCommand</span><span class="p">({</span>
+ <span class="nx">dropDatabase</span><span class="o">:</span> <span class="mi">1</span>
+ <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">db</span><span class="p">.</span><span class="nx">disconnect</span><span class="p">()</span>
+ <span class="nx">fn</span> <span class="o">&amp;&amp;</span> <span class="nx">fn</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span>
+ <span class="p">})</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>Drop all collections in a database</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">dropCollections</span> <span class="p">(</span><span class="nx">db</span><span class="p">,</span> <span class="nx">each</span><span class="p">,</span> <span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">each</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">fn</span><span class="p">)</span> <span class="p">{</span> <span class="nx">fn</span> <span class="o">=</span> <span class="nx">each</span><span class="p">;</span> <span class="nx">each</span> <span class="o">=</span> <span class="kc">null</span> <span class="p">}</span>
+ <span class="kd">var</span> <span class="nx">conn</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">connection</span>
+ <span class="p">,</span> <span class="nx">cols</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">conn</span><span class="p">.</span><span class="nx">collections</span><span class="p">)</span>
+
+ <span class="nx">cols</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;name: &#39;</span><span class="p">,</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span>
+ <span class="nx">conn</span><span class="p">.</span><span class="nx">collections</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">drop</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span> <span class="o">==</span> <span class="s1">&#39;ns not found&#39;</span><span class="p">)</span> <span class="nx">err</span> <span class="o">=</span> <span class="kc">null</span>
+ <span class="nx">each</span> <span class="o">&amp;&amp;</span> <span class="nx">each</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="p">})</span>
+ <span class="nx">fn</span> <span class="o">&amp;&amp;</span> <span class="nx">fn</span><span class="p">(</span><span class="kc">null</span><span class="p">)</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Exports</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
+ <span class="nx">emptyToSparse</span><span class="o">:</span> <span class="nx">emptyToSparse</span>
+<span class="p">,</span> <span class="nx">validatePresenceOf</span><span class="o">:</span> <span class="nx">validatePresenceOf</span>
+<span class="p">,</span> <span class="nx">nestedPath</span><span class="o">:</span> <span class="nx">nestedPath</span>
+<span class="p">,</span> <span class="nx">dataToObjects</span><span class="o">:</span> <span class="nx">dataToObjects</span>
+<span class="p">,</span> <span class="nx">objectOrFunction</span><span class="o">:</span> <span class="nx">objectOrFunction</span>
+<span class="p">,</span> <span class="nx">dropDatabase</span><span class="o">:</span> <span class="nx">dropDatabase</span>
+<span class="p">,</span> <span class="nx">dropCollections</span><span class="o">:</span> <span class="nx">dropCollections</span>
+<span class="p">}</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
19 docs/merge.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html> <html> <head> <title>merge.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> merge.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Tom Blobaum
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>Merge</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Plugin</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">merge</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{}</span>
+
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">method</span><span class="p">(</span><span class="s1">&#39;merge&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
+ <span class="nx">schema</span><span class="p">.</span><span class="nx">eachPath</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Merge all set fields, except for the ObjectID</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">name</span> <span class="o">!=</span> <span class="s1">&#39;_id&#39;</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">doc</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">self</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">doc</span><span class="p">[</span><span class="nx">name</span><span class="p">])</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+ <span class="k">return</span> <span class="k">this</span>
+ <span class="p">})</span>
+<span class="p">}</span>
+
+<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">merge</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
61 docs/obfuscate.html
@@ -0,0 +1,61 @@
+<!DOCTYPE html> <html> <head> <title>obfuscate.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> obfuscate.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Beau Sorensen
+ Contributors: Jacob Chapel
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>Obfuscate</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Dependencies</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">crypto</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;crypto&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">nestedPath</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../helpers&#39;</span><span class="p">).</span><span class="nx">nestedPath</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Plugin</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">obfuscate</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">||</span> <span class="p">(</span><span class="nx">options</span> <span class="o">=</span> <span class="p">{})</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">encryptPath</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">encryptPath</span> <span class="o">||</span> <span class="s1">&#39;obfuscate&#39;</span> <span class="c1">// Getter path for encrypting</span>
+ <span class="p">,</span> <span class="nx">decryptPath</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">decryptPath</span> <span class="o">||</span> <span class="s1">&#39;deobfuscate&#39;</span> <span class="c1">// Setter path for decrypting</span>
+ <span class="p">,</span> <span class="nx">algorithm</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">algorithm</span> <span class="o">||</span> <span class="s1">&#39;aes-256-cbc&#39;</span> <span class="c1">// Encryption algorithm</span>
+ <span class="p">,</span> <span class="nx">key</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">key</span> <span class="o">||</span> <span class="s1">&#39;secret&#39;</span> <span class="c1">// Encryption key</span>
+ <span class="p">,</span> <span class="nx">from</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">from</span> <span class="o">||</span> <span class="s1">&#39;utf8&#39;</span> <span class="c1">// Original encoding</span>
+ <span class="p">,</span> <span class="nx">to</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">to</span> <span class="o">||</span> <span class="s1">&#39;hex&#39;</span> <span class="c1">// Conversion encoding</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Encrypt a string with the options provided</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">encrypt</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">cipher</span> <span class="o">=</span> <span class="nx">crypto</span><span class="p">.</span><span class="nx">createCipher</span><span class="p">(</span><span class="nx">algorithm</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">crypted</span> <span class="o">=</span> <span class="nx">cipher</span><span class="p">.</span><span class="nx">update</span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">)</span> <span class="o">+</span> <span class="nx">cipher</span><span class="p">.</span><span class="kr">final</span><span class="p">(</span><span class="nx">to</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">crypted</span><span class="p">;</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Decrypt an encrypted string</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">decrypt</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">decipher</span> <span class="o">=</span> <span class="nx">crypto</span><span class="p">.</span><span class="nx">createDecipher</span><span class="p">(</span><span class="nx">algorithm</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">dec</span> <span class="o">=</span> <span class="nx">decipher</span><span class="p">.</span><span class="nx">update</span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="nx">to</span><span class="p">,</span> <span class="nx">from</span><span class="p">)</span> <span class="o">+</span> <span class="nx">decipher</span><span class="p">.</span><span class="kr">final</span><span class="p">(</span><span class="nx">from</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">dec</span><span class="p">;</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Recursively encode all ObjectID's in schema</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">encode</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">doc</span><span class="p">,</span> <span class="nx">toEncrypt</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">doc</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span>
+ <span class="kd">var</span> <span class="nx">method</span> <span class="o">=</span> <span class="p">(</span><span class="nx">toEncrypt</span><span class="p">)</span> <span class="o">?</span> <span class="nx">encrypt</span> <span class="o">:</span> <span class="nx">decrypt</span>
+ <span class="p">,</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">(</span><span class="nx">doc</span><span class="p">.</span><span class="nx">toObject</span><span class="p">)</span> <span class="o">?</span> <span class="nx">doc</span><span class="p">.</span><span class="nx">toObject</span><span class="p">()</span> <span class="o">:</span> <span class="nx">doc</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Traverse through all schema paths</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="nx">eachPath</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">path</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">val</span> <span class="o">=</span> <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">doc</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>ObjectID paths</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">instance</span> <span class="o">===</span> <span class="s1">&#39;ObjectID&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">method</span><span class="p">(</span><span class="nx">val</span><span class="p">.</span><span class="nx">toString</span><span class="p">()))</span>
+ <span class="p">}</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">casterConstructor</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Array of DBRefs</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!!~</span><span class="nx">path</span><span class="p">.</span><span class="nx">casterConstructor</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;ObjectId&#39;</span><span class="p">))</span> <span class="p">{</span>
+ <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">name</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">v</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">name</span><span class="p">)[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">method</span><span class="p">(</span><span class="nx">val</span><span class="p">[</span><span class="nx">k</span><span class="p">].</span><span class="nx">toString</span><span class="p">())</span>
+ <span class="p">})</span> </pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Array of embedded schemas</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!!~</span><span class="nx">path</span><span class="p">.</span><span class="nx">casterConstructor</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;EmbeddedDocument&#39;</span><span class="p">))</span> <span class="p">{</span>
+ <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">name</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">v</span><span class="p">,</span> <span class="nx">k</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">nestedPath</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">name</span><span class="p">)[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">encode</span><span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">val</span><span class="p">[</span><span class="nx">k</span><span class="p">],</span> <span class="nx">toEncrypt</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+ <span class="k">return</span> <span class="nx">obj</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Add encryption methods to both instance and model</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">;[</span><span class="s1">&#39;method&#39;</span>
+ <span class="p">,</span> <span class="s1">&#39;static&#39;</span>
+ <span class="p">].</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">method</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">schema</span><span class="p">[</span><span class="nx">method</span><span class="p">]({</span>
+ <span class="nx">encrypt</span><span class="o">:</span> <span class="nx">encrypt</span>
+ <span class="p">,</span> <span class="nx">decrypt</span><span class="o">:</span> <span class="nx">decrypt</span>
+ <span class="p">})</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Manually encode an object through the model</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="kr">static</span><span class="p">(</span><span class="s1">&#39;encode&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">toEncrypt</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">obj</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span>
+ <span class="k">return</span> <span class="nx">encode</span><span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">toEncrypt</span><span class="p">)</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Encode an instance when the virtual is called</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="nx">virtual</span><span class="p">(</span><span class="nx">encryptPath</span><span class="p">).</span><span class="nx">get</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">encode</span><span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="kc">true</span><span class="p">)</span>
+ <span class="p">})</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Decode an object and set it to the instance</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="nx">virtual</span><span class="p">(</span><span class="nx">decryptPath</span><span class="p">).</span><span class="nx">set</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">v</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">doc</span> <span class="o">=</span> <span class="nx">encode</span><span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">v</span><span class="p">,</span> <span class="kc">false</span><span class="p">)</span>
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">=</span> <span class="nx">doc</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span>
+ <span class="p">}</span>
+ <span class="p">})</span>
+<span class="p">}</span>
+
+<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">obfuscate</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
73 docs/pagination.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html> <html> <head> <title>pagination.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="index.html"> index.js </a> <a class="source" href="acl.html"> acl.js </a> <a class="source" href="basicAuth.html"> basicAuth.js </a> <a class="source" href="getdbrefs.html"> getdbrefs.js </a> <a class="source" href="merge.html"> merge.js </a> <a class="source" href="obfuscate.html"> obfuscate.js </a> <a class="source" href="pagination.html"> pagination.js </a> <a class="source" href="pubsub.html"> pubsub.js </a> <a class="source" href="removeDefaults.html"> removeDefaults.js </a> <a class="source" href="rest.html"> rest.js </a> <a class="source" href="slugify.html"> slugify.js </a> <a class="source" href="timestamp.html"> timestamp.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> pagination.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>(c) 2012 Beau Sorensen
+ MIT Licensed
+ For all details and documentation:
+ https://github.com/tblobaum/mongoose-troop</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>Pagination</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Plugin</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">pagination</span> <span class="p">(</span><span class="nx">schema</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">||</span> <span class="p">(</span><span class="nx">options</span> <span class="o">=</span> <span class="p">{})</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">defaultLimit</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">defaultLimit</span> <span class="o">||</span> <span class="mi">10</span>
+ <span class="p">,</span> <span class="nx">defaultQuery</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">defaultQuery</span> <span class="o">||</span> <span class="p">{}</span>
+ <span class="p">,</span> <span class="nx">defaultFields</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">defaultFields</span> <span class="o">||</span> <span class="p">[]</span>
+ <span class="p">,</span> <span class="nx">remember</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">remember</span> <span class="o">||</span> <span class="kc">false</span>
+ <span class="p">,</span> <span class="nx">defaultPage</span> <span class="o">=</span> <span class="mi">1</span>
+ <span class="p">,</span> <span class="nx">currentLimit</span> <span class="o">=</span> <span class="nx">defaultLimit</span>
+ <span class="p">,</span> <span class="nx">currentQuery</span> <span class="o">=</span> <span class="nx">defaultQuery</span>
+ <span class="p">,</span> <span class="nx">currentFields</span> <span class="o">=</span> <span class="nx">defaultFields</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Main pagination method, can pass <code>query</code>, <code>page</code>, <code>limit</code>
+and <code>fields</code> into the options to pass directly into the
+mongoose ORM, returns the standard <code>err</code> and <code>docs</code>, along with
+the total object count, total pages, and current page</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">schema</span><span class="p">.</span><span class="kr">static</span><span class="p">(</span><span class="s1">&#39;paginate&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">opt</span><span class="p">,</span> <span class="nx">fn</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">fn</span><span class="p">)</span> <span class="p">{</span> <span class="nx">fn</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">;</span> <span class="nx">opt</span> <span class="o">=</span> <span class="p">{}</span> <span class="p">}</span>
+ <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
+ <span class="p">,</span> <span class="nx">query</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">query</span> <span class="o">||</span> <span class="nx">currentQuery</span>
+ <span class="p">,</span> <span class="nx">page</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">page</span> <span class="o">||</span> <span class="nx">defaultPage</span>
+ <span class="p">,</span> <span class="nx">limit</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">limit</span> <span class="o">||</span> <span class="nx">currentLimit</span>
+ <span class="p">,</span> <span class="nx">fields</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">fields</span> <span class="o">||</span> <span class="nx">currentFields</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="nx">remember</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">opt</span><span class="p">.</span><span class="nx">query</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nx">currentQuery</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">query</span><span class="p">)</span>
+ <span class="nx">opt</span><span class="p">.</span><span class="nx">limit</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nx">currentLimit</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">limit</span><span class="p">)</span>
+ <span class="nx">opt</span><span class="p">.</span><span class="nx">fields</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nx">currentFields</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">fields</span><span class="p">)</span>
+ <span class="p">}</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">query</span><span class="p">,</span> <span class="nx">fields</span><span class="p">,</span> <span class="p">{</span>
+ <span class="nx">skip</span><span class="o">:</span> <span class="p">(</span><span class="nx">limit</span> <span class="o">*</span> <span class="p">(</span><span class="nx">page</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span>
+ <span class="p">,</span> <span class="nx">limit</span><span class="o">:</span> <span class="nx">limit</span>
+ <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">docs</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="p">(</span><span class="nx">fn</span> <span