Permalink
Browse files

simplified api for storages

  • Loading branch information...
jamuhl committed Feb 14, 2012
1 parent f07cc28 commit 4e3c7fd9bf8db6ba923c4e29971074b4abf5fdc4
View
@@ -1,4 +1,4 @@
-// lib/eventDispatcher.js v0.4.0
+// lib/eventDispatcher.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
@@ -1,4 +1,4 @@
-// lib/eventDispatcherProcess.js v0.4.0
+// lib/eventDispatcherProcess.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
View
@@ -1,4 +1,4 @@
-// lib/eventStore.js v0.4.0
+// lib/eventStore.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
@@ -39,7 +39,7 @@ if (typeof exports !== 'undefined') {
eventStore = root.eventStore = {};
}
-eventStore.VERSION = '0.3.0';
+eventStore.VERSION = '0.5.0';
// Create new instance of the event store.
eventStore.createStore = function(options) {
@@ -62,24 +62,29 @@ Store.prototype = {
// __start:__ will inject missing modules by using default inMemory or fake instances and
// finally start the dispatcher to publish undispatched (not yet published) events.
start: function() {
+
+ // set default usings if not configured
if (!this.logger && this.options.logger === 'console') {
this.use(require('./logger/consoleLogger'));
}
if (!this.storage) {
- require('./storage/inMemory/storage').createStorage(function(err, storage) {
- this.use(storage);
- }.bind(this));
+ this.use(require('./storage/inMemory/storage').createStorage());
}
if (!this.publisher) {
this.use(require('./publisher/fakePublisher').createPublisher());
}
+ // connect storage if not yet connected
+ if (!this.storage.isConnected) {
+ if (typeof this.storage.connect === 'function') this.storage.connect();
+ }
+ // if fork enabled, start event dispatcher as child process...
if (this.storage.filename && this.storage.options) {
- // if fork enabled, start event dispatcher as child process...
+
if (this.logger) {
this.logger.info('Start event dispatcher as child process!');
}
@@ -97,9 +102,10 @@ Store.prototype = {
this.dispatcher.addUndispatchedEvents = function(evts) {
self.dispatcher.send({ action: 'addUndispatchedEvents', payload: JSON.stringify(evts) });
};
- } else {
-
- // else, start event dispatcher in same process...
+ }
+ // else, start event dispatcher in same process...
+ else {
+
if (this.logger) {
this.logger.info('Start event dispatcher in same process!');
}
@@ -289,9 +295,7 @@ Store.prototype = {
callback = revMin;
revMin = 0;
revMax = -1;
- }
-
- if (typeof revMax === 'function') {
+ } else if (typeof revMax === 'function') {
callback = revMax;
revMax = -1;
}
View
@@ -1,4 +1,4 @@
-// lib/interfaces.js v0.4.0
+// lib/interfaces.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
@@ -19,7 +19,7 @@ if (typeof exports !== 'undefined') {
interfaces = root.eventStore.interfaces = {};
}
-interfaces.VERSION = '0.3.0';
+interfaces.VERSION = '0.5.0';
// ## storage interface
interfaces.IStorage = {
@@ -1,4 +1,4 @@
-// lib/logger/consoleLogger.js v0.4.0
+// lib/logger/consoleLogger.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
@@ -1,4 +1,4 @@
-// lib/publisher/fakePublisher.js v0.4.0
+// lib/publisher/fakePublisher.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
@@ -14,7 +14,7 @@ if (typeof exports !== 'undefined') {
fakePublisher = root.fakePublisher = {};
}
-fakePublisher.VERSION = '0.3.0';
+fakePublisher.VERSION = '0.5.0';
// create new publisher instance
fakePublisher.createPublisher = function(options) {
@@ -16,17 +16,18 @@ if (typeof exports !== 'undefined') {
inMemoryStorage = root.inMemoryStorage = {};
}
-inMemoryStorage.VERSION = '0.3.0';
+inMemoryStorage.VERSION = '0.5.0';
// Create new instance of storage.
inMemoryStorage.createStorage = function(options, callback) {
- new Storage(options, callback);
+ return new Storage(options, callback);
};
// ## inMemory storage
Storage = function(options, callback) {
this.filename = __filename;
+ this.isConnected = false;
if (typeof options === 'function')
callback = options;
@@ -36,11 +37,24 @@ Storage = function(options, callback) {
this.store = {};
this.snapshots = {};
- callback(null, this);
+ if (callback) {
+ this.connect(callback);
+ }
};
Storage.prototype = {
+ // __connect:__ connects the underlaying database.
+ //
+ // `storage.connect(callback)`
+ //
+ // - __callback:__ `function(err, storage){}`
+ connect: function(callback) {
+ this.isConnected = true;
+
+ if (callback) callback(null, this);
+ },
+
// __addEvents:__ saves all events.
//
// `storage.addEvents(events, callback)`
View
@@ -1,4 +1,4 @@
-// lib/util.js v0.4.0
+// lib/util.js v0.5.0
// (c) 2012 Kaba AG, MIC AWM; under MIT License
// (by) Jan Muehlemann (jamuhl)
// , Adriano Raiano (adrai)
@@ -17,7 +17,7 @@ if (typeof exports !== 'undefined') {
util = root.eventStore.util = {};
}
-util.VERSION = '0.3.0';
+util.VERSION = '0.5.0';
// Checks if an object implements an interface.
// __Example:__
View
@@ -1,7 +1,7 @@
{
"author": "Kaba MIC AWM"
, "name": "eventstore"
- , "version": "0.4.0"
+ , "version": "0.5.0"
, "contributors": [
{ "name": "Jan Muehlemann", "email": "jan.muehlemann@gmail.com" },
{ "name": "Adriano Raiano", "email": "adriano.raiano@kaba.com" }
@@ -1,7 +1,7 @@
{
"author": "Kaba MIC AWM"
, "name": "eventstore.couchDb"
- , "version": "0.4.0"
+ , "version": "0.5.0"
, "contributors": [
{ "name": "Jan Muehlemann", "email": "jan.muehlemann@gmail.com" },
{ "name": "Adriano Raiano", "email": "adriano.raiano@kaba.com" }
View
@@ -22,18 +22,19 @@ if (typeof exports !== 'undefined') {
couchDbStorage = root.couchDbStorage = {};
}
-couchDbStorage.VERSION = '0.3.0';
+couchDbStorage.VERSION = '0.5.0';
// Create new instance of storage.
couchDbStorage.createStorage = function(options, callback) {
- new Storage(options, callback);
+ return new Storage(options, callback);
};
// ## CouchDb storage
Storage = function(options, callback) {
this.filename = __filename;
+ this.isConnected = false;
if (typeof options === 'function')
callback = options;
@@ -47,60 +48,76 @@ Storage = function(options, callback) {
};
this.options = mergeOptions(options, defaults);
-
- this.store = new(cradle.Connection)();
- this.client = this.store.database(this.options.dbName);
- this.client.exists(function(err, res) {
- if (err) {
- callback(err);
- }
- else if (res === false) {
- this.client.create(function(err) {
- if (err) {
- callback(err);
- }
- else {
- this.client.save('_design/'+this.options.dbName, {
- eventsByStreamId: {
- map: function (evt) {
- if (!evt.snapshotId) {
- emit(evt.streamId, evt);
+
+ if (callback) {
+ this.connect(callback);
+ }
+};
+
+Storage.prototype = {
+
+ // __connect:__ connects the underlaying database.
+ //
+ // `storage.connect(callback)`
+ //
+ // - __callback:__ `function(err, storage){}`
+ connect: function(callback) {
+ var self = this;
+
+ this.store = new(cradle.Connection)();
+ this.client = this.store.database(this.options.dbName);
+ this.client.exists(function(err, res) {
+ if (err) {
+ if (callback) callback(err);
+ }
+ else if (res === false) {
+ self.client.create(function(err) {
+ if (err) {
+ callback(err);
+ }
+ else {
+ self.client.save('_design/' + self.options.dbName, {
+ eventsByStreamId: {
+ map: function (evt) {
+ if (!evt.snapshotId) {
+ emit(evt.streamId, evt);
+ }
}
- }
- },
- allUndispatched: {
- map: function (evt) {
- if (!evt.snapshotId && !evt.dispatched) {
- emit(evt._id, evt);
+ },
+ allUndispatched: {
+ map: function (evt) {
+ if (!evt.snapshotId && !evt.dispatched) {
+ emit(evt._id, evt);
+ }
}
- }
- },
- snapshotsByStreamId: {
- map: function (snapshot) {
- if (snapshot.snapshotId) {
- emit(snapshot.streamId, snapshot);
+ },
+ snapshotsByStreamId: {
+ map: function (snapshot) {
+ if (snapshot.snapshotId) {
+ emit(snapshot.streamId, snapshot);
+ }
}
- }
- },
- allEvents: {
- map: function (evt) {
- if (!evt.snapshotId) {
- emit(evt._id, evt);
+ },
+ allEvents: {
+ map: function (evt) {
+ if (!evt.snapshotId) {
+ emit(evt._id, evt);
+ }
}
}
- }
- });
- callback(null, this);
- }
- }.bind(this));
- }
- else {
- callback(null, this);
- }
- }.bind(this));
-};
-
-Storage.prototype = {
+ });
+
+ self.isConnected = true;
+ if (callback) callback(null, self);
+ }
+ });
+ }
+ else {
+ self.isConnected = true;
+ if (callback) callback(null, self);
+ }
+ });
+ },
// __addEvents:__ saves all events.
//
@@ -358,6 +375,6 @@ var mergeOptions = function(options, defaultOptions) {
var merged = {};
for (var attrname in defaultOptions) { merged[attrname] = defaultOptions[attrname]; }
- for (var attrname in options) { if (options[attrname]) merged[attrname] = options[attrname]; }
+ for (attrname in options) { if (options[attrname]) merged[attrname] = options[attrname]; }
return merged;
};
@@ -1,7 +1,7 @@
{
"author": "Kaba MIC AWM"
, "name": "eventstore.mongoDb"
- , "version": "0.4.0"
+ , "version": "0.5.0"
, "contributors": [
{ "name": "Jan Muehlemann", "email": "jan.muehlemann@gmail.com" },
{ "name": "Adriano Raiano", "email": "adriano.raiano@kaba.com" }
Oops, something went wrong.

0 comments on commit 4e3c7fd

Please sign in to comment.