Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 5a7e4c3

Browse files
authored
Merge pull request #374 from cloudant/372-add-partitioned-database-support
Add partitioned database support.
2 parents 320329e + 8797ce0 commit 5a7e4c3

File tree

8 files changed

+430
-51
lines changed

8 files changed

+430
-51
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# UNRELEASED
2+
- [NEW] Added partitioned database support.
3+
14
# 4.0.0 (2019-03-12)
25
- [NEW] Added option for client to authenticate with IAM token server.
36
- [FIXED] Add `vcapServiceName` configuration option to TS declarations.

cloudant.js

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2015, 2018 IBM Corp. All rights reserved.
1+
// Copyright © 2015, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -25,6 +25,18 @@ const Client = require('./lib/client.js');
2525
// return a URL.
2626
var reconfigure = require('./lib/reconfigure.js');
2727

28+
// Helper function for optional parameter `opts`.
29+
function getCallback(opts, callback) {
30+
if (typeof opts === 'function') {
31+
callback = opts;
32+
opts = {};
33+
}
34+
if (typeof opts === 'undefined') {
35+
opts = {};
36+
}
37+
return {opts, callback};
38+
}
39+
2840
// This IS the Cloudant API. It is mostly nano, with a few functions.
2941
function Cloudant(options, callback) {
3042
debug('Initialize', options);
@@ -150,7 +162,81 @@ function Cloudant(options, callback) {
150162
body: query}, callback);
151163
};
152164

165+
// Partitioned Databases
166+
// ---------------------
167+
168+
function partitionInfo(partitionKey, callback) {
169+
return nano.request(
170+
{db: db, path: '_partition/' + partitionKey}, callback
171+
);
172+
}
173+
174+
function executePartitionedView(partitionKey, ddoc, viewName, meta, qs, callback) {
175+
meta.viewPath = '_partition/' + partitionKey + '/_design/' + ddoc + '/_' +
176+
meta.type + '/' + viewName;
177+
// Note: No need to pass `ddoc` or `viewName` to the `baseView` function
178+
// as they are passed in the `meta.viewPath`.
179+
return nano._use(db).baseView(null, null, meta, qs, callback);
180+
}
181+
182+
function partitionedList(partitionKey, qs0, callback0) {
183+
const {opts, callback} = getCallback(qs0, callback0);
184+
let path = '_partition/' + partitionKey + '/_all_docs';
185+
return nano.request({db: db, path: path, qs: opts}, callback);
186+
}
187+
188+
function partitionedListAsStream(partitionKey, qs) {
189+
let path = '_partition/' + partitionKey + '/_all_docs';
190+
return nano.request(
191+
{db: db, path: path, qs: qs, stream: true}, callback
192+
);
193+
}
194+
195+
function partitionedFind(partitionKey, selector, callback) {
196+
return nano.request({
197+
db: db,
198+
path: '_partition/' + partitionKey + '/_find',
199+
method: 'POST',
200+
body: selector
201+
}, callback);
202+
}
203+
204+
function partitionedFindAsStream(partitionKey, selector) {
205+
return nano.request({
206+
db: db,
207+
path: '_partition/' + partitionKey + '/_find',
208+
method: 'POST',
209+
body: selector,
210+
stream: true
211+
});
212+
}
213+
214+
function partitionedSearch(partitionKey, ddoc, viewName, qs, callback) {
215+
return executePartitionedView(
216+
partitionKey, ddoc, viewName, {type: 'search'}, qs, callback
217+
);
218+
}
219+
220+
function partitionedSearchAsStream(partitionKey, ddoc, viewName, qs) {
221+
return executePartitionedView(
222+
partitionKey, ddoc, viewName, {type: 'search', stream: true}, qs
223+
);
224+
}
225+
226+
function partitionedView(partitionKey, ddoc, viewName, qs, callback) {
227+
return executePartitionedView(
228+
partitionKey, ddoc, viewName, {type: 'view'}, qs, callback
229+
);
230+
}
231+
232+
function partitionedViewAsStream(partitionKey, ddoc, viewName, qs) {
233+
return executePartitionedView(
234+
partitionKey, ddoc, viewName, {type: 'view', stream: true}, qs
235+
);
236+
}
237+
153238
var obj = nano._use(db);
239+
154240
obj.geo = geo;
155241
obj.bulk_get = bulk_get; // eslint-disable-line camelcase
156242
obj.get_security = get_security; // eslint-disable-line camelcase
@@ -159,6 +245,16 @@ function Cloudant(options, callback) {
159245
obj.index.del = index_del; // eslint-disable-line camelcase
160246
obj.find = find;
161247

248+
obj.partitionInfo = partitionInfo;
249+
obj.partitionedFind = partitionedFind;
250+
obj.partitionedFindAsStream = partitionedFindAsStream;
251+
obj.partitionedList = partitionedList;
252+
obj.partitionedListAsStream = partitionedListAsStream;
253+
obj.partitionedSearch = partitionedSearch;
254+
obj.partitionedSearchAsStream = partitionedSearchAsStream;
255+
obj.partitionedView = partitionedView;
256+
obj.partitionedViewAsStream = partitionedViewAsStream;
257+
162258
return obj;
163259
};
164260

package-lock.json

Lines changed: 30 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"concat-stream": "^1.6.0",
2929
"debug": "^3.1.0",
3030
"lockfile": "1.0.3",
31-
"nano": "^8.0.0",
31+
"nano": "^8.1.0",
3232
"request": "^2.81.0",
3333
"tmp": "0.0.33"
3434
},

test/legacy/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2015, 2018 IBM Corp. All rights reserved.
1+
// Copyright © 2015, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -813,7 +813,7 @@ describe('#db Cloudant Search', function() {
813813

814814
it('searches test data: author:charles', function(done) {
815815
var mocks = nock(SERVER)
816-
.get('/' + dbName + '/_design/library/_search/books?q=author%3Acharles')
816+
.post('/' + dbName + '/_design/library/_search/books', '{\"q\":\"author:charles\"}')
817817
.reply(200, { total_rows: 1, bookmark: 'g2wAAAABaANkAB1kYmNvcmVAZGI2LnNsaW5nLmNsb3VkYW50Lm5ldGwAAAACbgQAAAAAgG4EAP___79qaAJGP8iMWIAAAABhAGo', rows: [ { id: 'a_tale', order: [Object], fields: {} } ] });
818818

819819
mydb.search('library', 'books', {q: 'author:charles'}, function(er, d) {
@@ -833,7 +833,7 @@ describe('#db Cloudant Search', function() {
833833

834834
it('searches test data: title:two', function(done) {
835835
var mocks = nock(SERVER)
836-
.get('/' + dbName + '/_design/library/_search/books?q=title%3Atwo')
836+
.post('/' + dbName + '/_design/library/_search/books', '{\"q\":\"title:two\"}')
837837
.reply(200, {total_rows: 2, bookmark: 'g1AAAACIeJzLYWBgYMpgTmGQTUlKzi9KdUhJMtcrzsnMS9dLzskvTUnMK9HLSy3JASlLcgCSSfX____PymBysz_RE9EAFEhkIFJ7HguQZGgAUkAT9oONOLy4igFsRBYAPRQqlQ', rows: [{id: 'towers', order: [Object], fields: {}}, {id: 'a_tale', order: [Object], fields: {}}]});
838838

839839
mydb.search('library', 'books', {q: 'title:two'}, function(er, d) {

test/legacy/readme-examples.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2015, 2018 IBM Corp. All rights reserved.
1+
// Copyright © 2015, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -406,7 +406,7 @@ describe('Cloudant Search #db', function() {
406406
{ id: '764de7aeca95c27fdd7fb6565dcfc727', rev: '1-86039ff130d36c08a71b3f293fd4ea7e' }])
407407
.post(`/${my_db}`)
408408
.reply(200, { ok: true, id: '_design/library', rev: '1-cdbb57f890d060055b7fb8cb07628068' })
409-
.get(`/${my_db}/_design/library/_search/books`).query(true)
409+
.post(`/${my_db}/_design/library/_search/books`, '{\"q\":\"author:dickens\"}')
410410
.reply(200, {total_rows: 2, rows: [{id: '764de7aeca95c27fdd7fb6565dcfc0fe'}, {id: '764de7aeca95c27fdd7fb6565dcfc727'}]})
411411
.delete(`/${my_db}`)
412412
.reply(200, {ok: true});

0 commit comments

Comments
 (0)