The CouchDB adapter for the Generic Object Mapper (github.com/phifty/gom) provides an easy way store and fetch object data to/from a CouchDB server. Object properties are mapped as well as object relations.
If the couchdb adapter is chosen in the storage configuration, the following configuration values should be specified.
-
host
- Hostname where the CouchDB server is running on. Default is localhost. -
port
- Port where the CouchDB server is listening to. Default is 5984. -
database
- Name of the database that should be used. -
delete_database_if_exists
- Can be true or false and specifies if an existing database should be delete or not. This can be useful in your test setup. Default is false. -
create_database_if_missing
- Can be true or false and specifies if a missing database should be created or not. Default is false.
GOM::Storage.configure { storage { name :storage_name adapter :couchdb host "another_host" database "production_db" delete_database_if_exists false create_database_if_missing true } }
Basically one document is created in the CouchDB database for each stored object. The instance variables from the object are stored as the document’s keys and values. The objects relations to other objects are stored as references to the other object’s document id. So if the following object is saved …
class Book attr_accessor :pages attr_accessor :author end class Author attr_accessor :name end author = Author.new author.name = "Mr. Storyteller" book = Book.new book.pages = 1253 book.author = GOM::Object.reference author GOM::Storage.store book, :storage_name
… the following documents will occur in the database.
{ "_id": "book_1", "_rev": "...", "model_class": "Book", "pages": 1253, "author_id": "author_1" } { "_id": "author_1", "_rev": "...", "model_class": "Author", "name": "Mr. Storyteller" }
If the author would have been stored before and assigned to an id, it wouldn’t be stored again.
To get back the book from the storage, simply call …
book = GOM::Storage.fetch "storage_name:book_1"
… and an object of the class Book
with same instance variable values as it had before, will be returned. Since lazy loading is supported, the author will just be fetched on the first access. So …
author_name = book.author.name
… will invoke another fetch of the author’s object.
This adapter currently supports class and map/reduce views. For documentation see github.com/phifty/gom.
Development has been done test-driven and the code follows at most the Clean Code paradigms. Code smells has been removed by using the reek code smell detector.
The project is still experimental and under development. Any bug report and contribution is welcome!
Apart from contribution, support via Flattr is welcome.