Skip to content

Commit

Permalink
refactor couch storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Janda committed Apr 27, 2012
1 parent 0a8f153 commit 9a9bf8a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/repository/couchRepository.js
Expand Up @@ -16,7 +16,7 @@ exports.createRepository = function(options) {
return instance;
}

CouchRepository = function(options) {
var CouchRepository = function(options) {
CouchDb.call(this, options);
}

Expand Down
15 changes: 12 additions & 3 deletions lib/storage/couchStorage.js
@@ -1,19 +1,28 @@
var utils = require('../utils'),
CouchDb = require('../util/couchdb');

var instance = null;

exports.createStorage = function(options) {
if(!instance) {
instance = new CouchStorage(options);
}

return instance;
}

/*
* Storage is the system component, used to persist view layer snapshots. Main
* motivation, is to lower the load for event storage and speed up the view
* generate process, when client request comes. The storage is separate system
* component and can be even implemented on different persistance system from
* event storage.
*/
module.exports = CouchStorage = function() {
CouchDb.call(this, 'cqrs');
var CouchStorage = function(options) {
CouchDb.call(this, options);
}

utils.inherits(CouchStorage, CouchDb);
utils.singleton(CouchStorage);

/*
* Method called to persist current view state, to the database snapshot.
Expand Down
2 changes: 1 addition & 1 deletion lib/view.js
@@ -1,5 +1,5 @@
var repo = require('./repository').getInstance(),
storage = require('./storage/couchStorage').getInstance();
storage = require('./storage/couchStorage').createStorage();

/*
* View is the ultimate way, how to look on your data, wider then the scope
Expand Down
6 changes: 3 additions & 3 deletions spec/storage/couchStorageSpec.js
Expand Up @@ -41,17 +41,17 @@ describe('CouchStorage', function() {
}

beforeEach(function() {
couchStorage = new CouchStorage();
couchStorage = CouchStorage.createStorage();
})

describe('instance', function() {
it('should get instance of CouchStorage', function() {
var couchStorage = CouchStorage.getInstance()
var couchStorage = CouchStorage.createStorage()
expect(typeof couchStorage.request).toEqual('function');
})

it('should return just one instance', function() {
expect(CouchStorage.getInstance()).toEqual(CouchStorage.getInstance())
expect(CouchStorage.createStorage()).toEqual(CouchStorage.createStorage())
})
})

Expand Down
4 changes: 2 additions & 2 deletions spec/viewSpec.js
@@ -1,7 +1,7 @@
var util = require('util'),
couchdb = require('../lib/repository/couchRepository').createRepository({database: 'foo'}),
couchdb = require('../lib/repository/couchRepository').createRepository(),
repository = require('../lib/repository').getInstance(),
storage = require('../lib/storage/couchStorage').getInstance(),
storage = require('../lib/storage/couchStorage').createStorage(),
View = require('../lib/view');

describe('View', function() {
Expand Down

0 comments on commit 9a9bf8a

Please sign in to comment.