On Tue, Feb 5, 2013 at 8:06 PM, Tibor Gemes <[email protected]> wrote:
> A simle example which filters by type:
>
> fun({Doc}, {Req}) ->
> DocType = couch_util:get_value(<<"type">>, Doc),
> case DocType of
> undefined -> false;
> <<"mytype">> -> true;
> _ -> false
> end
> end.
>
>
> This is another example. A bit more complicated. This receives a values
> query parameter (comma separated). The filter lets through any design docs,
> any _deleted docs, and the mytype doc only if its value propery is
> contained in the query parameter:
>
> fun({Doc}, {Req}) ->
> DocId = couch_util:get_value(<<"_id">>, Doc),
> DocType = couch_util:get_value(<<"type">>, Doc),
> DocValue = couch_util:get_value(<<"value">>, Doc),
> DocDeleted = couch_util:get_value(<<"_deleted">>, Doc),
>
> {Query} = couch_util:get_value(<<"query">>,Req, {[]}),
> ValuesParam = couch_util:get_value(<<"values">>,Query),
> Values = binary:split(ValuesParam, <<",">>, [global]),
> case {DocId, DocDeleted} of
> {<<"_design/", _/binary>>, _} -> true;
> {_, true} -> true;
> _ ->
> case DocType of
> undefined -> false;
> <<"mytype">> -> lists:any(fun(E) -> E =:= DocValue
> end, Values);
> _ -> false
> end
> end
> end.
>
> Those might be suboptimal but works perfectly for me.
>
Thanks for sharing. I've added them as examples to the wiki with links to
your mail
http://wiki.apache.org/couchdb/EnableErlangViews
-- Stefan