Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Add support for let and unset (#10)
Browse files Browse the repository at this point in the history
* add support for let and unset
* add testing for set
  • Loading branch information
meppu authored Jun 20, 2023
1 parent ab11dcf commit 6eee3b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/surreal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
start_link/1,
start/1,
signin/3,
signin/6,
signup/6,
authenticate/2,
invalidate/1,
set/3,
unset/2,
use/3,
query/3, query/2,
select/2,
Expand Down Expand Up @@ -93,6 +96,17 @@ authenticate(Connection, Token) ->
invalidate(Connection) ->
gen_server:call(Connection, {invalidate}).

%% @doc Assigns a value as a parameter for this connection.
-spec set(Connection :: gen_server:server_ref(), Name :: string(), Value :: term()) ->
surreal_response:result().
set(Connection, Name, Value) ->
gen_server:call(Connection, {set, unicode:characters_to_binary(Name), Value}).

%% @doc Removes a parameter for this connection
-spec unset(Connection :: gen_server:server_ref(), Name :: string()) -> surreal_response:result().
unset(Connection, Name) ->
gen_server:call(Connection, {unset, unicode:characters_to_binary(Name)}).

%% @doc Switch to a specific namespace and database.
-spec use(Connection :: gen_server:server_ref(), Namespace :: string(), Database :: string()) ->
surreal_response:result().
Expand Down
6 changes: 6 additions & 0 deletions src/surreal_gen_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ handle_call({authenticate, Token}, _From, Connection) ->
handle_call({invalidate}, _From, Connection) ->
Response = send_payload(Connection, <<"invalidate">>, []),
{reply, Response, Connection};
handle_call({set, Name, Value}, _From, Connection) ->
Response = send_payload(Connection, <<"let">>, [Name, Value]),
{reply, Response, Connection};
handle_call({unset, Name}, _From, Connection) ->
Response = send_payload(Connection, <<"unset">>, [Name]),
{reply, Response, Connection};
handle_call({use, Namespace, Database}, _From, Connection) ->
Response = send_payload(Connection, <<"use">>, [Namespace, Database]),
{reply, Response, Connection};
Expand Down
8 changes: 7 additions & 1 deletion test/surreal_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ surreal_test() ->

Should1And3 = {ok, #{<<"id">> => <<"testing:wow">>, <<"name">> => <<"kekw">>}},
Should2 = {ok, [#{<<"id">> => <<"testing:wow">>, <<"name">> => <<"kekw">>}]},
Should4 = {ok, [#{<<"id">> => <<"testing:muchwow">>, <<"value">> => <<"test value">>}]},

Result1 = surreal:create(Pid, "testing:wow", #{
<<"name">> => <<"kekw">>
Expand All @@ -37,7 +38,12 @@ surreal_test() ->
?assertEqual(Result2, Should2),

Result3 = surreal:delete(Pid, "testing:wow"),
?assertEqual(Result3, Should1And3).
?assertEqual(Result3, Should1And3),

surreal:set(Pid, "example", <<"test value">>),
[Result4] = surreal:query(Pid, "CREATE testing:muchwow SET value = $example"),

?assertEqual(Result4, Should4).

query_test() ->
Query1 = [
Expand Down

0 comments on commit 6eee3b0

Please sign in to comment.