anchor.diff:  %% Returns DB handle used by couchbeam library to connect to couchdb-based DB.
   %% @end
   %%--------------------------------------------------------------------
  --spec get_db() -> {ok, term()} | {error, term()}.
  +-spec get_db() -> {ok, {pid, term()}} | {error, term()}.
   get_db() ->
       Gateways = maps:values(datastore_worker:state_get(db_gateways)),
       ActiveGateways = [GW || #{status := running} = GW <- Gateways],
   
  -    case ActiveGateways of
  -        [] ->
  +    case length(ActiveGateways) of
  +        0 ->
               ?error("Unable to select CouchBase Gateway: no active gateway among: ~p", [Gateways]),
               {error, no_active_gateway};
           _ ->
  -            #{gw_port := Port} = lists:nth(crypto:rand_uniform(1, length(ActiveGateways) + 1), ActiveGateways),
  +            #{gw_port := Port, server := ServerLoop} = lists:nth(crypto:rand_uniform(1, length(ActiveGateways) + 1), ActiveGateways),
               Server = couchbeam:server_connection("localhost", Port),
  -            couchbeam:open_db(Server, <<"default">>)
  -    end.
  -
  -
  +            case couchbeam:open_db(Server, <<"default">>) of
  +                {ok, DB} ->
  +                    {ok, {ServerLoop, DB}};
  +                {error, Reason} ->
  +                    {error, Reason}
  +            end
  +    end.
  +
  +
  +-spec db_run(atom(), atom(), [term()], non_neg_integer()) -> term().
  +db_run(Mod, Fun, Args, Retry) ->
  +    {ok, {ServerPid, DB}} = get_db(),
  +    case apply(Mod, Fun, [DB | Args]) of
  +        {error, econnrefused} when Retry > 0 ->
  +            ?info("Unable to connect to ~p",[DB]),
  +            ServerPid ! restart,
  +            db_run(Mod, Fun, Args, Retry - 1);
  +        Other -> Other
  +    end.
   
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
positive.code: delete(#model_config{bucket = Bucket, name = ModelName} = ModelConfig, Key, Pred) ->
      datastore:run_synchronized(ModelName, to_binary({?MODULE, Key}),
          fun() ->
              {ok, DB} = get_db(),
              
              case Pred() of
                  true ->
                      case get(ModelConfig, Key) of
  
negative.code:         false -> ok
      end,
  
      {Props} = to_json_term(Value),
      Doc = {[{<<"_revisions">>, {[{<<"ids">>, Ids}, {<<"start">>, Start}]}}, {<<"_rev">>, rev_info_to_rev(Revs)}, {<<"_id">>, to_driver_key(Bucket, Key)} | Props]},
      case db_run(couchbeam, save_doc, [Doc, [{<<"new_edits">>, <<"false">>}]], 3) of
          {ok, {_}} ->
              {ok, Key};
          {error, conflict} ->
  