From 97ec00e958a16af73d476665e0c6bdb4befbcd0c Mon Sep 17 00:00:00 2001 From: Jeffrey Layanto Date: Fri, 24 Jul 2020 23:30:07 +1000 Subject: [PATCH] Added db.isPartitioned(dbName) and extended db.create(dbName, params) to allow creation of partitioned database (#128) --- API.md | 5 +++-- scripts/db.js | 15 +++++++++++---- test/spec/db.js | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index bfc6ba3..15d1fe4 100644 --- a/API.md +++ b/API.md @@ -19,9 +19,10 @@ * [changes(dbName, params, filter)](https://github.com/redgeoff/slouch/blob/master/API.md#changesdbname-params-filter) * [changesArray(dbName, params, filter)](https://github.com/redgeoff/slouch/blob/master/API.md#changesarraydbname-params-filter) * copy(fromDBName, toDBName) - * create(dbName) + * create(dbName, params) * destroy(dbName) * exists(dbName) + * isPartitioned(dbName) * replicate(params) * get(dbName) * view(dbName, viewDocId, view, params) @@ -70,8 +71,8 @@ * system * get() * isCouchDB1() - * supportPartitioned() * reset(exceptDBNames) + * supportPartitioned() * updates(params) * updatesNoHistory(params) * updatesViaGlobalChanges(params) diff --git a/scripts/db.js b/scripts/db.js index 6e7ac47..86f87b6 100644 --- a/scripts/db.js +++ b/scripts/db.js @@ -7,17 +7,18 @@ var DB = function (slouch) { this._slouch = slouch; }; -DB.prototype._create = function (dbName) { +DB.prototype._create = function (dbName, params) { return this._slouch._req({ uri: this._slouch._url + '/' + encodeURIComponent(dbName), - method: 'PUT' + method: 'PUT', + qs: params }); }; -DB.prototype.create = function (dbName) { +DB.prototype.create = function (dbName, params) { var self = this; - return self._create(dbName).catch(function (err) { + return self._create(dbName, params).catch(function (err) { // During heavy traffic, CouchDB does this strange thing where it will return an error even when // the DB has been created. So, we check to see if the DB exists and then only throw the error // if the DB does not exist. @@ -29,6 +30,12 @@ DB.prototype.create = function (dbName) { }); }; +DB.prototype.isPartitioned = function (dbName) { + return this.get(dbName).then(function (obj) { + return obj.props && (obj.props.partitioned === true); + }); +}; + DB.prototype.destroy = function (dbName) { return this._slouch._req({ uri: this._slouch._url + '/' + encodeURIComponent(dbName), diff --git a/test/spec/db.js b/test/spec/db.js index e691d00..4cef0e6 100644 --- a/test/spec/db.js +++ b/test/spec/db.js @@ -99,6 +99,28 @@ describe('db', function () { }); }); + it('should create partitioned database if supported', function () { + return slouch.system.supportPartitioned().then(function (partitioned) { + if (partitioned) { + return db.create('p' + utils.createdDB, { + partitioned: true + }).then(function () { + dbsToDestroy.push('p' + utils.createdDB); + return db.isPartitioned('p' + utils.createdDB).then(function (partitioned) { + partitioned.should.eql(true); + }); + }); + } else { + return db.create('p' + utils.createdDB).then(function () { + dbsToDestroy.push('p' + utils.createdDB); + return db.isPartitioned('p' + utils.createdDB).then(function (partitioned) { + partitioned.should.eql(false); + }); + }); + } + }); + }); + it('should iterate through dbs', function () { var dbNames = [];