anchor.diff:      "TRUE";
   pack_value(false) ->
       "FALSE".
  +
  +%% Functions below copied from emongo <https://github.com/boorad/emongo>
  +%% 
  +%% Copyright (c) 2009 Jacob Vorreuter <jacob.vorreuter@gmail.com> 
  +%% Jacob Perkins <japerk@gmail.com> 
  +%% Belyaev Dmitry <rumata-estor@nm.ru> 
  +%% François de Metz <fdemetz@af83.com>
  +
  +dec2hex(Dec) ->
  +    dec2hex(<<>>, Dec).
  +
  +dec2hex(N, <<I:8,Rem/binary>>) ->
  +    dec2hex(<<N/binary, (hex0((I band 16#f0) bsr 4)):8, (hex0((I band 16#0f))):8>>, Rem);
  +dec2hex(N,<<>>) ->
  +    N.
  +
  +hex2dec(Hex) when is_list(Hex) ->
  +    hex2dec(list_to_binary(Hex));
  +
  +hex2dec(Hex) ->
  +    hex2dec(<<>>, Hex).
  +
  +hex2dec(N,<<A:8,B:8,Rem/binary>>) ->
  +    hex2dec(<<N/binary, ((dec0(A) bsl 4) + dec0(B)):8>>, Rem);
  +hex2dec(N,<<>>) ->
  +    N.
  +
  +dec0($a) -> 10;
  +dec0($b) -> 11;
  +dec0($c) -> 12;
  +dec0($d) -> 13;
  +dec0($e) -> 14;
  +dec0($f) -> 15;
  +dec0(X) -> X - $0.
  +
  +hex0(10) -> $a;
  +hex0(11) -> $b;
  +hex0(12) -> $c;
  +hex0(13) -> $d;
  +hex0(14) -> $e;
  +hex0(15) -> $f;
  +hex0(I) ->  $0 + I.
  +
  
positive.code: 
  
  find(Conn, Id) when is_list(Id) ->
      {Type, TableName, TableId} = infer_type_from_id(Id),
      Res = pgsql:equery(Conn, ["SELECT * FROM ", TableName, " WHERE id = $1"], [TableId]),
      case Res of
          {ok, _Columns, []} ->
              undefined;
          {ok, Columns, [Record]} ->
              case model_is_loaded(Type) of
                  true -> activate_record(Record, Columns, Type);
                  false -> {error, {module_not_loaded, Type}}
              end;
          {error, Reason} ->
              {error, Reason}
      end.
  
  find(Conn, Type, Conditions, Max, Skip, Sort, SortOrder) when is_atom(Type), is_list(Conditions), 
                                                                is_integer(Max), is_integer(Skip), 
                                                                is_atom(Sort), is_atom(SortOrder) ->
      case model_is_loaded(Type) of
          true ->
              Query = build_select_query(Type, Conditions, Max, Skip, Sort, SortOrder),
              Res = pgsql:equery(Conn, Query, []),
              case Res of
                  {ok, Columns, Rows} ->
                      lists:map(fun(Row) ->
                                  activate_record(Row, Columns, Type)
                          end, Rows);
                  {error, Reason} ->
                      {error, Reason}
              end;
          false -> {error, {module_not_loaded, Type}}
      end.
  
  count(Conn, Type, Conditions) ->
      ConditionClause = build_conditions(Conditions),
      TableName = type_to_table_name(Type),
      {ok, _, [{Count}]} = pgsql:equery(Conn, 
          ["SELECT COUNT(*) AS count FROM ", TableName, " WHERE ", ConditionClause]),
      Count.
  
  counter(Conn, Id) when is_list(Id) ->
  
negative.code:     end.
  
  delete(Conn, Id) when is_list(Id) ->
      ?LOG("ID", Id),
      {_Type, Collection, MongoId} = infer_type_from_id(Id),
      
      Res = execute(Conn, fun() ->
                  mongo:delete(Collection, {'_id', MongoId})
          end),
      ?LOG("Res", Res),
      case Res of
          {ok, ok} -> ok;
          {failure, Reason} -> {error, Reason};
          {connection_failure, Reason} -> {error, Reason}
      end.
  
  proplist_to_tuple(PropList) ->
      ListOfLists = [[K,V]||{K,V} <- PropList],
      list_to_tuple(lists:foldl(
              fun([K, V], Acc) ->
                      [K,V|Acc] 
              end, [], ListOfLists)).
  
  type_to_collection(Type) ->
      list_to_atom(type_to_table_name(Type)).
  
  save_record(Conn, Record) when is_tuple(Record) ->
      Type = element(1, Record),
      Collection = type_to_collection(Type),
      Attributes = case Record:id() of
          id ->
              proplist_to_tuple(proplists:delete(id, Record:attributes()));
          DefinedId when is_list(DefinedId) ->
              io:format(">>>> ~p, ~p~n", [Type, DefinedId]),
              PropList = lists:map(fun({K,V}) ->
                              case K of 
                                  id -> {'_id', boss_to_mongo_id(DefinedId)};
                                  _ -> {K, V}
                              end
                      end, Record:attributes()),
              proplist_to_tuple(PropList)
      end,
      Res = execute(Conn, fun() -> 
                  mongo:save(Collection, Attributes)
          end),
      case Res of
          {ok, ok} -> {ok, Record};
          {ok, Id} -> 
              {ok, Record:id(mongo_to_boss_id(Type, Id))};
          {failure, Reason} -> {error, Reason};
          {connection_failure, Reason} -> {error, Reason}
      end.
  