- set @revision too when creating a Document
[couchobject.git] / lib / couch_object / database.rb
blob0c17085f83fff1222a58711854c1d0ef0e6872a8
1 module CouchObject
2   # A CouchDb database object
3   class Database
4     # Create a new database at +uri+ with the name if +dbname+
5     def self.create!(uri, dbname)
6       server = Server.new(uri)
7       response = Response.new(server.put("/#{dbname}", "")).parse
8       response.parsed_body
9     end
10     
11     # Delete the database at +uri+ with the name if +dbname+
12     def self.delete!(uri, dbname)
13       server = Server.new(uri)
14       response = Response.new(server.delete("/#{dbname}")).parse
15       response.parsed_body
16     end
17     
18     # All databases at +uri+
19     def self.all_databases(uri)
20       # FIXME: Move to Server ?
21       server = Server.new(uri)
22       resp = server.get("/_all_dbs")
23       response = Response.new(resp).parse
24       response.parsed_body
25     end
26     
27     # Open a connection to the database at +uri+, where +uri+ is a full uri
28     # like: http://localhost:8888/foo
29     def self.open(uri)
30       uri = URI.parse(uri)
31       server_uri = "#{uri.scheme}://#{uri.host}:#{uri.port}"
32       new(server_uri, uri.path.tr("/", ""))
33     end
34     
35     def initialize(uri, dbname)
36       @uri = uri
37       @dbname = dbname
38       @server = Server.new(@uri)
39     end
40     attr_accessor :server
41     
42     # The full url of this database, eg http://localhost:8888/foo
43     def url
44       Utils.join_url(@uri, @dbname).to_s
45     end
46     
47     # Name of this database
48     def name
49       @dbname.dup
50     end
51     
52     # Send a GET request to the +path+ which is relative to the database path
53     # so calling with with "bar" as the path in the "foo_db" database will call
54     # http://host:port/foo_db/bar.
55     # Returns a Response object
56     def get(path)
57       Response.new(@server.get("/#{Utils.join_url(@dbname, path)}")).parse
58     end
59     
60     # Send a POST request to the +path+ which is relative to the database path
61     # so calling with with "bar" as the path in the "foo_db" database will call
62     # http://host:port/foo_db/bar. The post body is the +payload+
63     # Returns a Response object
64     def post(path, payload)
65       Response.new(@server.post("/#{Utils.join_url(@dbname, path)}", payload)).parse
66     end
67     
68     # Send a PUT request to the +path+ which is relative to the database path
69     # so calling with with "bar" as the path in the "foo_db" database will call
70     # http://host:port/foo_db/bar. The put body is the +payload+
71     # Returns a Response object
72     def put(path, payload="")
73       Response.new(@server.put("/#{Utils.join_url(@dbname, path)}", payload)).parse
74     end
75     
76     # Send a DELETE request to the +path+ which is relative to the database path
77     # so calling with with "bar" as the path in the "foo_db" database will call
78     # http://host:port/foo_db/bar.
79     # Returns a Response object
80     def delete(path)
81       Response.new(@server.delete("/#{Utils.join_url(@dbname, path)}")).parse
82     end
83     
84     # Get a document by id
85     def [](id)
86       get(id.to_s)
87     end
88     
89     # Get a document by +id+, optionally a specific +revision+ too
90     def document(id, revision=nil)
91       if revision
92         get("#{id}?rev=#{revision}")
93       else
94         get(id.to_s)
95       end
96     end
97     
98     # Returns an Array of all the documents in this db
99     def all_documents
100       resp = Response.new(get("_all_docs")).parse
101       resp.to_document.rows
102     end
103     
104     # Queries the database with the block (using a temp. view)
105     def filter(&blk)
106       
107     end
108     
109     def views(view_name)
110       view = View.new(self, view_name)
111       view.query
112     end
113     
114   end