inoi / trombi

Trombi is an asynchronous CouchDB client for Tornado

Home Page:http://inoi.github.com/trombi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Disjoint between Document and ViewResult objects

cwells opened this issue · comments

When a Document is instantiated, _id is added as the self.id attribute to the object, but not added to self.data. However when retrieving a view, the documents come back as plain dicts, with no self.id attribute and having an _id key.

The net result of this is that there's an inconsistent feel to query results. You must either use Document.raw() or map Document over ViewResults. _id especially is something that must be dealt with on every record, so having to access it two different ways isn't appealing.

My suggestion would be to put all the values in self.data, regardless of whether they start with underscore or not.

Original idea behind hiding the _id and other attributes, like _rev or _attachments was to separate the document data from the document metadata which the CouchDB adds to the documents.

This way handling the actual data and testing it (for example) are easier, as there's no need to pop different underscore prefixed values from the Document object. In general, when using trombi there should be no need to tamper the CouchDB metadata. This is especially vital when working with attachments (under _attachments).

Reason why ViewResult objects don't contain Document objects is that the views can return anything, not only documents.

I could add an option for Database.view to wrap view result objects in documents, if the user explicitly tells so. I'm not that keen of removing the separation of metadata and actual data

I can do the wrapping myself, it's not that big of a deal. I was just wondering why the two situations were treated differently when one is merely a multiple of the other.

The main case is the behaviour of the views, as you never know if there's full documents coming or just excerpts (or computed values) of those.

I still kind of like the idea of receiving Document objects from the view by saying to the view function that the returned values are docs. Let's see what I come up with.