Class: CouchRest::Database
- Inherits:
-
Object
- Object
- CouchRest::Database
- Defined in:
- lib/couchrest/core/database.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#bulk_save(docs) ⇒ Object
POST an array of documents to CouchDB.
-
#delete(doc) ⇒ Object
DELETE the document from CouchDB that has the given
_idand_rev. -
#delete! ⇒ Object
DELETE the database itself.
-
#documents(params = nil) ⇒ Object
Query the
_all_docsview. -
#fetch_attachment(doc, name) ⇒ Object
GET an attachment directly from CouchDB.
-
#get(id) ⇒ Object
GET a document from CouchDB, by id.
-
#info ⇒ Object
GET the database info from CouchDB.
-
#initialize(server, name) ⇒ Database
constructor
Create a CouchRest::Database adapter for the supplied CouchRest::Server and database name.
-
#put_attachment(doc, name, file, options = {}) ⇒ Object
PUT an attachment directly to CouchDB.
-
#save(doc) ⇒ Object
Save a document to CouchDB.
-
#temp_view(funcs, params = nil) ⇒ Object
POST a temporary view function to CouchDB for querying.
-
#to_s ⇒ Object
returns the database’s uri.
-
#view(name, params = nil) ⇒ Object
Query a CouchDB view as defined by a
_designdocument.
Constructor Details
#initialize(server, name) ⇒ Database
Create a CouchRest::Database adapter for the supplied CouchRest::Server and database name.
Parameters
- server<CouchRest::Server>
-
database host
- name<String>
-
database name
14 15 16 17 18 19 |
# File 'lib/couchrest/core/database.rb', line 14 def initialize server, name @name = name @server = server @host = server.uri @root = "#{host}/#{name}" end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def host @host end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def root @root end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
6 7 8 |
# File 'lib/couchrest/core/database.rb', line 6 def server @server end |
Instance Method Details
#bulk_save(docs) ⇒ Object
POST an array of documents to CouchDB. If any of the documents are missing ids, supply one from the uuid cache.
94 95 96 97 98 99 100 101 102 |
# File 'lib/couchrest/core/database.rb', line 94 def bulk_save docs ids, noids = docs.partition{|d|d['_id']} uuid_count = [noids.length, @server.uuid_batch_count].max noids.each do |doc| nextid = @server.next_uuid(uuid_count) rescue nil doc['_id'] = nextid if nextid end CouchRest.post "#{@root}/_bulk_docs", {:docs => docs} end |
#delete(doc) ⇒ Object
DELETE the document from CouchDB that has the given _id and _rev.
105 106 107 108 |
# File 'lib/couchrest/core/database.rb', line 105 def delete doc slug = CGI.escape(doc['_id']) CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}" end |
#delete! ⇒ Object
DELETE the database itself. This is not undoable and could be rather catastrophic. Use with care!
111 112 113 |
# File 'lib/couchrest/core/database.rb', line 111 def delete! CouchRest.delete @root end |
#documents(params = nil) ⇒ Object
Query the _all_docs view. Accepts all the same arguments as view.
32 33 34 35 |
# File 'lib/couchrest/core/database.rb', line 32 def documents params = nil url = CouchRest.paramify_url "#{@root}/_all_docs", params CouchRest.get url end |
#fetch_attachment(doc, name) ⇒ Object
GET an attachment directly from CouchDB
56 57 58 59 60 |
# File 'lib/couchrest/core/database.rb', line 56 def doc, name doc = CGI.escape(doc) name = CGI.escape(name) RestClient.get "#{@root}/#{doc}/#{name}" end |
#get(id) ⇒ Object
GET a document from CouchDB, by id. Returns a Ruby Hash.
50 51 52 53 |
# File 'lib/couchrest/core/database.rb', line 50 def get id slug = CGI.escape(id) CouchRest.get "#{@root}/#{slug}" end |
#info ⇒ Object
GET the database info from CouchDB
27 28 29 |
# File 'lib/couchrest/core/database.rb', line 27 def info CouchRest.get @root end |
#put_attachment(doc, name, file, options = {}) ⇒ Object
PUT an attachment directly to CouchDB
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/couchrest/core/database.rb', line 63 def doc, name, file, = {} docid = CGI.escape(doc['_id']) name = CGI.escape(name) uri = if doc['_rev'] "#{@root}/#{docid}/#{name}?rev=#{doc['_rev']}" else "#{@root}/#{docid}/#{name}" end JSON.parse(RestClient.put(uri, file, )) end |
#save(doc) ⇒ Object
Save a document to CouchDB. This will use the _id field from the document as the id for PUT, or request a new UUID from CouchDB, if no _id is present on the document. IDs are attached to documents on the client side because POST has the curious property of being automatically retried by proxies in the event of network segmentation and lost responses.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/couchrest/core/database.rb', line 76 def save doc if doc['_attachments'] doc['_attachments'] = (doc['_attachments']) end if doc['_id'] slug = CGI.escape(doc['_id']) CouchRest.put "#{@root}/#{slug}", doc else begin slug = doc['_id'] = @server.next_uuid CouchRest.put "#{@root}/#{slug}", doc rescue #old version of couchdb CouchRest.post @root, doc end end end |
#temp_view(funcs, params = nil) ⇒ Object
POST a temporary view function to CouchDB for querying. This is not recommended, as you don’t get any performance benefit from CouchDB’s materialized views. Can be quite slow on large databases.
38 39 40 41 |
# File 'lib/couchrest/core/database.rb', line 38 def temp_view funcs, params = nil url = CouchRest.paramify_url "#{@root}/_temp_view", params JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'})) end |
#to_s ⇒ Object
returns the database’s uri
22 23 24 |
# File 'lib/couchrest/core/database.rb', line 22 def to_s @root end |
#view(name, params = nil) ⇒ Object
Query a CouchDB view as defined by a _design document. Accepts paramaters as described in wiki.apache.org/couchdb/HttpViewApi
44 45 46 47 |
# File 'lib/couchrest/core/database.rb', line 44 def view name, params = nil url = CouchRest.paramify_url "#{@root}/_view/#{name}", params CouchRest.get url end |