Permalink
Browse files

refactoring database storages wip

  • Loading branch information...
petrjanda committed Apr 27, 2012
1 parent 9bdf7e4 commit 0a8f153cd9e6e31d754e549c8fd66fad96ad5d4e
@@ -3,18 +3,24 @@ var http = require('http'),
CouchDb = require('../util/couchdb'),
instance;
-module.exports = CouchRepository = function() {
- this.database = 'cqrs';
- this.options = {
- host: 'localhost',
- port: 5984
- };
-
- CouchDb.call(this, this.database, this.options);
+var instance = null;
+
+/*
+ * The only public action responsible to create single
+ */
+exports.createRepository = function(options) {
+ if(!instance) {
+ instance = new CouchRepository(options);
+ }
+
+ return instance;
+}
+
+CouchRepository = function(options) {
+ CouchDb.call(this, options);
}
utils.inherits(CouchRepository, CouchDb);
-utils.singleton(CouchRepository);
//
// EVENT SOURCING METHODS
View
@@ -1,16 +1,16 @@
var http = require('http'),
nano = require('nano');
-module.exports = CouchDb = function(database, options) {
+module.exports = CouchDb = function(options) {
options = options || {};
- this.database = database || 'cqrs';
- this.options = {
- host: options.host || 'localhost',
- port: options.port || 5984
- };
+ this.database = options.database || 'cqrs';
- this._db = nano('http://' + this.options.host + ':' + this.options.port + '/' + database);
+ this.host = options.host || 'localhost';
+
+ this.port = options.port || 5984;
+
+ this._db = nano('http://' + this.host + ':' + this.port + '/' + this.database);
}
/*
@@ -46,14 +46,15 @@ CouchDb.prototype._documentPath = function(id) {
CouchDb.prototype.request = function(options, callback) {
options = options || {};
- var params = this.options,
+ var params = { host: this.host, port: this.port},
buffer = '';
+
params.method = options.method || 'GET';
params.path = options.path || '/';
- if(params.user && params.password) {
+ if(this.user && this.password) {
params.headers = {};
- params.headers['Authorization'] = "Basic " + new Buffer(params.user + ":" + params.password).toString('base64');
+ params.headers['Authorization'] = "Basic " + new Buffer(this.user + ":" + this.password).toString('base64');
}
var req = http.request(params, function(res) {
View
@@ -1,5 +1,5 @@
var util = require('util'),
- couchdb = require('../lib/repository/couchRepository').getInstance(),
+ couchdb = require('../lib/repository/couchRepository').createRepository(),
repository = require('../lib/repository').getInstance(),
Aggregate = require('../lib/aggregate');
@@ -8,22 +8,21 @@ describe('CouchRepository', function() {
var couchdb;
beforeEach(function() {
- couchdb = new CouchDb('cqrs');
+ couchdb = CouchDb.createRepository({ database: 'cqrs' });
})
- describe('instance', function() {
+ describe('.createRepository', function() {
it('should get instance of couchdb', function() {
- var couchdb = CouchDb.getInstance()
+ var couchdb = CouchDb.createRepository({ database: 'cqrs' });
+
expect(typeof couchdb._db).toEqual('object');
})
it('should return just one instance', function() {
- var couch1 = CouchDb.getInstance(),
- couch2 = CouchDb.getInstance();
-
- couch1.database = 'foobar';
+ var couch1 = CouchDb.createRepository({ database: 'cqrs' });
+ couch2 = CouchDb.createRepository({ database: 'cqrs' });
- expect(couch2.database).toEqual('foobar')
+ expect(couch1).toEqual(couch2);
})
})
@@ -34,11 +33,11 @@ describe('CouchRepository', function() {
})
it('host should be default to localhost', function() {
- expect(couchdb.options.host).toEqual('localhost');
+ expect(couchdb.host).toEqual('localhost');
})
it('port should be default to localhost', function() {
- expect(couchdb.options.port).toEqual(5984);
+ expect(couchdb.port).toEqual(5984);
})
})
View
@@ -8,7 +8,7 @@ describe('couchdb', function() {
var couchdb;
beforeEach(function() {
- couchdb = new CouchDb('cqrs');
+ couchdb = new CouchDb({database: 'cqrs'});
})
describe('constructor', function() {
@@ -63,8 +63,8 @@ describe('couchdb', function() {
it('should call http.request with auth header if auth params are given', function() {
var got;
- couchdb.options.user = 'foo';
- couchdb.options.password = 'bar';
+ couchdb.user = 'foo';
+ couchdb.password = 'bar';
http.request.andCallFake(function(options, callback) {
got = options.headers;
@@ -81,31 +81,48 @@ describe('couchdb', function() {
it('should call http.request with valid params', function() {
couchdb.request();
- expect(http.request).toHaveBeenCalledWith(couchdb.options, jasmine.any(Function));
+ expect(http.request).toHaveBeenCalledWith({
+ host: 'localhost',
+ port: 5984,
+ method: 'GET', path : '/'
+ }, jasmine.any(Function));
})
it('should setup proper default options', function() {
couchdb.options = {};
couchdb.request();
- expect(http.request).toHaveBeenCalledWith({method: 'GET', path: '/'}, jasmine.any(Function));
+ expect(http.request).toHaveBeenCalledWith({
+ host: 'localhost',
+ port: 5984,
+ method: 'GET',
+ path: '/'
+ }, jasmine.any(Function));
})
it('should call post', function() {
couchdb.options = {};
couchdb.request({method: 'POST'});
- expect(http.request).toHaveBeenCalledWith({method: 'POST', path: '/'}, jasmine.any(Function));
+ expect(http.request).toHaveBeenCalledWith({
+ host: 'localhost',
+ port: 5984,
+ method: 'POST',
+ path: '/' }, jasmine.any(Function));
})
it('should call proper url', function() {
couchdb.options = {};
couchdb.request({path: '/foo'});
- expect(http.request).toHaveBeenCalledWith({method: 'GET', path: '/foo'}, jasmine.any(Function));
+ expect(http.request).toHaveBeenCalledWith({
+ host: 'localhost',
+ port: 5984,
+ method: 'GET',
+ path: '/foo' }, jasmine.any(Function));
})
it('should end request', function() {
View
@@ -1,5 +1,5 @@
var util = require('util'),
- couchdb = require('../lib/repository/couchRepository').getInstance(),
+ couchdb = require('../lib/repository/couchRepository').createRepository({database: 'foo'}),
repository = require('../lib/repository').getInstance(),
storage = require('../lib/storage/couchStorage').getInstance(),
View = require('../lib/view');

0 comments on commit 0a8f153

Please sign in to comment.