From 3a3fc3c41d8244810efe02cd501b66113741657f Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Sun, 13 Mar 2011 20:42:53 +0100 Subject: [PATCH] Quick example for proxy --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 36985de..d996c95 100644 --- a/README.md +++ b/README.md @@ -499,6 +499,48 @@ A really interesting use of `:proxy` and `:view` together could be where you'd l Pretty cool! +## Proxy Support + +CouchDB makes it really easy to create databases on the fly, so easy in fact that it is perfectly +feasable to have one database per user or per company or per whatever makes sense to split into +its own individual database. CouchRest Model now makes it really easy to support this scenario +using the proxy methods. Here's a quick example: + + # Define a master company class, its children should be in their own DB + class Company < CouchRest::Model::Base + use_database COUCHDB_DATABASE + property :name + property :slug + + proxy_for :invoices + + def proxy_database + @proxy_database ||= COUCHDB_SERVER.database!("project_#{slug}") + end + end + + # Invoices belong to a company + class Invoice < CouchRest::Model::Base + property :date + property :total + + proxied_by :company + + design do + view :by_date + end + end + +By setting up our models like this, the invoices should be accessed via a company object: + + company = Company.first + company.invoices.new # build a new invoice + company.invoices.by_date.first # find company's first invoice by date + +Internally, all requests for invoices are passed through a model proxy. Aside from the +basic methods and views, it also ensures that some of the more complex queries are supported +such as validating for uniqueness and associations. + ## Configuration