Skip to content

Commit

Permalink
Merge pull request #309 from c0nstantx/revisions
Browse files Browse the repository at this point in the history
Get and set revisions limit per database
  • Loading branch information
panuhorsmalahti committed May 4, 2016
2 parents 4e22e4a + ba46b78 commit 33cb730
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ You can check if a database exists with the `exists()` method.
db.destroy(cb);
```

### set revisions limit ###
Each database has a limit on how many revisions of a document can save.

Default value: **1000**

``` js
db.maxRevisions(4, function(err, res) {
if (err) {
console.log('error', err);
} else {
console.log('Revisions limit has changed');
}
});
```

### get revisions limit ###
You can also get the current revisions limit

``` js
db.maxRevisions(function(err, limit) {
if (err) {
console.log('error', err);
} else {
console.log('Revisions limit is: '+limit);
}
});
```


### fetching a document _(GET)_ ###

``` js
Expand Down Expand Up @@ -650,6 +679,9 @@ Other API methods
- `compact()`: Compact database
- `viewCleanup()`: Cleanup old view data
- `replicate(target, options)`: Replicate this database to `target`.
- `maxRevisions(function(error, limit))`: Get revision limit
- `maxRevisions(rev, function(error, result))`: Set revision limit


### cache API ###

Expand Down
26 changes: 25 additions & 1 deletion lib/cradle/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ Database.prototype.create = function (callback) {
this.query({ method: 'PUT' }, callback);
};

Database.prototype.maxRevisions = function (revisions, callback) {
if (typeof(revisions) === 'function') {
callback = revisions;
var options = {
method: 'GET',
path: '_revs_limit'
};
options.path = [this.name, options.path].filter(Boolean).join('/');
this.connection.rawRequest(options, function(err, res) {
if (err) {
return callback && callback(true, null);
} else {
return callback && callback(null, parseInt(res.body, 10));
}
});
} else {
this.query({
method: 'PUT',
path: '_revs_limit',
body: !Number.isNaN(revisions) ? parseInt(revisions, 10) : 1000
}, callback);
}
};

// Destroys a database with 'DELETE'
// we raise an exception if arguments were supplied,
// as we don't want users to confuse this function with `remove`.
Expand All @@ -57,7 +81,7 @@ Database.prototype.destroy = function (callback) {

this.query({
method: 'DELETE',
path: '/',
path: '/'
}, callback);
};

Expand Down
31 changes: 31 additions & 0 deletions test/database-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ function shouldQueryCouch(name) {
return promise;
},
"returns a 200": macros.status(200)
},
"getting database max revisions (GET)": {
topic: function(db) {
var promise = new(events.EventEmitter);
db.maxRevisions(function(e, res) {
promise.emit('success', res);
});
return promise;
},
"returns default value (1000)": function(res) {
assert.equal(res, 1000);
}
},
"setting database max revisions to 10 (PUT)": {
topic: function(db) {
var promise = new(events.EventEmitter);
db.maxRevisions(10, function(err, putResponse) {
db.maxRevisions(function(err1, getResponse) {
promise.emit('success', putResponse, getResponse);
/* Reset to default */
db.maxRevisions(1000, function(e, res) {});
});
});
return promise;
},
"returns true": function(err, putResponse) {
assert.isTrue(putResponse.ok);
},
"new value is assigned (10)": function(err, putResponse, getResponse) {
assert.equal(getResponse, 10);
}
}
}
}
Expand Down

0 comments on commit 33cb730

Please sign in to comment.