Skip to content

Simple plugin for Mongoose which adds a findOrCreate method to models.

License

Notifications You must be signed in to change notification settings

drudge/mongoose-findorcreate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoose findOrCreate Plugin Build Status

Simple plugin for Mongoose which adds a findOrCreate method to models. This is useful for libraries like Passport which require it.

Installation

npm install mongoose-findorcreate

Usage

var findOrCreate = require('mongoose-findorcreate')
var ClickSchema = new Schema({ ... });
ClickSchema.plugin(findOrCreate);
var Click = mongoose.model('Click', ClickSchema);

The Click model now has a findOrCreate static method

Click.findOrCreate({ip: '127.0.0.1'}, function(err, click, created) {
  // created will be true here
  console.log('A new click from "%s" was inserted', click.ip);
  Click.findOrCreate({}, function(err, click, created) {
    // created will be false here
    console.log('Did not create a new click for "%s"', click.ip);
  })
});

You can also include properties that aren't used in the find call, but will be added to the object if it is created.

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
  })
});

Promise Support

Choose your Promise library by setting Mongoose.Promise.

The returned Promise shall resolve to an object with keys doc and created on success. It shall be rejected with err on failure.

// Use environment-provided Promise (necessary to silence a Mongoose warning).
mongoose.Promise = Promise;
// To a findOrCreate().
Click.findOrCreate({ip: '127.0.0.2'}).then(function (result) {
  click = result.doc;
  console.log('A click from', click.ip, ' using ', click.browser, ' was ', click.created ? 'created' : 'found');
})

License

(The MIT License)

Copyright (c) 2012-2017 Nicholas Penree <nick@penree.com>

Based on supergoose: Copyright (c) 2012 Jamplify

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.