Skip to content

Commit

Permalink
added extra parameters for the create method
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmt committed Jul 11, 2012
1 parent 3c56083 commit cb92aa4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
37 changes: 29 additions & 8 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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();
})

Expand All @@ -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();
})
})
})
})
})

Expand Down

0 comments on commit cb92aa4

Please sign in to comment.