Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update setup methods, improve view snapshot ids, fix global variables…

… issue
  • Loading branch information...
commit deb3ad39a683c4a7b83d85cf84feb8875bd6bffe 1 parent be23726
@petrjanda authored
View
17 lib/repository/couchRepository.js
@@ -171,16 +171,11 @@ CouchRepository.prototype.setup = function() {
}
}
- this.request({
- method: 'PUT',
- path: '/' + this.database + '/_design/cqrs',
- data: JSON.stringify({
- language: "javascript",
- views: {
+ this.createDocument('_design/cqrs', {
+ language: "javascript",
+ views: {
name: { map: mapByName.toString() },
- aggregate: { map: mapByAggregate.toString() },
- viewSnapshot: {map: mapViewSnapshot.toString() }
- }
- })
- });
+ aggregate: { map: mapByAggregate.toString() }
+ }
+ }, function(a, b) { console.log(a); console.log(b)})
}
View
21 lib/storage/couchStorage.js
@@ -35,12 +35,14 @@ utils.inherits(CouchStorage, CouchDb);
CouchStorage.prototype.storeView = function(view) {
var time = utils.uuid();
- this.createDocument(time, {
+ this.createDocument(view.uid.toLowerCase() + '-' + time, {
viewId: view.uid,
type: 'view',
lastEvent: view.lastEvent,
time: time,
data: view.data
+ }, function(err, data) {
+ console.log(err)
});
}
@@ -94,4 +96,21 @@ CouchStorage.prototype._loadBareView = function(id, callback) {
callback(data);
});
+}
+
+// EXPERIMENTAL
+CouchStorage.prototype.setup = function() {
+
+ var mapViewSnapshot = function(doc) {
+ if(doc.viewId) {
+ emit([doc.viewId, doc.time], doc);
+ }
+ }
+
+ this.createDocument('_design/cqrs', {
+ language: "javascript",
+ views: {
+ viewSnapshot: {map: mapViewSnapshot.toString() }
+ }
+ }, function(a, b) { console.log(a); console.log(b)})
}
View
3  lib/view.js
@@ -60,7 +60,7 @@ View.prototype.load = function(reload, callback) {
reload = false
}
- start = new Date().getTime(),
+ var start = new Date().getTime(),
loadEvents = function() {
repo.getEventsByName(self.eventNames, self.lastEvent + 1, function(events) {
@@ -93,7 +93,6 @@ View.prototype.load = function(reload, callback) {
}
storage.loadView(this.uid, function(data) {
-
// Apply data, loaded from snapshot.
if(data) {
self.data = data.data;
View
24 spec/storage/couchStorageSpec.js
@@ -37,7 +37,9 @@ describe('CouchStorage', function() {
}
function viewPath(id) {
- return '/cqrs/_design/cqrs/_view/viewSnapshot?startkey=["' + id + '","999999999999999999"]&endkey=["' + id + '","0"]&limit=1&descending=true'
+ return '/cqrs/_design/cqrs/_view/viewSnapshot?startkey=["' +
+ id + '","999999999999999999"]&endkey=["' +
+ id + '","0"]&limit=1&descending=true';
}
beforeEach(function() {
@@ -57,13 +59,23 @@ describe('CouchStorage', function() {
describe('.storeView', function() {
it('should call createDocument', function() {
- var view = {uid: 1, lastEvent: 12, data: 'foo'};
+ var view = {uid: 'name', lastEvent: 12, data: 'foo'};
spyOn(couchStorage, 'createDocument');
spyOn(utils, 'uuid').andReturn(123456);
couchStorage.storeView(view);
- expect(couchStorage.createDocument).toHaveBeenCalledWith(123456, {viewId:1,type:"view",lastEvent:12,time:123456,data:"foo"});
+ expect(couchStorage.createDocument).toHaveBeenCalledWith(
+ 'name-123456',
+ {
+ viewId: 'name',
+ type: "view",
+ lastEvent: 12,
+ time: 123456,
+ data: "foo"
+ },
+ jasmine.any(Function)
+ );
})
})
@@ -74,7 +86,11 @@ describe('CouchStorage', function() {
couchStorage.purgeView('1j0ddsesdfsfdo0m7');
- expect(couchStorage.deleteDocument).toHaveBeenCalledWith('e9f59b5f8c965ebce700eeec1baf7a60', '1-0145acc24d7c96db4d8ef260ad4afec4');
+ expect(couchStorage.deleteDocument).toHaveBeenCalledWith(
+ 'e9f59b5f8c965ebce700eeec1baf7a60',
+ '1-0145acc24d7c96db4d8ef260ad4afec4'
+ );
+
expect(couchStorage.request).toHaveBeenCalledWith({
method : 'GET',
path : viewPath('1j0ddsesdfsfdo0m7') },
Please sign in to comment.
Something went wrong with that request. Please try again.