Permalink
Browse files

Initial Refactor

  • Loading branch information...
1 parent 118b611 commit 29812a2c8fc6396c308f36b3144aed9076f3a85f @drudge committed Nov 13, 2012
Showing with 85 additions and 142 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 .travis.yml
  3. +3 −2 LICENSE
  4. +31 −47 README.md
  5. +17 −17 index.js
  6. +6 −10 package.json
  7. +22 −66 test/index_test.js
View
@@ -1 +1,2 @@
+.DS_Store
node_modules
View
@@ -0,0 +1,5 @@
+language: "node_js"
+node_js:
+ - 0.4
+ - 0.6
+ - 0.8
View
@@ -1,4 +1,5 @@
-copyright (c) 2012 Jamplify
+copyright (c) 2012 Nicholas Penree <nick@penree.com>
+Original work: 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
@@ -16,4 +17,4 @@ 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.
+THE SOFTWARE.
View
@@ -1,35 +1,19 @@
-supergoose
-==================
+Mongoose findOrCreate Plugin
+============================
-[Mongoose](https://github.com/LearnBoost/mongoose) simple plugin adding some
-handy functions.
+[Mongoose](https://github.com/LearnBoost/mongoose) simple plugin adding a
+findOrCreate method to models.
-```javasript
-/* Adds find or create functionality to mongoose models. This is handy
- * for libraries like passport.js which require it
- */
-Model.findOrCreate()
+## Installation
-/* Parses the complex validation errors return from mongoose into a simple
- * array of messages to be displayed as flash messages or something similar
- */
-Model.errors()
-```
-
-Installation
-------------
-
-`npm install supergoose`
+`npm install mongoose-findorcreate`
-Usage
------
-
-# findOrCreate
+## Usage
```javascript
-var supergoose = require('supergoose')
+var findOrCreate = require('mongoose-findorcreate')
var ClickSchema = new Schema({ ... });
-Click.plugin(supergoose);
+ClickSchema.plugin(findOrCreate);
var Click = mongoose.model('Click', ClickSchema);
```
@@ -56,28 +40,28 @@ Click.create({ip: '127.0.0.1'}, {browser: 'Mozilla'}, function(err, val) {
});
```
-# errors
-```javascript
-var supergoose = require('supergoose')
-var ClickSchema = new Schema({ip: {type: String, required: true}});
-Click.plugin(supergoose, {messages: {'required': '%s is a required field'}});
-var Click = mongoose.model('Click', ClickSchema);
-```
+## License
-The Click model now has an errors static method
+(The MIT License)
-```javascript
-Click.create({}, function(err, click) {
- if(err) {
- Click.errors(err, function(messages) {
- console.log(messages);
- // outputs ['ip is a required field']
- })
- }
-});
-```
-
-License
--------
+Copyright (c) 2012 Nicholas Penree &lt;nick@penree.com&gt;
+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.
-MIT License
+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.
View
@@ -1,17 +1,17 @@
-var _ = require('underscore')
-_.str = require('underscore.string');
-_.mixin(_.str.exports());
-
-module.exports = exports = function superGoosePlugin(schema, options) {
- if(options) var messages = options.messages
+/*!
+ * Mongoose findOrCreate Plugin
+ * Copyright(c) 2012 Nicholas Penree <nick@penree.com>
+ * MIT Licensed
+ */
+function findOrCreatePlugin(schema, options) {
schema.statics.findOrCreate = function findOrCreate(conditions, doc, options, callback) {
if (arguments.length < 4) {
- if (_.isFunction(options)) {
+ if (typeof options === 'function') {
// Scenario: findOrCreate(conditions, doc, callback)
callback = options;
options = {};
- } else if (_.isFunction(doc)) {
+ } else if (typeof doc === 'function') {
// Scenario: findOrCreate(conditions, callback);
callback = doc;
doc = {};
@@ -29,20 +29,20 @@ module.exports = exports = function superGoosePlugin(schema, options) {
callback(err, result)
}
} else {
- _.extend(conditions, doc)
+ for (var key in conditions) {
+ doc[key] = conditions[key];
+ }
var obj = new self(conditions)
obj.save(function(err) {
callback(err, obj)
});
}
})
}
-
- schema.statics.errors = function errors(errors, callback) {
- errors = _.toArray(errors.errors)
- errors = _.map(errors, function(error) {
- return _.sprintf(messages[error.type], error.path)
- })
- callback(errors)
- }
}
+
+/**
+ * Expose `findOrCreatePlugin`.
+ */
+
+module.exports = findOrCreatePlugin;
View
@@ -1,21 +1,17 @@
{
- "name": "supergoose"
- , "description": "Mongoose plugin for simple addons like findOrCreate"
- , "version": "0.0.8"
+ "name": "mongoose-findorcreate"
+ , "description": "Mongoose plugin that adds findOrCreate"
+ , "version": "0.1.0"
, "author": "Matt Roman <romanmt@gmail.com>"
- , "keywords": ["supergoose", "mongodb", "mongoose", "plugin", "findOrCreate"]
+ , "keywords": ["mongodb", "mongoose", "plugin", "findOrCreate"]
, "repository": {
"type": "git"
- , "url": "git://github.com/jamplify/supergoose"
+ , "url": "git://github.com/drudge/mongoose-findorcreate"
}
- , "dependencies": {
- "mongoose": ">= 2.6.5"
- , "underscore": ">= 1.3.3"
- , "underscore.string": ">= 2.2.0rc"
- }
, "devDependencies": {
"should": ">= 0.2.1"
, "mocha": ">= 0.13.0"
+ , "mongoose": ">= 3.0"
}
, "scripts": {
"test": "mocha -u bdd -R spec -c ./test/*test.js"
View
@@ -1,34 +1,35 @@
-var mocha = require('mocha')
- , should = require('should')
- , mongoose = require('mongoose')
- , Schema = mongoose.Schema
- , supergoose = require('../index.js')
-mongoose.connect('mongodb://localhost:27017/supergoose')
+/**
+ * @list dependencies
+ **/
+
+var mocha = require('mocha');
+var should = require('should');
+var mongoose = require('mongoose');
+var Schema = mongoose.Schema;
+var findOrCreate = require('../');
+
+mongoose.connect('mongodb://localhost/findOrCreate')
mongoose.connection.on('error', function (err) {
console.error('MongoDB error: ' + err.message);
console.error('Make sure a mongoDB server is running and accessible by this application')
});
var ClickSchema = new Schema({
- ip : {type: String, required: true}
- , browser: String
+ ip : {type: String, required: true}
})
-var messages = {
- 'required': "%s is required"
-}
+ClickSchema.plugin(findOrCreate);
-ClickSchema.plugin(supergoose, {messages: messages});
var Click = mongoose.model('Click', ClickSchema);
-afterEach(function(done) {
- Click.collection.remove()
+after(function(done) {
+ mongoose.connection.db.dropDatabase()
done();
})
describe('findOrCreate', function() {
- it("should create the obeject if it doesn't exist", function(done) {
+ it("should create the object if it doesn't exist", function(done) {
Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {
click.ip.should.eql('127.0.0.1')
Click.count({}, function(err, num) {
@@ -39,58 +40,13 @@ describe('findOrCreate', function() {
})
it("returns the object if it already exists", function(done) {
- 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();
- })
- })
- })
- })
-
- describe('with upsert', function() {
- it("updates the existing object", function(done) {
- Click.create({ip: '127.0.0.1', browser: 'Chrome'}, function(err, val) {
- Click.findOrCreate({ip: '127.0.0.1'}, {browser: 'IE'}, {upsert: true}, function(err, click) {
- click.should.have.property('browser', 'IE')
- Click.count({}, function(err, num) {
- num.should.equal(1)
- done();
- })
- })
- })
- })
- })
-})
-
-describe('errorMessages', function() {
- it("creates an array of of custom errors", function(done) {
- Click.create({}, function(err, val) {
- Click.errors(err, function(messages) {
- messages.should.eql(['ip is required'])
+ 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)
done();
})
})
})
-})
+})

0 comments on commit 29812a2

Please sign in to comment.