You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2018/02/23 15:33:08 UTC

[GitHub] janl closed pull request #72: Add couchdb 2.* index creation support

janl closed pull request #72: Add couchdb 2.* index creation support
URL: https://github.com/apache/couchdb-nano/pull/72
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/README.md b/README.md
index b5fd8fc..d440d1e 100644
--- a/README.md
+++ b/README.md
@@ -33,9 +33,9 @@ or save `nano` as a dependency of your project with
   - [nano.db.list([callback])](#nanodblistcallback)
   - [nano.db.compact(name, [designname], [callback])](#nanodbcompactname-designname-callback)
   - [nano.db.replicate(source, target, [opts], [callback])](#nanodbreplicatesource-target-opts-callback)
-  - [nano.db.replication.enable(source, target, [opts], [callback])](#nanodbreplicatorenablesource-target-opts-callback)
-  - [nano.db.replication.query(id, [opts], [callback])](#nanodbreplicatorquery-id-opts-callback)
-  - [nano.db.replication.disable(id, [opts], [callback])](#nanodbreplicatordisable-id-opts-callback)
+  - [nano.db.replication.enable(source, target, [opts], [callback])](#nanodbreplicationenablesource-target-opts-callback)
+  - [nano.db.replication.query(id, [opts], [callback])](#nanodbreplicationenablesource-target-opts-callback)
+  - [nano.db.replication.disable(id, [opts], [callback])](#nanodbreplicationdisableid-opts-callback)
   - [nano.db.changes(name, [params], [callback])](#nanodbchangesname-params-callback)
   - [nano.db.follow(name, [params], [callback])](#nanodbfollowname-params-callback)
   - [nano.db.info([callback])](#nanodbinfocallback)
@@ -53,7 +53,8 @@ or save `nano` as a dependency of your project with
   - [db.bulk(docs, [params], [callback])](#dbbulkdocs-params-callback)
   - [db.list([params], [callback])](#dblistparams-callback)
   - [db.fetch(docnames, [params], [callback])](#dbfetchdocnames-params-callback)
-  - [db.fetchRevs(docnames, [params], [callback])](#dbfetchRevsdocnames-params-callback)
+  - [db.fetchRevs(docnames, [params], [callback])](#dbfetchrevsdocnames-params-callback)
+  - [db.createIndex(indexDef, [callback])](#dbcreateindexindexdef-callback)
 - [Multipart functions](#multipart-functions)
   - [db.multipart.insert(doc, attachments, [params], [callback])](#dbmultipartinsertdoc-attachments-params-callback)
   - [db.multipart.get(docname, [params], [callback])](#dbmultipartgetdocname-params-callback)
@@ -615,6 +616,21 @@ Bulk fetch of the revisions of the database documents, `docnames` are specified
 additional query string `params` can be specified, this is the same method as fetch but
  `include_docs` is not automatically set to `true`.
 
+### db.createIndex(indexDef, [callback])
+
+Create index on database fields, as specified in
+[CouchDB doc](http://docs.couchdb.org/en/latest/api/database/find.html#db-index).
+
+```js
+var indexDef = {
+  index: { fields: ['foo'] },
+  name: 'fooindex'
+};
+alice.createIndex(indexDef, function(err, result) {
+  console.log(result);
+});
+```
+
 ## Multipart functions
 
 ### db.multipart.insert(doc, attachments, params, [callback])
diff --git a/lib/nano.js b/lib/nano.js
index 70fec9a..69fed84 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -761,6 +761,15 @@ module.exports = exports = nano = function dbScope(cfg) {
       }, callback);
     }
 
+    function createIndex(indexDef, callback) {
+      return relax({
+        db: dbName,
+        path: '_index',
+        method: 'POST',
+        body: indexDef
+      }, callback);
+    }
+
     // db level exports
     docScope = {
       info: function(cb) {
@@ -806,6 +815,7 @@ module.exports = exports = nano = function dbScope(cfg) {
       spatial: viewSpatial,
       view: viewDocs,
       find: find,
+      createIndex: createIndex,
       viewWithList: viewWithList,
       server: serverScope,
       replication: {
diff --git a/tests/fixtures/document/create_index.json b/tests/fixtures/document/create_index.json
new file mode 100644
index 0000000..a419823
--- /dev/null
+++ b/tests/fixtures/document/create_index.json
@@ -0,0 +1,19 @@
+[
+  { "method"   : "put"
+  , "path"     : "/document_create_index"
+  , "status"   : 201
+  , "response" : "{ \"ok\": true }"
+  }
+, { "method"   : "put"
+  , "status"   : 201
+  , "path"     : "/document_create_index/foobaz"
+  , "body"     : "{\"foo\":\"baz\"}"
+  , "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"1-611488\"}"
+  }
+, { "method"   : "post"
+  , "path"     : "/document_create_index/_index"
+  , "status"   : 200
+  , "body"     : "{\"name\":\"fooindex\",\"index\":{\"fields\":[\"foo\"]}}"
+  , "response" : "{\"result\":\"created\",\"id\":\"_design/a7ee061f1a2c0c6882258b2f1e148b714e79ccea\",\"name\": \"fooindex\"}"
+  }
+]
diff --git a/tests/integration/document/create_index.js b/tests/integration/document/create_index.js
new file mode 100644
index 0000000..dab3cee
--- /dev/null
+++ b/tests/integration/document/create_index.js
@@ -0,0 +1,33 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+'use strict';
+
+var helpers = require('../../helpers/integration');
+var harness = helpers.harness(__filename);
+var db = harness.locals.db;
+var it = harness.it;
+
+it('should insert a one item', helpers.insertOne);
+
+it ('Should create one simple index', function(assert) {
+  db.createIndex({
+    name: 'fooindex',
+    index: { fields: ['foo'] }
+  }, function(error, foo) {
+    assert.equal(error, null, 'should have indexed fields');
+    assert.equal(foo.result, 'created', 'index should be created');
+    assert.equal(foo.name, 'foobaz', 'index should have correct name');
+
+    assert.end();
+  });
+});


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services