Permalink
Browse files

Adding plugin chaining where applicable, nested path support for merge

  • Loading branch information...
sorensen committed Jan 24, 2012
1 parent 6556930 commit 66bbac992062b2f8b5e6865df95e4d4f9360302f
Showing with 62 additions and 26 deletions.
  1. +0 −1 README.md
  2. +17 −10 helpers/index.js
  3. +2 −0 lib/acl.js
  4. +4 −0 lib/basicAuth.js
  5. +5 −2 lib/merge.js
  6. +3 −0 lib/pagination.js
  7. +6 −5 lib/pubsub.js
  8. +4 −0 lib/rest.js
  9. +21 −8 test/merge.test.js
View
@@ -620,7 +620,6 @@ Get the dbrefs from a schema
instance.getdbrefs(function (refs) {
// ...
})
-
```
## Note
View
@@ -9,7 +9,9 @@
// Setter to ensure sparse fields are undefined if empty
function emptyToSparse (str) {
- return (!str || !str.length) ? undefined : str
+ return (!str || !str.length)
+ ? undefined
+ : str
}
// Validator to ensure that a property exists
@@ -18,16 +20,19 @@ function validatePresenceOf (value) {
}
// Create an object out of a nested path
-function nestedPath (obj, key, val) {
- if (!obj) return obj
- var keys = key.split('.')
+function nestedPath (obj, path, val) {
+ if (typeof obj !== 'object') {
+ return obj
+ }
+ var keys = path.split('.')
if (keys.length > 1) {
- key = keys.shift()
- return nestedPath(obj[key], keys.join('.'), val)
+ path = keys.shift()
+ return nestedPath(obj[path], keys.join('.'), val)
+ }
+ if (val !== undefined) {
+ obj[path] = val
}
- // if (arguments.length > 2) obj[key] = val
- val && (obj[key] = val)
- return obj[key]
+ return obj[path]
}
// Convert a document or an array of documents to objects
@@ -68,7 +73,9 @@ function dropCollections (db, each, fn) {
cols.forEach(function (name, k) {
console.log('name: ', name, k)
conn.collections[name].drop(function (err) {
- if (err == 'ns not found') err = null
+ if (err == 'ns not found') {
+ err = null
+ }
each && each(err, name)
})
})
View
@@ -28,13 +28,15 @@ function acl (schema, options) {
this.acl.push(key)
this.markModified('acl')
}
+ return this
})
schema.method('removeAccess', function (key) {
if (this.access(key)) {
this.acl.splice(this.acl.indexOf(key), 1)
this.markModified('acl')
}
+ return this
})
schema.method('access', function (key, next) {
View
@@ -42,6 +42,7 @@ function auth (schema, options) {
return next('missing parameters')
}
bcrypt.compare(password, this[hashPath], next)
+ return this
})
// Set and encrypt the password of the current model
@@ -55,6 +56,7 @@ function auth (schema, options) {
next(null)
})
})
+ return this
})
// Authenticate with the configured login path and password on
@@ -72,6 +74,7 @@ function auth (schema, options) {
return next('invalid password', null)
})
})
+ return this
})
// Register a new user instance with the supplied attributes, passing
@@ -86,6 +89,7 @@ function auth (schema, options) {
}
return next(null, model)
})
+ return this
})
// Create a virtual path for the password path, storing a temporary
View
@@ -7,16 +7,19 @@
// Merge
// -----
+var nestedPath = require('../helpers').nestedPath
+
// Plugin
function merge (schema, options) {
options = options || {}
schema.method('merge', function (doc) {
var self = this
schema.eachPath(function (name) {
+ var val = nestedPath(doc, name)
// Merge all set fields, except for the ObjectID
- if (name != '_id' && typeof doc[name] !== 'undefined') {
- self.set(name, doc[name])
+ if (name !== '_id' && val !== undefined) {
+ nestedPath(self, name, val)
}
})
return this
View
@@ -49,6 +49,7 @@ function pagination (schema, options) {
fn && fn(err, docs, count, Math.ceil(count / limit), page)
})
})
+ return this
})
// Shortcut method for retrieving the first page
@@ -64,6 +65,7 @@ function pagination (schema, options) {
, limit: limit
, fields: fields
}, fn)
+ return this
})
// Shortcut method for retrieving the last page
@@ -83,6 +85,7 @@ function pagination (schema, options) {
, fields: fields
}, fn)
})
+ return this
})
}
View
@@ -38,6 +38,7 @@ function pubsub (schema, options) {
next(err)
})
})
+ return this
})
}
@@ -47,17 +48,15 @@ function pubsub (schema, options) {
].forEach(function (key) {
schema[key]('publish', function (doc, opt, fn) {
if (arguments.length === 2) {
- fn = opt
- opt = {}
+ fn = opt; opt = {}
} else if (arguments.length === 1) {
- fn = doc
- doc = false
- opt = {}
+ fn = doc; doc = false; opt = {}
}
pub.publish(this.getChannel(), JSON.stringify({
options: opt
, data: (doc ? doc : this).toObject()
}), fn)
+ return this
})
})
@@ -90,6 +89,7 @@ function pubsub (schema, options) {
fn(chan, resp)
}
})
+ return this
})
})
@@ -104,6 +104,7 @@ function pubsub (schema, options) {
sub[method](this.getChannel(), function (err, result) {
fn && fn(err, result)
})
+ return this
})
})
})
View
@@ -32,6 +32,7 @@ function rest (schema, options) {
this.findById(id, function (err, doc) {
fn(err, dataToObjects(doc))
})
+ return this
}
function update (data, fn) {
@@ -45,6 +46,7 @@ function rest (schema, options) {
if (err || !count) return fn(err, false)
fn(null, true)
})
+ return this
}
function del (id, fn) {
@@ -55,6 +57,7 @@ function rest (schema, options) {
fn(null, true)
})
})
+ return this
}
function search (query, fn) {
@@ -72,6 +75,7 @@ function rest (schema, options) {
}, function (err, docs) {
fn(err, dataToObjects(docs))
})
+ return this
}
}
View
@@ -21,6 +21,11 @@ describe('Merge', function () {
, num2: Number
, bool2: Boolean
, ref: { type: ObjectId, ref: BarSchema }
+ , und: String
+ , nested: {
+ one: String
+ , two: Boolean
+ }
})
FooSchema.plugin(merge)
@@ -39,6 +44,11 @@ describe('Merge', function () {
str2: 'there'
, num2: 200
, bool2: false
+ , und: undefined
+ , nested: {
+ one: 'one'
+ , two: true
+ }
})
var hello = new FooModel({
@@ -50,21 +60,24 @@ describe('Merge', function () {
done()
})
- it('should merge the documents', function (done) {
- foo.merge(bar)
- assert.strictEqual(foo.str2, 'there')
- assert.strictEqual(Number(foo.num2), 200)
- assert.strictEqual(foo.bool2, false)
- done()
- })
-
it('should merge the documents', function (done) {
bar.merge(foo)
assert.strictEqual(bar.str, 'hello')
assert.strictEqual(Number(bar.num), 10)
assert.strictEqual(bar.bool, true)
done()
})
+
+ it('should merge the documents with nested paths', function (done) {
+ foo.merge(bar)
+ assert.strictEqual(foo.str2, 'there')
+ assert.strictEqual(Number(foo.num2), 200)
+ assert.strictEqual(foo.bool2, false)
+ assert.strictEqual(foo.und, undefined)
+ assert.strictEqual(foo.nested.one, 'one')
+ assert.strictEqual(foo.nested.two, true)
+ done()
+ })
it('should work with dbrefs', function (done) {
bar.merge(hello)

0 comments on commit 66bbac9

Please sign in to comment.