Skip to content

Commit

Permalink
conditional callback for request method, stringify event to json
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Janda committed Dec 31, 2011
1 parent 965be15 commit 3348a4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 1 addition & 3 deletions examples/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ var CouchDb = require('../lib/couchdb');

var couchdb = new CouchDb();

couchdb.getUuid(function(data) {
console.log(data);
});
couchdb.storeEvent(1, 'account:opened', {number: '35-439598'});
26 changes: 22 additions & 4 deletions lib/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ module.exports = CouchDb = function(database) {
//

CouchDb.prototype.storeEvent = function(aggregateId, name, attrs) {
this.createDocument({
this.createDocument(JSON.stringify({
aggregateId: aggregateId,
name: name,
attrs: attrs
});
}));
}

CouchDb.prototype.getEventsByAggregate = function(aggregateId, callback) {

}

CouchDb.prototype.getEventsByType = function(type, callback) {

}


Expand All @@ -41,6 +49,14 @@ CouchDb.prototype.getUuid = function(callback) {
});
}

/*
* Documents are CouchDB’s central data structure. The idea behind a document
* is, unsurprisingly, that of a real-world document—a sheet of paper such as
* an invoice, a recipe, or a business card.
*
* @param {String} Document content.
* @param {Function} Function to be called when document is created.
*/
CouchDb.prototype.createDocument = function(data, callback) {
var self = this;

Expand Down Expand Up @@ -70,8 +86,10 @@ CouchDb.prototype.request = function(options, callback) {
if(chunk) {
buffer += chunk;
}

callback(buffer);

if(callback) {
callback(buffer);
}
})
});

Expand Down
10 changes: 8 additions & 2 deletions spec/couchdbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ describe('couchdb', function() {

couchdb.storeEvent(1, 'user:created', {foo: 'bar'});

expect(couchdb.createDocument).toHaveBeenCalledWith({
expect(couchdb.createDocument).toHaveBeenCalledWith(JSON.stringify({
aggregateId: 1,
name: 'user:created',
attrs: {foo: 'bar'}
});
}));
})
})

Expand Down Expand Up @@ -176,6 +176,12 @@ describe('couchdb', function() {
expect(foo.callback).toHaveBeenCalledWith('foo');
})

it('dont require callback to be specified', function() {
couchdb.request({});

res.emit('end', 'foo');
})

it('should call store data into buffer', function() {
var foo = { callback: function() {} };
spyOn(foo, 'callback');
Expand Down

0 comments on commit 3348a4a

Please sign in to comment.