TerminusDB.Schema (terminusdb_ex v0.3.3)

Copy Markdown View Source

Schema frame API for TerminusDB.

Wraps the /api/schema endpoint, which returns the class frame for a class or all classes for a database's schema. A "frame" is a JSON-LD description of a schema class: its properties, types, key strategy, and documentation.

All functions require a TerminusDB.Config scoped to a database (via TerminusDB.Config.with_database/2).

Quick start

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

# Get the frame for a specific class
{:ok, frame} = TerminusDB.Schema.frame(config, "Person")
# => %{"@type" => "Class", "name" => "xsd:string", ...}

# Get all class frames
{:ok, all} = TerminusDB.Schema.all(config)
# => %{"Person" => %{"@type" => "Class", ...}, "Room" => %{...}}

Summary

Functions

Returns all class frames for the database's schema.

Returns all class frames, or raises TerminusDB.Error.

Returns the class frame for a specific class class_name, or all class frames if class_name is nil.

Types

frame_opt()

@type frame_opt() ::
  {:compress_ids, boolean()}
  | {:expand_abstract, boolean()}
  | {:organization, String.t()}

Functions

all(config, opts \\ [])

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

Returns all class frames for the database's schema.

Equivalent to frame(config, nil, opts).

Examples

iex> config = TerminusDB.Config.new(
...>   endpoint: "http://localhost:6363",
...>   adapter: fn req ->
...>     {req, Req.Response.new(status: 200, body: %{"@context" => %{"@type" => "Context"}, "Person" => %{"@type" => "Class"}})}
...>   end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> {:ok, all} = TerminusDB.Schema.all(config)
iex> Map.keys(all)
["Person"]

all!(config, opts \\ [])

@spec all!(TerminusDB.Config.t(), [frame_opt()]) :: map()

Returns all class frames, or raises TerminusDB.Error.

Examples

iex> config = TerminusDB.Config.new(
...>   endpoint: "http://localhost:6363",
...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"Person" => %{"@type" => "Class"}})} end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> TerminusDB.Schema.all!(config)
%{"Person" => %{"@type" => "Class"}}

frame(config, class_name \\ nil, opts \\ [])

@spec frame(TerminusDB.Config.t(), String.t() | nil, [frame_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}

Returns the class frame for a specific class class_name, or all class frames if class_name is nil.

Options

  • :compress_ids — compress the URLs returned using prefixes (default true).
  • :expand_abstract — expand abstract classes into lists of concrete classes in frame options (default true).
  • :organization — overrides config.organization.

Examples

Get the frame for a specific class:

iex> config = TerminusDB.Config.new(
...>   endpoint: "http://localhost:6363",
...>   adapter: fn req ->
...>     {req, Req.Response.new(status: 200, body: %{"@type" => "Class", "name" => "xsd:string"})}
...>   end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> {:ok, frame} = TerminusDB.Schema.frame(config, "Person")
iex> frame["name"]
"xsd:string"

Get all class frames (pass nil or omit class_name):

iex> config = TerminusDB.Config.new(
...>   endpoint: "http://localhost:6363",
...>   adapter: fn req ->
...>     {req, Req.Response.new(status: 200, body: %{"Person" => %{"@type" => "Class"}, "Room" => %{"@type" => "Class"}})}
...>   end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> {:ok, all} = TerminusDB.Schema.frame(config)
iex> Map.keys(all) |> Enum.sort()
["Person", "Room"]

frame!(config, class_name \\ nil, opts \\ [])

@spec frame!(TerminusDB.Config.t(), String.t() | nil, [frame_opt()]) :: map()

Returns the class frame, or raises TerminusDB.Error.

Examples

iex> config = TerminusDB.Config.new(
...>   endpoint: "http://localhost:6363",
...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"@type" => "Class", "name" => "xsd:string"})} end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> TerminusDB.Schema.frame!(config, "Person")
%{"@type" => "Class", "name" => "xsd:string"}