Skip to content

Commit

Permalink
Fix .save method
Browse files Browse the repository at this point in the history
  • Loading branch information
deedubs committed Sep 14, 2012
1 parent 60b3f2e commit ef54e67
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@
TESTS=test/*.js
MOCHA=./node_modules/.bin/mocha

test:
@$(MOCHA) $(TESTS)

.PHONY: test
12 changes: 12 additions & 0 deletions index.js
Expand Up @@ -27,6 +27,18 @@ Robe.prototype.insert = function(attrs, callback) {
this.collection.insert(attrs, callback);
}

Robe.prototype.save = function (attrs, callback) {
if(attrs._id) {
this.collection.updateById(attrs._id, attrs, callback);
} else {
this.collection.insert(attrs, callback);
}
}

Robe.prototype.remove = function (object, callback) {
this.collection.remove({_id: object._id}, callback);
}

Robe.prototype.byId = function (id, callback) {
if (!id) {
return callback(new Error('Id must be supplied'));
Expand Down
44 changes: 44 additions & 0 deletions lib/index.js
@@ -0,0 +1,44 @@
var debug = require('debug')('robe')
, monkManager = require('monk')
, monk;

function Robe (collectionName) {
this.collection = monk.get(collectionName);
}

Robe.prototype.all = function (options, callback) {
if(typeof(options) === 'function') {
callback = options;
options = {};
}

this.collection.find({}, callback);
}

Robe.prototype.find = function(query, callback) {
this.collection.find(query, callback);
}

Robe.prototype.findOne = function(query, callback) {
this.collection.findOne(query, callback);
}

Robe.prototype.insert = function(attrs, callback) {
this.collection.insert(attrs, callback);
}

Robe.prototype.save = Robe.prototype.insert;

Robe.prototype.byId = function (id, callback) {
if (!id) {
return callback(new Error('Id must be supplied'));
}

this.collection.findById(id, callback);
}

module.exports = function (mongoUrlString) {
monk = monkManager(mongoUrlString);

return Robe;
}
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -2,10 +2,11 @@
"author": "Dan Williams <me@deedubs.com> (http://deedubs.com/)",
"name": "robe",
"description": "A thin wrapper for monk",
"version": "0.0.1",
"version": "0.0.5",
"main": "index.js",
"dependencies": {
"debug": "~0.7.0"
"debug": "~0.7.0",
"monk": "~0.2.1"
},
"devDependencies": {},
"optionalDependencies": {},
Expand Down
5 changes: 5 additions & 0 deletions test/common.js
@@ -0,0 +1,5 @@
expect = require('expect.js');

config = {
db: 'mongo://localhost/robe_test'
};
4 changes: 4 additions & 0 deletions test/mocha.opts
@@ -0,0 +1,4 @@
--require test/common.js
--growl
--ui bdd
--reporter spec
33 changes: 33 additions & 0 deletions test/robe.js
@@ -0,0 +1,33 @@
var Robe = require('../')(config.db);

describe('Robe', function() {
describe('Factory', function () {
before(function () {
this.robe = new Robe('TestModel');
});

it('should have a .all function', function() {
expect(this.robe.all).to.be.a('function');
});

it('should have a .find function', function() {
expect(this.robe.find).to.be.a('function');
});

it('should have a .findOne function', function() {
expect(this.robe.findOne).to.be.a('function');
});

it('should have a .insert function', function() {
expect(this.robe.insert).to.be.a('function');
});

it('should have a .byId function', function() {
expect(this.robe.byId).to.be.a('function');
});

it('should have a .save function', function() {
expect(this.robe.save).to.be.a('function');
});
});
});

0 comments on commit ef54e67

Please sign in to comment.