SurrealEx.Socket (surrealdb_ex v0.0.2)

Summary

Functions

Authenticates the current connection with a JWT token.

Modifies all records in a table, or a specific record, in the database.

Creates a record in the database.

Deletes all records in a table, or a specific record, from the database.

Retreive info about the current Surreal instance.

Invalidates the authentication for the current connection.

Kill a specific query.

Switch to a specific namespace and database.

Get a live status from a specific table or row.

Applies JSON Patch changes to all records, or a specific record, in the database.

Ping SurrealDB instance

Runs a set of SurrealQL statements against the database.

Selects all records in a table, or a specific record, from the database.

Signs in to a specific authentication scope.

Signs up to a specific authentication scope.

Updates all records in a table, or a specific record, in the database.

Switch to a specific namespace and database.

Types

payload_type()

@type payload_type() :: map() | struct()

Functions

authenticate(pid, token)

@spec authenticate(process_identifier(), String) :: socket_response()

Authenticates the current connection with a JWT token.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.authenticate(pid, "[YOUR JWT TOKEN HERE]")

# SUCCESS CASE:
{:ok, %{"id" => "1915", "result" => nil}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "There was a problem with authentication"
    },
    "id" => "1492"
  }
}

authenticate(pid, token, task, opts \\ Domain.TaskOpts.default())

@spec authenticate(process_identifier(), String, Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

change(pid, table, payload)

@spec change(process_identifier(), String, payload_type()) :: socket_response()

Modifies all records in a table, or a specific record, in the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.change(pid, "users:tobie", %{admin: true})

# SUCCESS CASE:
{:ok, %{"id" => "303", "result" => [%{"admin" => true, "id" => "users:tobie"}]}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "6432"
  }
}

change(pid, table, payload, task, opts \\ Domain.TaskOpts.default())

@spec change(
  process_identifier(),
  String,
  payload_type(),
  Task,
  SurrealEx.Domain.TaskOpts
) ::
  socket_response()

create(pid, table, payload)

@spec create(process_identifier(), String, payload_type()) :: socket_response()

Creates a record in the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.create(pid, "users", %{name: "John Doe", age: 30})

# SUCCESS CASE:
{:ok,
  %{
    "id" => "9802",
    "result" => [
      %{"age" => 30, "id" => "users:agboh28f2vvy18d91q04", "name" => "John Doe"}
    ]
  }
}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

create(pid, table, payload, task, opts \\ Domain.TaskOpts.default())

@spec create(
  process_identifier(),
  String,
  payload_type(),
  Task,
  SurrealEx.Domain.TaskOpts
) ::
  socket_response()

delete(pid, table)

@spec delete(process_identifier(), String) :: socket_response()

Deletes all records in a table, or a specific record, from the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.delete(pid, "users:jeremy")

# SUCCESS CASE:
{:ok, %{"id" => "4054", "result" => []}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

delete(pid, table, task, opts \\ Domain.TaskOpts.default())

@spec delete(process_identifier(), String, Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

info(pid)

@spec info(process_identifier()) :: socket_response()

Retreive info about the current Surreal instance.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.info(pid)

# SUCCESS CASE:
{:ok, %{"id" => "9250", "result" => nil}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

info(pid, task, opts \\ Domain.TaskOpts.default())

@spec info(process_identifier(), Task, SurrealEx.Domain.TaskOpts) :: socket_response()

invalidate(pid)

@spec invalidate(process_identifier()) :: socket_response()

Invalidates the authentication for the current connection.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.invalidate(pid)

# SUCCESS CASE:
{:ok, %{"id" => "9250", "result" => nil}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

invalidate(pid, task, opts \\ Domain.TaskOpts.default())

@spec invalidate(process_identifier(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

is_payload_type(target)

(macro)

is_process_identifier(pid)

(macro)

kill(pid, query)

@spec kill(process_identifier(), String) :: socket_response()

Kill a specific query.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.kill(pid)

# SUCCESS CASE:
{:ok, %{"id" => "9250", "result" => nil}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

kill(pid, query, task, opts \\ Domain.TaskOpts.default())

@spec kill(process_identifier(), binary(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

let(pid, key, value)

@spec let(process_identifier(), String, String) :: socket_response()

Switch to a specific namespace and database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.use(pid, "test", "test")

# SUCCESS CASE:
{:ok, %{"id" => "9250", "result" => nil}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

let(pid, key, value, task, opts \\ Domain.TaskOpts.default())

@spec let(process_identifier(), binary(), binary(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

live(pid, table)

@spec live(process_identifier(), String) :: socket_response()

Get a live status from a specific table or row.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.live(pid, "users")

# SUCCESS CASE:
{:ok, %{"id" => "8913", "result" => "8354534f-5e42-4bb7-8bae-6cf41d38236a"}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

live(pid, table, task, opts \\ Domain.TaskOpts.default())

@spec live(process_identifier(), binary(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

modify(pid, table, payload)

@spec modify(process_identifier(), String, [payload_type()]) :: socket_response()

Applies JSON Patch changes to all records, or a specific record, in the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.modify(pid, "users", %{"name" => "John Doe"})

# SUCCESS CASE:
{:ok, %{"id" => "8044", "result" => []}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

modify(pid, table, payload, task, opts \\ Domain.TaskOpts.default())

@spec modify(
  process_identifier(),
  String,
  [payload_type()],
  Task,
  SurrealEx.Domain.TaskOpts
) ::
  socket_response()

ping(pid)

@spec ping(process_identifier()) :: socket_response()

Ping SurrealDB instance

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.ping(pid)

# SUCCESS CASE:
{:ok, %{"id" => "4562", "result" => true}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

ping(pid, task, opts \\ Domain.TaskOpts.default())

@spec ping(process_identifier(), Task, SurrealEx.Domain.TaskOpts) :: socket_response()

query(pid, query, payload)

@spec query(process_identifier(), String, payload_type()) :: socket_response()

Runs a set of SurrealQL statements against the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.query(pid, "SELECT * FROM type::table($table) WHERE admin = true;", %{table: "users"})

# SUCCESS CASE:
{:ok,
  %{
    "id" => "6042",
    "result" => [
      %{
        "result" => [
          %{
            "admin" => true,
            "age" => 29,
            "id" => "users:5ypb5ifhfo7tnj31pajl",
            "name" => "John Doe"
          },
          %{
            "admin" => true,
            "age" => 32,
            "id" => "users:zb71vk0kh9d33bucozr7",
            "name" => "Mary Jane"
          }
        ],
        "status" => "OK",
        "time" => "80.164µs"
      }
    ]
  }
}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "There was a problem with the database: Parse error on line 1 at character 0 when parsing [...]"
    },
    "id" => "1120"
  }
}

query(pid, query, payload, task, opts \\ Domain.TaskOpts.default())

@spec query(
  process_identifier(),
  binary(),
  payload_type(),
  Task,
  SurrealEx.Domain.TaskOpts
) ::
  socket_response()

select(pid, query)

@spec select(process_identifier(), String) :: socket_response()

Selects all records in a table, or a specific record, from the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.select(pid, "SELECT * FROM users;")

# SUCCESS CASE:
{:ok, %{"id" => "948", "result" => []}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "[...]"
    },
    "id" => "2578"
  }
}

select(pid, query, task, opts \\ Domain.TaskOpts.default())

@spec select(process_identifier(), String, Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

signin(pid, payload)

Signs in to a specific authentication scope.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.signin(pid, %{user: "root", pass: "root"})

# SUCCESS CASE:
{:ok, %{"id" => "6420", "result" => ""}}

# ERROR CASE:
{:error,
  %{
  "error" => %{
    "code" => -32000,
    "message" => "There was a problem with authentication"
  },
  "id" => "7335"
}}

signin(pid, payload, task, opts \\ Domain.TaskOpts.default())

@spec signin(
  process_identifier(),
  SurrealEx.Domain.SignInPayload,
  Task,
  SurrealEx.Domain.TaskOpts
) ::
  socket_response()
@spec signin(process_identifier(), payload_type(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

signup(pid, payload)

@spec signup(process_identifier(), payload_type()) :: socket_response()

Signs up to a specific authentication scope.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.signup(pid, %{user: "root", pass: "root", SC: "allusers", DB: "test", NS: "test"})

# SUCCESS CASE:
{:ok,
  %{
    "id" => "9267",
    "result" => "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2ODA2NjE5NDksIm5iZiI6MTY4MDY2MTk0OSwiZXhwIjoxNjgxODcxNTQ5LCJpc3MiOiJTdXJyZWFsREIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6ImFsbHVzZXJzIiwiSUQiOiJ1c2VyOjdqN2hubnloMGFseTV0cHlnb3JrIn0.7IZ7QL6BMgNv9xW_QHu-JrZdfDdX9ngGV5xlxNPHHIkPzgi9OW2iHdUt2wt8x4_5vRo9rijQge04Nvbl3aTV9A"
  }
}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "There was a problem with authentication"
    },
    "id" => "3694"
  }
}

signup(pid, payload, task, opts \\ Domain.TaskOpts.default())

@spec signup(process_identifier(), payload_type(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()

start_link(opts \\ [])

stop(pid)

@spec stop(pid()) :: :ok

update(pid, table, payload)

@spec update(process_identifier(), String, payload_type()) :: socket_response()

Updates all records in a table, or a specific record, in the database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.update(pid, "users:jeremy", %{admin: true})

# SUCCESS CASE:
{:ok, %{"id" => "6427", "result" => []}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "There was a problem with authentication"
    },
    "id" => "3694"
  }
}

update(pid, table, payload, task, opts \\ Domain.TaskOpts.default())

@spec update(
  process_identifier(),
  String,
  payload_type(),
  Task,
  SurrealEx.Domain.TaskOpts
) ::
  socket_response()

use(pid, namespace, database)

@spec use(process_identifier(), String, String) :: socket_response()

Switch to a specific namespace and database.

iex> {:ok, pid} = SurrealEx.start_link() # Include your connection options
iex> {:ok, result} = SurrealEx.use(pid, "default", "default")

# SUCCESS CASE:
{:ok, %{"id" => "1915", "result" => nil}}

# ERROR CASE:
{:error,
  %{
    "error" => %{
      "code" => -32000,
      "message" => "There was a problem with the database: [...]"
    },
    "id" => "1120"
  }
}

use(pid, namespace, database, task, opts \\ Domain.TaskOpts.default())

@spec use(process_identifier(), binary(), binary(), Task, SurrealEx.Domain.TaskOpts) ::
  socket_response()