Skip to content

Commit

Permalink
Use merge so autoupdate keeps user created design docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
makara committed May 25, 2016
1 parent 55403aa commit a6b5b9f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var util = require('util');
var Connector = require('loopback-connector').Connector;
var httpError = require('http-errors');
var Promise = require('bluebird');
var merge = require('mixable-object').merge;

/*!
* Generate the CouchDB URL from the options
Expand Down Expand Up @@ -391,9 +392,8 @@ CouchDB.prototype.saveDesignDocs = function() {
var options = { docName: _id };
return connection.call('getAsync', _id).then(function(data) {
debug('updating design doc:', name);
return connection.call('insertAsync', Object.assign({}, designDocs[name], {
_rev: data._rev
}), options);
// Using `merge` here, to keep the things created in the other ways.
return connection.call('insertAsync', merge.call(data, designDocs[name]), options);
}, function(err) {
debug('creating design doc:', name);
return connection.call('insertAsync', designDocs[name], options);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"debug": "^2.2.0",
"http-errors": "^1.5.0",
"loopback-connector": "^2.3.0",
"mixable-object": "^0.1.1",
"nano": "^6.2.0"
},
"devDependencies": {
Expand Down
103 changes: 103 additions & 0 deletions test/20.operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ describe('CouchDB operations', function() {
}).catch(done);
});

it('can use the DB', function(done) {
Person.create(persons[1]).then(function(person) {
person.id.should.equal('1');
person.name.should.equal('Mary');
done();
}).catch(done);
});

it('can use the DB', function(done) {
Person.create(persons[3]).then(function(person) {
person.id.should.be.String();
Expand Down Expand Up @@ -111,6 +119,101 @@ describe('CouchDB operations', function() {
}).catch(done);
});

it('can have another view', function(done) {
connector.connect().call('insertAsync', {
_id: '_design/group',
views: {
byAge: {
map: 'function(doc) { if (doc.age) emit(doc.age, null); }'
}
}
}).asCallback(done);
});

it('can use the other view', function(done) {
connector.connect().call('viewAsync', 'group', 'byAge', { keys: [24] }).then(function(res) {
res.should.be.Object();
res.should.have.property('rows').which.is.Array().with.length(2);
done();
}).catch(done);
});

it('can do autoupdate again', function(done) {
ds.autoupdate(function(err, res) {
if (err) {
return done(err);
}
done();
});
});

it('can use the view', function(done) {
connector.connect().call('viewAsync', 'find', 'byName', { keys: ['Charlie'] }).then(function(res) {
res.should.be.Object();
res.should.have.property('rows').which.is.Array().with.length(1);
done();
}).catch(done);
});

it('can still use the other view', function(done) {
connector.connect().call('viewAsync', 'group', 'byAge', { keys: [24] }).then(function(res) {
res.should.be.Object();
res.should.have.property('rows').which.is.Array().with.length(2);
done();
}).catch(done);
});

it('can remove the design docs', function(done) {
var connection = connector.connect();
return connection.call('getAsync', '_design/find').then(function(data) {
return connection.call('destroyAsync', data._id, data._rev);
}).asCallback(done);
});

it('can have another view', function(done) {
connector.connect().call('insertAsync', {
_id: '_design/find',
views: {
byAge: {
map: 'function(doc) { if (doc.age) emit(doc.age, null); }'
}
}
}).asCallback(done);
});

it('can use the other view', function(done) {
connector.connect().call('viewAsync', 'find', 'byAge', { keys: [24] }).then(function(res) {
res.should.be.Object();
res.should.have.property('rows').which.is.Array().with.length(2);
done();
}).catch(done);
});

it('can do autoupdate again', function(done) {
ds.autoupdate(function(err, res) {
if (err) {
return done(err);
}
done();
});
});

it('can use the view', function(done) {
connector.connect().call('viewAsync', 'find', 'byName', { keys: ['Charlie'] }).then(function(res) {
res.should.be.Object();
res.should.have.property('rows').which.is.Array().with.length(1);
done();
}).catch(done);
});

it('can still use the other view', function(done) {
connector.connect().call('viewAsync', 'find', 'byAge', { keys: [24] }).then(function(res) {
res.should.be.Object();
res.should.have.property('rows').which.is.Array().with.length(2);
done();
}).catch(done);
});

it('can do automigrate', function(done) {
ds.automigrate(function(err, res) {
if (err) {
Expand Down

0 comments on commit a6b5b9f

Please sign in to comment.