From a6b5b9ffe5de0d5184254b888a9dbca4941442b0 Mon Sep 17 00:00:00 2001 From: Makara Wang Date: Wed, 25 May 2016 17:26:16 +0800 Subject: [PATCH] Use `merge` so `autoupdate` keeps user created design docs. --- lib/couchdb.js | 6 +-- package.json | 1 + test/20.operation.test.js | 103 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/lib/couchdb.js b/lib/couchdb.js index 642f5ce..471faa1 100644 --- a/lib/couchdb.js +++ b/lib/couchdb.js @@ -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 @@ -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); diff --git a/package.json b/package.json index 2a03cb7..bc434fa 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/20.operation.test.js b/test/20.operation.test.js index b5edea8..0684ba5 100644 --- a/test/20.operation.test.js +++ b/test/20.operation.test.js @@ -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(); @@ -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) {