Permalink
Browse files

Implemented doOnCollection and added some tests.

  • Loading branch information...
1 parent 871ca39 commit 4dd010d219fb3d79acb7e27dc00898efb5505d78 @dmcquay committed Nov 28, 2010
Showing with 55 additions and 1 deletion.
  1. +21 −0 lib/DataProvider.js
  2. +34 −1 test/DataProvider.js
View
@@ -27,6 +27,27 @@ DataProvider.getInstance = function(connName) {
return DataProvider._instances[connName];
};
+DataProvider.prototype.doOnCollection = function(collectionName, funcName, args, callback) {
+ var db = new mongodb.Db(
+ this.cfg.dbname,
+ new mongodb.Server(this.cfg.host, this.cfg.port, {auto_reconnect:true}),
+ {strict:false}
+ );
+ db.addListener('error', callback);
+ db.open(function(err, db) {
+ if (err) { return callback(err) }
+ db.createCollection(collectionName, function(err, collection) {
+ if (err) { return callback(err) }
+ var internalCallback = function() {
+ db.close();
+ callback.apply(this, arguments);
+ };
+ args.push(internalCallback);
+ collection[funcName].apply(collection, args);
+ });
+ });
+};
+
DataProvider.prototype.save = function(persistable, callback) {
var db = new mongodb.Db(
this.cfg.dbname,
View
@@ -1,4 +1,5 @@
var DataProvider = require('../lib/DataProvider').DataProvider,
+ Persistable = require('../lib/Persistable').Persistable,
vows = require('vows'),
assert = require('assert'),
sys = require('sys');
@@ -9,6 +10,16 @@ var mongoConfig = {
dbname: 'test'
};
+//Implement a Persistable subclass
+var TestModel = function(raw) {
+ raw = raw || {};
+ Persistable.call(this);
+ this.raw = raw;
+};
+sys.inherits(TestModel, Persistable);
+TestModel.prototype.getCollectionName = function() { return 'test' };
+TestModel.prototype.toObject = function() { return this.raw };
+
vows.describe('DataProvider').addBatch({
'DataProvider': {
'should have an initCfg method': function() {
@@ -51,6 +62,28 @@ vows.describe('DataProvider').addBatch({
'must be an instance of DataProvider': function(dp) {
assert.ok(dp instanceof DataProvider);
- }
+ },
+
+ 'should save': {
+ topic: function(dp) {
+ var model = new TestModel({id:1,foo:'bar'});
+ dp.save(model, this.callback);
+ },
+
+ 'test': function(err, model) {
+ assert.ok(!err);
+ }
+ },
+
+ 'should save using doOnCollection': {
+ topic: function(dp) {
+ var model = new TestModel({id:1,foo:'bar'});
+ dp.doOnCollection('test', 'insert', [{id:2,foo:'bar2'}], this.callback);
+ },
+
+ 'test': function(err, docs) {
+ assert.ok(!err);
+ }
+ }
}
}).export(module);

0 comments on commit 4dd010d

Please sign in to comment.