How can i pass a parameter to a map function in PouchDB?

818 views
Skip to first unread message

Jens Vanderhaeghe

unread,
Jun 5, 2013, 3:30:24 AM6/5/13
to pou...@googlegroups.com
Hello,

I was wondering if it is possible to pass a parameter to a map function in PouchDB, in CouchDB I do this with a queryparameter but how would I achieve this in PouchDB?
I am trying to get products from my PouchDB database by name or by category.

I am currently using PouchDB-GQL and then I construct a query dynamically, but I feel like the performance of that plugin is somewhat slow and I would prefer to not rely on external plugins for PouchDB.

Thanks in advance!

Brian Toth

unread,
Jun 6, 2013, 12:01:22 PM6/6/13
to pou...@googlegroups.com
Hi Jens!

I'm excited that you are using GQL (I created that particular plugin).  If you have any feedback I would be happy to hear it.

Unfortunately, it isn't possible right now to use query parameters.  This is tied in to the poor performance that you are seeing - PouchDB does not store indexes.  The performance for the map reduce plugin will be approximately the same for that reason.  This is something that is being looked into: https://github.com/daleharvey/pouchdb/issues/99 (unfortunately I have been really busy and have not been able to revisit this).

Brian

Dale Harvey

unread,
Jun 6, 2013, 1:06:09 PM6/6/13
to pou...@googlegroups.com
Yeh it doesnt look like we pass in query paramters into view functions, I didnt think that would be possible with CouchDB either? if you are using query parameters inside the map function you cant build incremental indexes? (we support query_params in filters though)

If you want to fetch by name or category then you could: byname: { map: function(doc) { emit(doc.name) }}, bycategory: { map ..... surely?

Or use GQL, the performance of these 'should' be relatively similiar, as Brian said follow #99 which will improve performance drastically



--
You received this message because you are subscribed to the Google Groups "PouchDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pouchdb+u...@googlegroups.com.
To post to this group, send email to pou...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pouchdb/441a01f0-29a0-4296-8260-79c0c7faa4ad%40googlegroups.com?hl=en.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jens Vanderhaeghe

unread,
Jun 7, 2013, 4:17:30 AM6/7/13
to pou...@googlegroups.com
Allright, that sounds like a great solution untill the performance update. As my dataset is relatively small  (about 30-50 documents, maybe 100kb of total database size). it shouldn't be too much of a problem.
I really like your GQL plugin as I am coming from a SQL background and it makes it easier for me to query the database!

Op donderdag 6 juni 2013 19:06:09 UTC+2 schreef Dale Harvey het volgende:

Fred Cox

unread,
Jun 10, 2013, 4:33:14 PM6/10/13
to pou...@googlegroups.com
When you use a map function, the function you pass is actually converted to a string, this is because in couchdb terms, that function is run on the server side.

You can actually pass a string, instead of a function, this is what I wrote in my code - note the quotes before function(doc).

db.query({map: "function(doc) {\
if(doc.type == 'course' && doc.slug == " + JSON.stringify(slug) + ") {\
emit(doc._id, null);\
}\
}"}, cb);

I use JSON.stringify to safely convert my slug parameter to a javascript string.
Reply all
Reply to author
Forward
0 new messages