# `TypeDB.Database`
[🔗](https://github.com/NoeticEcho/TypedbEx/blob/v0.11.0/typedb/lib/typedb/database.ex#L1)

Database administration.

    {:ok, names} = TypeDB.Database.list(conn)
    :ok = TypeDB.Database.create(conn, "social")
    {:ok, schema} = TypeDB.Database.schema(conn, "social")
    :ok = TypeDB.Database.delete(conn, "social")

`delete/3` destroys the database and everything in it, irreversibly.

Every function here takes `:timeout` and `:deadline` as trailing options,
bounding one attempt and the whole call respectively — a schema fetch over a
large database is the call most likely to want more than the connection's
default.

# `create`

```elixir
@spec create(TypeDB.Connection.t(), String.t(), keyword()) ::
  :ok | {:error, TypeDB.Error.t()}
```

Creates a database.

Creating a database that already exists is a no-op on TypeDB 3.x: the call
succeeds and the existing data is left untouched. `create_if_not_exists/2`
states that intent explicitly and also copes with a server that rejects the
duplicate.

# `create!`

```elixir
@spec create!(TypeDB.Connection.t(), String.t(), keyword()) :: :ok
```

Creates a database, raising on failure.

# `create_if_not_exists`

```elixir
@spec create_if_not_exists(TypeDB.Connection.t(), String.t(), keyword()) ::
  :ok | {:error, TypeDB.Error.t()}
```

Creates a database unless it already exists.

Two callers racing here is safe: whoever loses sees the database already
present and returns `:ok`.

# `create_if_not_exists!`

```elixir
@spec create_if_not_exists!(TypeDB.Connection.t(), String.t(), keyword()) :: :ok
```

Creates a database unless it already exists, raising on failure.

# `delete`

```elixir
@spec delete(TypeDB.Connection.t(), String.t(), keyword()) ::
  :ok | {:error, TypeDB.Error.t()}
```

Deletes a database and all of its data. This cannot be undone.

Deleting a database that does not exist is an error (`DBD1`), unlike
`create/2` — the asymmetry is TypeDB's, not this driver's.

# `delete!`

```elixir
@spec delete!(TypeDB.Connection.t(), String.t(), keyword()) :: :ok
```

Deletes a database, raising on failure.

# `exists?`

```elixir
@spec exists?(TypeDB.Connection.t(), String.t(), keyword()) :: boolean()
```

Returns whether a database exists.

Raises `TypeDB.Error` for anything other than a clean "not found" — an
unreachable server, a rejected token, a 500. A boolean cannot express "I could
not ask", and answering `false` to that question is the answer that makes a
caller do the wrong thing: `unless exists?(conn, x), do: create(conn, x)` would
try to create while the server is down. Use `get/2` if you would rather branch
on the error yourself.

# `get`

```elixir
@spec get(TypeDB.Connection.t(), String.t(), keyword()) ::
  {:ok, String.t()} | {:error, TypeDB.Error.t()}
```

Returns the name of a database, or `{:error, %TypeDB.Error{status: 404}}` if it
does not exist.

# `get!`

```elixir
@spec get!(TypeDB.Connection.t(), String.t(), keyword()) :: String.t()
```

Returns a database name, raising on failure.

# `list`

```elixir
@spec list(
  TypeDB.Connection.t(),
  keyword()
) :: {:ok, [String.t()]} | {:error, TypeDB.Error.t()}
```

Lists the names of all databases the authenticated user can see.

# `list!`

```elixir
@spec list!(
  TypeDB.Connection.t(),
  keyword()
) :: [String.t()]
```

Lists database names, raising on failure.

# `schema`

```elixir
@spec schema(TypeDB.Connection.t(), String.t(), keyword()) ::
  {:ok, String.t()} | {:error, TypeDB.Error.t()}
```

Returns the full schema of a database as TypeQL source: types *and* functions.

Round-trips: feeding the result to a `define` query in an empty database
reproduces the schema.

# `schema!`

```elixir
@spec schema!(TypeDB.Connection.t(), String.t(), keyword()) :: String.t()
```

Returns the full schema of a database, raising on failure.

# `type_schema`

```elixir
@spec type_schema(TypeDB.Connection.t(), String.t(), keyword()) ::
  {:ok, String.t()} | {:error, TypeDB.Error.t()}
```

Returns only the type definitions of a database's schema, without functions.

# `type_schema!`

```elixir
@spec type_schema!(TypeDB.Connection.t(), String.t(), keyword()) :: String.t()
```

Returns a database's type definitions, raising on failure.

---

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