Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More client functions!

  • Loading branch information...
commit 7c4752a9b949b50a8feccc4da1461dfab8b9531c 1 parent 49db7e0
@felixge authored
View
19 LICENSE.txt
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Debuggable Limited <felix@debuggable.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
1  README.md
@@ -3,7 +3,6 @@
# Todo
* doc:attachments
-* db:all_dbs
* db:compact
* db:bulk_docs
* db:temp_view
View
55 lib/couchdb.js
@@ -116,6 +116,49 @@ Client.prototype.request = function(method, path, data) {
return this._queueRequest(options);
};
+Client.prototype.allDbs = function() {
+ return this.request({
+ path: '/_all_dbs'
+ });
+};
+
+Client.prototype.config = function() {
+ return this.request({
+ path: '/_config'
+ });
+};
+
+Client.prototype.uuids = function(count) {
+ return this.request({
+ path: '/_uuids'+(count ? '?count='+count : '')
+ });
+};
+
+Client.prototype.replicate = function(source, target, options) {
+ options = process.mixin({
+ source: source,
+ target: target,
+ }, options || {});
+
+ return this.request({
+ method: 'POST',
+ path: '/_replicate',
+ data: options
+ });
+};
+
+Client.prototype.stats = function() {
+ return this.request({
+ path: '/_stats'
+ });
+};
+
+Client.prototype.activeTasks = function() {
+ return this.request({
+ path: '/_active_tasks'
+ });
+};
+
Client.prototype.db = function(name) {
var
couchClient = this,
@@ -125,6 +168,10 @@ Client.prototype.db = function(name) {
return name;
});
+ couchDb.__defineGetter__('client', function() {
+ return couchClient;
+ });
+
couchDb.request = function(method, path, data) {
var options = requestOptions(method, path, data);
options.path = '/'+name+(options.path || '');
@@ -157,11 +204,15 @@ Db.prototype.exists = function() {
};
Db.prototype.info = function() {
- return this.request('GET', '');
+ return this.request({
+ method: 'GET'
+ });
};
Db.prototype.create = function() {
- return this.request('PUT', '');
+ return this.request({
+ method: 'PUT'
+ });
};
Db.prototype.saveDoc = function(id, doc) {
View
5 test/common.js
@@ -1,4 +1,7 @@
process.mixin(require('sys'));
GLOBAL.couchdb = require('../lib/couchdb');
-GLOBAL.assert = require('assert');
+GLOBAL.assert = require('assert');
+
+// Provide a port/host here if your local db has a non-default setup
+GLOBAL.client = couchdb.createClient();
View
83 test/test-client.js
@@ -0,0 +1,83 @@
+require('./common');
+
+var
+ DB_NAME = 'node-couchdb-test',
+ DB_NAME2 = 'node-couchdb-test-mirror',
+ TEST_ID = 'my-doc',
+ TEST_DOC = {hello: 'world'},
+
+ callbacks = {
+ A: false,
+ B: false,
+ C: false,
+ D: false,
+ E: false,
+ F: false,
+ };
+
+// Get a list of all databases
+client
+ .allDbs()
+ .addCallback(function(r) {
+ callbacks.A = true;
+ assert.ok('length' in r);
+ });
+
+// Get the couch config
+client
+ .config()
+ .addCallback(function(r) {
+ callbacks.B = true;
+ assert.ok('httpd' in r);
+ });
+
+// Get some uuids
+client
+ .uuids(3)
+ .addCallback(function(r) {
+ callbacks.C = true;
+ assert.equal(3, r.uuids.length);
+ });
+
+// Get the couch stats
+client
+ .stats()
+ .addCallback(function(r) {
+ callbacks.D = true;
+ assert.ok('httpd_status_codes' in r);
+ });
+
+// Find all active tasks
+client
+ .activeTasks()
+ .addCallback(function(r) {
+ callbacks.E = true;
+ assert.ok('length' in r);
+ });
+
+// Lets create two dbs to test replication
+var db = client.db(DB_NAME);
+db.remove().addErrback(function() {});
+db.create();
+db.saveDoc(TEST_ID, TEST_DOC);
+
+var db2 = client.db(DB_NAME2);
+db2.remove().addErrback(function() {});
+db2.create();
+
+client
+ .replicate(DB_NAME, DB_NAME2)
+ .addCallback(function(r) {
+ callbacks.F = true;
+ assert.ok('session_id' in r);
+ });
+
+// Cleanup
+db.remove();
+db2.remove();
+
+process.addListener('exit', function() {
+ for (var k in callbacks) {
+ assert.ok(callbacks[k], 'Callback '+k+' fired');
+ }
+});
View
1  test/test-db.js
@@ -21,7 +21,6 @@ var
L: false,
},
- client = couchdb.createClient(),
db = client.db(DB_NAME);
// Cleanup if test crashed in the middle
View
4 test/test-request.js
@@ -8,9 +8,7 @@ var
D: false,
E: false,
F: false,
- },
-
- client = couchdb.createClient();
+ };
client
.request('/_uuids')
Please sign in to comment.
Something went wrong with that request. Please try again.