Are Views in Documents temporary?

30 views
Skip to first unread message

burli

unread,
Dec 8, 2011, 3:39:55 PM12/8/11
to couchdb...@googlegroups.com
It is possible to add a ViewField to a Document.

Are these views temporary (slow)?

Alexander Shorin

unread,
Dec 8, 2011, 3:52:08 PM12/8/11
to couchdb...@googlegroups.com
On Fri, Dec 9, 2011 at 12:39 AM, burli <m...@embedit.de> wrote:
> It is possible to add a ViewField to a Document.
>
> Are these views temporary (slow)?
>

1. Sure, in same way as any other field.

2. No, it will be static because you have to define design and view
name for ViewField and sync it with database or ResourceNotFound
exception will be raised when you'll get the results.

--
,,,^..^,,,

burli

unread,
Dec 8, 2011, 4:17:15 PM12/8/11
to couchdb...@googlegroups.com
My problem is, that the view is not stored in the database. How can this be fast?

Alexander Shorin

unread,
Dec 8, 2011, 4:23:49 PM12/8/11
to couchdb...@googlegroups.com
On Fri, Dec 9, 2011 at 1:17 AM, burli <m...@embedit.de> wrote:
> My problem is, that the view is not stored in the database. How can this be
> fast?
>

Technically it could be (cache hit by function code hash), but in real
world it could not and you shouldn't relay on this. For small
databases this is not a problem, but not for big ones.

--
,,,^..^,,,

burli

unread,
Dec 8, 2011, 4:54:48 PM12/8/11
to couchdb...@googlegroups.com
Oh, did I send the last post to you? I'm sorry

Can I use "normal" views to load a document class? Or do I have to call a view, pick the ID and load the document?

Alexander Shorin

unread,
Dec 8, 2011, 4:57:29 PM12/8/11
to couchdb...@googlegroups.com

No problem(: I'll repost my answer:

Sure, just pass include_doc=True parameter to view call and wrap
document from row['doc'] by related class. For example:

import couchdb
from couchdb.mapping import *

class User(Document):
name = TextField()
type = TextField(default='user')

class Article(Document):
author = TextField()
type = TextField(default='article')

@ViewField.define('article',
include_docs=True,
wrapper=lambda row: User(**row['doc']))
def by_user(doc):
if doc['type'] == 'article':
yield doc['_id'], {'_id': doc['author']}

db = couchdb.Database('http://localhost:5984/blog')

for user in Article.by_user(db):
assert isinstance(user, User)
assert user.name is not None
# assert user.name == 'bar'


--
,,,^..^,,,

burli

unread,
Dec 8, 2011, 5:08:59 PM12/8/11
to couchdb...@googlegroups.com
Oh, can you translate this to JavaScript, please? I cant use Python views :( and I dont know how lambda looks like in JS

Alexander Shorin

unread,
Dec 8, 2011, 5:25:25 PM12/8/11
to couchdb...@googlegroups.com
On Fri, Dec 9, 2011 at 2:08 AM, burli <m...@embedit.de> wrote:
> Oh, can you translate this to JavaScript, please? I cant use Python views :(
> and I dont know how lambda looks like in JS
>

This lambda runs on couchdb-python side to handle rows from views
result. If you remove it, next code will do the same thing:

for row in Article.by_user(db):
user = User(**row['doc'])

For JS those ViewField will looks like:

MAP_FUN = """
function(doc){
if (doc.type == 'article'){
emit(doc._id, {'_id': doc.author})
}
}
"""

class Article(Document):
author = TextField()
type = TextField(default='article')

by_user = ViewField('article',
map_fun=MAP_FUN,


include_docs=True,
wrapper=lambda row: User(**row['doc']))


P.S. `by_user` name wasn't good because actually you'll get an authors
list, not articles by some user id.

--
,,,^..^,,,

Reply all
Reply to author
Forward
0 new messages