brailateo
1/22/2014 - 6:18 PM

LIST function for CouchDB - extracting information from a view (or from _all_docs) in order to be returned as a JSON data well formed, suita

LIST function for CouchDB - extracting information from a view (or from _all_docs) in order to be returned as a JSON data well formed, suitable for an aaData data source for a DataTable in browser

fun(Head, {Req}) ->
  Send(<<"[">>),
  Fun = fun({Row}, Virgula) ->
      Send(Virgula),
      case proplists:get_value(<<"doc">>, Row) of
      {Doc} ->
          Elocalitate = proplists:is_defined(<<"Siruta">>, Doc),
          if
            Elocalitate ->
                Send(<<"[\"">>),
                Send(proplists:get_value(<<"id">>, Row)),
                Send(<<"\",\"">>),
                Send(proplists:get_value(<<"Nume">>, Doc)),
                Send(<<"\",\"">>),
                Send(proplists:get_value(<<"Siruta">>, Doc)),
                Send(<<"\",\"">>),
                Send(proplists:get_value(<<"Județ">>, Doc)),
                Send(<<"\"]">>),
                {ok, <<",">>};
            true ->
                {ok,Virgula}
          end;
      _ ->
        {ok, <<",">>}
      end
  end,
  {ok, _} = FoldRows(Fun, <<"">>),
  <<"]">>
end.