Permalink
Browse files

added extra parameters for the create method

  • Loading branch information...
1 parent 3c56083 commit cb92aa4a65dc7e1d3160a8421a0922e03fce73aa @romanmt romanmt committed Jul 11, 2012
Showing with 49 additions and 11 deletions.
  1. +12 −0 README.md
  2. +7 −2 index.js
  3. +1 −1 package.json
  4. +29 −8 test/index_test.js
View
@@ -44,6 +44,18 @@ Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {
});
```
+You can also include properties that aren't used in the
+find call, but will be added to the object if it is created.
+
+```javascript
+Click.create({ip: '127.0.0.1'}, {browser: 'Mozilla'}, function(err, val) {
+ Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'Chrome'}, function(err, click) {
+ console.log('A click from "%s" using "%s" was found', click.ip, click.browser);
+ // prints A click from "127.0.0.1" using "Mozilla" was found
+ })
+});
+```
+
# errors
```javascript
var supergoose = require('supergoose')
View
@@ -4,13 +4,18 @@ _.mixin(_.str.exports());
module.exports = exports = function superGoosePlugin(schema, options) {
if(options) var messages = options.messages
- schema.statics.findOrCreate = function findOrCreate(object, callback) {
+
+ schema.statics.findOrCreate = function findOrCreate(object, properties, callback) {
+ if(_.isFunction(properties)) {
+ callback = properties
+ properties = {}
+ }
var self = this;
this.findOne(object, function(err, result) {
if(err || result) {
callback(err, result)
} else {
- var obj = new self(object)
+ var obj = new self(_.extend(object, properties))
obj.save(function(err) {
callback(err, obj)
});
View
@@ -1,7 +1,7 @@
{
"name": "supergoose"
, "description": "Mongoose plugin for simple addons like findOrCreate"
- , "version": "0.0.4"
+ , "version": "0.0.5"
, "author": "Matt Roman"
, "keywords": ["supergoose", "mongodb", "mongoose", "plugin", "findOrCreate"]
, "repository": {
View
@@ -11,7 +11,8 @@ mongoose.connection.on('error', function (err) {
});
var ClickSchema = new Schema({
- ip : {type: String, required: true}
+ ip : {type: String, required: true}
+ , browser: String
})
var messages = {
@@ -21,8 +22,8 @@ var messages = {
ClickSchema.plugin(supergoose, {messages: messages});
var Click = mongoose.model('Click', ClickSchema);
-after(function(done) {
- mongoose.connection.db.dropDatabase()
+beforeEach(function(done) {
+ Click.collection.remove()
done();
})
@@ -38,14 +39,34 @@ describe('findOrCreate', function() {
})
it("returns the object if it already exists", function(done) {
- Click.create({ip: '127.0.0.1'})
- Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {
- click.ip.should.eql('127.0.0.1')
- Click.count({}, function(err, num) {
- num.should.equal(1)
+ Click.create({ip: '127.0.0.1'}, function(err, val) {
+ Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {
+ click.ip.should.eql('127.0.0.1')
+ Click.count({}, function(err, num) {
+ num.should.equal(1)
+ done();
+ })
+ })
+ })
+ })
+
+ describe('with extra properties', function() {
+ it("extends the object with additional properties", function(done) {
+ Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'Mozilla'}, function(err, click) {
+ click.should.have.property('ip', '127.0.0.1')
+ click.should.have.property('browser', 'Mozilla')
done();
})
})
+
+ it("finds the object without extra params", function(done) {
+ Click.create({ip: '127.0.0.1', browser: 'Chrome'}, function(err, val) {
+ Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'IE'}, function(err, click) {
+ click.should.have.property('browser', 'Chrome')
+ done();
+ })
+ })
+ })
})
})

0 comments on commit cb92aa4

Please sign in to comment.