Skip to content

Commit

Permalink
Changed db.create to call success even if the database already exists…
Browse files Browse the repository at this point in the history
…. Added an additional parameter to db.create called failOnDuplicate, if true, db.create will behave as it used to.
  • Loading branch information
jasonwyatt committed Apr 24, 2010
1 parent 0e4464e commit ce568bd
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function _interact(verb, path, successStatus, options, host) {
// HTTP request type (or verb). E.g. GET, PUT, POST, DELETE
// path: String
// CouchDB resource path.
// successStatus: Integer
// The HTTP response code we expect on a successful interaction
// successStatus: Integer|Array
// The HTTP response code(s) we expect on a successful interaction
// with the server.
// options: Object
// Options for the interaction with the server.
Expand Down Expand Up @@ -132,7 +132,7 @@ function _interact(verb, path, successStatus, options, host) {
}
responseBody = JSON.parse(responseBody);

if (response.statusCode === successStatus) {
if (isAcceptableStatus(response.statusCode,successStatus)) {
if (options.success) {
options.success(responseBody);
}
Expand All @@ -146,6 +146,30 @@ function _interact(verb, path, successStatus, options, host) {

}

function isAcceptableStatus(testCode, acceptable){
// summary:
// Quick utility function to test if an HTTP response code is
// acceptable for a purpose.
// testCode: Integer
// Code we're testing.
// acceptable: Integer|Array
// Either an individual status code, or an array of acceptable
// status codes.
// returns:
// True if testCode is an acceptable status code. False otherwise.

if (acceptable.constructor.toString().indexOf("Array") == -1) {
acceptable = [acceptable];
}

for(var i = 0, len = acceptable.length; i < len; i++){
if(acceptable[i] == testCode){
return true;
}
}
return false;
}

function encodeOptions(options) {
// summary:
// Builds an HTTP query string given an object containing options
Expand Down Expand Up @@ -315,8 +339,8 @@ var CouchDB = {
// That is, it's the stuff that comes after
// [host]/[name]/ in a URL. Otherwise, it's a full
// path.
// successStatus: Integer
// HTTP status code that indicates successful
// successStatus: Integer|Array
// HTTP status code(s) that indicate successful
// interaction.
// options: Object
// Standard options object.
Expand All @@ -341,7 +365,7 @@ var CouchDB = {
this.interact("post", "_compact", 202, options);
},

create : function(options) {
create : function(options, failOnDuplicate) {
// summary:
// Creates the database.
// description:
Expand All @@ -350,7 +374,19 @@ var CouchDB = {
// http://wiki.apache.org/couchdb/HTTP_database_API
// options: Object
// Standard options object.
this.interact("put", "", 201, options);
// failOnDuplicate: Boolean - Optional
// If set to true, this function will fail and the
// error option callback will be run if the database
// already exists. If set to false, or left unset, the
// success function will be called even if the database
// exists.

var acceptableStatuses = [201, 412];
if(failOnDuplicate){
acceptableStatuses.pop();
}

this.interact("put", "", acceptableStatuses, options);
},

drop : function(options) {
Expand Down

0 comments on commit ce568bd

Please sign in to comment.