Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit 42a4434f4f26453a1b6d5c5c5fe5df56afcf3431 0 parents
@romanmt romanmt authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
19 LICENSE
@@ -0,0 +1,19 @@
+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.
0  README.md
No changes.
12 index.js
@@ -0,0 +1,12 @@
+module.exports = exports = function superGoosePlugin(schema) {
+ schema.statics.findOrCreate = function findOrCreate(object, callback) {
+ var self = this;
+ this.findOne(object, function(err, result) {
+ if(err || result)
+ callback(err, result)
+ else
+ self.create(object, callback)
+ })
+ }
+}
+
21 package.json
@@ -0,0 +1,21 @@
+{
+ "name": "supergoose"
+ , "description": "Mongoose plugin for simple add ons like find or create"
+ , "version": "1.0.0"
+ , "author": "Matt Roman"
+ , "keywords": ["supergoose", "mongodb", "mongoose", "plugin", "findOrCreate"]
+ , "repository": {
+ "type": "git"
+ , "url": "git://github.com/jamplify/supergoose"
+ }
+ , "devDependencies": {
+ "should": ">= 0.2.1"
+ , "mocha": ">= 0.13.0"
+ , "sinon": ">= 1.3.1"
+ , "mongoose": ">= 2.6.5"
+ }
+ , "license": "MIT"
+ , "engine": {
+ "node": ">= 0.6"
+ }
+}
33 test/index_test.js
@@ -0,0 +1,33 @@
+var mocha = require('mocha')
+ , should = require('should')
+ , sinon = require('sinon')
+ , mongoose = require('mongoose')
+ , Schema = mongoose.Schema
+ , supergoose = require('../index.js')
+
+var ClickSchema = new Schema({
+ ip : String
+})
+
+ClickSchema.plugin(supergoose);
+var Click = mongoose.model('Click', ClickSchema);
+
+describe('findOrCreate', function() {
+ it("should return the object if it exists", sinon.test(function(done) {
+ this.stub(Click, 'findOne').yields(null, {ip: '127.0.0.1'})
+ Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {
+ click.should.eql({ip: '127.0.0.1'})
+ })
+ }))
+
+ it("check to see if the object exists", sinon.test(function() {
+ this.mock(Click).expects('findOne')
+ Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {})
+ }))
+
+ it("should call create if it doesn't exist", sinon.test(function() {
+ this.stub(Click, 'findOne').yields(null, null);
+ this.stub(Click, 'create').withArgs({ip: '127.0.0.1'})
+ Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {})
+ }))
+})
Please sign in to comment.
Something went wrong with that request. Please try again.