Anu
Anu

Reputation: 630

How to create a database in couchdb programatically?

I am able to create a database using futon or curl command but I am trying to create a database by getting the database name from the user. I had a textbox and a button when user types the database name and on clicking create db i am calling a ajax function to access the couch db. I am dont really know how to create a database on button click. I could get example like task list to insert edit and update but I am not able to create a database using ajax. I tried with this

$(document).ready(function(){
            $("#adddb").click(function(){
                var dbname = $("#dbname").val();
                 $.ajax({
                type: "PUT",
                url: DATABASE,
                contentType: "application/json",
                data: dbname
             });
            });
        });
<input type="text" id="dbname" placeholder="Database Name"> 
    <input type="button" id="adddb" value="Add Db">

I am getting this error.

PUT http://127.0.0.1:5984/ 405 (Method Not Allowed)

Is this a way to create a db or I am wrong. Can someone help me?

Upvotes: 0

Views: 931

Answers (2)

snehil
snehil

Reputation: 41

As the Doc suggests:

Your Request need not contain anything in the body, the request URL should itself contain the dbName

var dbName = $("#dbname").val();
$.ajax({
  type: "PUT",
  url: DATABASE_URL+"/"+ dbName
});

Also the dbName should make sure it follows these rules:

  • Name must begin with a lowercase letter (a-z)
  • Lowercase characters (a-z)
  • Digits (0-9)
  • Any of the characters _, $, (, ), +, -, and /.

Or follow this RegExp ^[a-z][a-z0-9_$()+/-]*$.

Upvotes: 1

Anu
Anu

Reputation: 630

I was keep trying this following works fine for me.

 $("#adddb").click(function(){
                 dbname = $("#dbname").val();
                 $.ajax({
                type: "PUT",
                url: DATABASE+"/"+dbname,
                contentType: "application/json",
                data: dbname
             });

PUT request helps to create a database as it is in curl PUT command

Upvotes: 0

Related Questions