# `TerminusDB.Prefix`
[🔗](https://github.com/thanos/terminusdb-client-elixir/blob/v0.3.3/lib/terminus_db/prefix.ex#L1)

Prefix management API for TerminusDB.

Wraps the `/api/prefix/{path}` and `/api/prefixes/{path}` endpoints for
managing custom prefix mappings in a database.

All functions require a `TerminusDB.Config` scoped to a database.

## Quick start

    config =
      TerminusDB.Config.new(endpoint: "http://localhost:6363")
      |> TerminusDB.Config.with_database("mydb")

    {:ok, uri} = TerminusDB.Prefix.get(config, "ex")
    {:ok, _} = TerminusDB.Prefix.add(config, "ex", "http://example.org/")
    {:ok, _} = TerminusDB.Prefix.update(config, "ex", "http://example.com/")
    {:ok, _} = TerminusDB.Prefix.delete(config, "ex")

# `prefix_opt`

```elixir
@type prefix_opt() :: {:organization, String.t()} | {:repo, String.t()}
```

# `add`

```elixir
@spec add(TerminusDB.Config.t(), String.t(), String.t(), [prefix_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}
```

Adds a new prefix mapping.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, resp} = TerminusDB.Prefix.add(config, "ex", "http://example.org/")
    iex> resp["api:status"]
    "api:success"

# `add!`

```elixir
@spec add!(TerminusDB.Config.t(), String.t(), String.t(), [prefix_opt()]) :: map()
```

Adds a new prefix mapping, or raises.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Prefix.add!(config, "ex", "http://example.org/")
    %{"api:status" => "api:success"}

# `all`

```elixir
@spec all(TerminusDB.Config.t(), [prefix_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}
```

Gets all prefix mappings for the database.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"@base" => "terminusdb:///data/", "@schema" => "terminusdb:///schema#"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, prefixes} = TerminusDB.Prefix.all(config)
    iex> prefixes["@schema"]
    "terminusdb:///schema#"

# `all!`

```elixir
@spec all!(TerminusDB.Config.t(), [prefix_opt()]) :: map()
```

Gets all prefix mappings, or raises.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"@base" => "terminusdb:///data/", "@schema" => "terminusdb:///schema#"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Prefix.all!(config)["@schema"]
    "terminusdb:///schema#"

# `delete`

```elixir
@spec delete(TerminusDB.Config.t(), String.t(), [prefix_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}
```

Deletes a prefix mapping.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, resp} = TerminusDB.Prefix.delete(config, "ex")
    iex> resp["api:status"]
    "api:success"

# `delete!`

```elixir
@spec delete!(TerminusDB.Config.t(), String.t(), [prefix_opt()]) :: map()
```

Deletes a prefix mapping, or raises.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Prefix.delete!(config, "ex")
    %{"api:status" => "api:success"}

# `get`

```elixir
@spec get(TerminusDB.Config.t(), String.t(), [prefix_opt()]) ::
  {:ok, String.t()} | {:error, TerminusDB.Error.t()}
```

Gets a single prefix IRI by name.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:prefix_uri" => "http://example.org/"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, uri} = TerminusDB.Prefix.get(config, "ex")
    iex> uri
    "http://example.org/"

# `get!`

```elixir
@spec get!(TerminusDB.Config.t(), String.t(), [prefix_opt()]) :: String.t()
```

Gets a single prefix IRI by name, or raises.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:prefix_uri" => "http://example.org/"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Prefix.get!(config, "ex")
    "http://example.org/"

# `update`

```elixir
@spec update(TerminusDB.Config.t(), String.t(), String.t(), [prefix_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}
```

Updates an existing prefix mapping.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, resp} = TerminusDB.Prefix.update(config, "ex", "http://example.com/")
    iex> resp["api:status"]
    "api:success"

# `update!`

```elixir
@spec update!(TerminusDB.Config.t(), String.t(), String.t(), [prefix_opt()]) :: map()
```

Updates an existing prefix mapping, or raises.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Prefix.update!(config, "ex", "http://example.com/")
    %{"api:status" => "api:success"}

# `upsert`

```elixir
@spec upsert(TerminusDB.Config.t(), String.t(), String.t(), [prefix_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}
```

Creates or updates a prefix mapping (upsert).

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, resp} = TerminusDB.Prefix.upsert(config, "ex", "http://example.org/")
    iex> resp["api:status"]
    "api:success"

# `upsert!`

```elixir
@spec upsert!(TerminusDB.Config.t(), String.t(), String.t(), [prefix_opt()]) :: map()
```

Creates or updates a prefix mapping (upsert), or raises.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Prefix.upsert!(config, "ex", "http://example.org/")
    %{"api:status" => "api:success"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
