A functional builder DSL for WOQL (Web Object Query Language).
WOQL is TerminusDB's Datalog-based query language. This module provides a
small but solid set of composable functions that build a TerminusDB.WOQL.Query
struct, which can be serialized to the JSON-LD wire format via to_jsonld/1
and executed via TerminusDB.WOQL.execute/3.
This is WOQL DSL v0.1 - a focused subset covering the most common patterns. Future releases will extend the vocabulary.
Design
The DSL is purely functional (no macros). Each function returns a
%WOQL.Query{} struct and composes by nesting, mirroring the recommended
functional WOQL style. Variables are plain strings using the v:Name
convention.
Supported vocabulary (v0.1)
| Function | WOQL JSON-LD type |
|---|---|
triple/3 | Triple |
and_/1 | And |
or_/1 | Or |
eq/2 | Equals |
select/2 | Select |
read_document/2 | ReadDocument |
type_of/2 | TypeOf |
Quick start
import TerminusDB.WOQL
query =
and_([
triple("v:Person", "rdf:type", "@schema:Person"),
triple("v:Person", "name", "v:Name")
])
jsonld = TerminusDB.WOQL.to_jsonld(query)
# Execute against a database
config = TerminusDB.Config.new(endpoint: "http://localhost:6363")
config = TerminusDB.Config.with_database(config, "mydb")
{:ok, result} = TerminusDB.WOQL.execute(config, query)
Summary
Functions
Builds an And conjunction of sub-queries.
Builds an Equals unification: left equals right.
Executes a WOQL query against the database scoped in config.
Executes a WOQL query, or raises TerminusDB.Error.
Deserializes a JSON-LD WOQL query back into a WOQL.Query struct.
Builds an Or disjunction of sub-queries.
Builds a ReadDocument that reads a document by ID into a variable.
Builds a Select that projects the given variables from a sub-query.
Serializes a WOQL.Query to the JSON-LD wire format expected by the
/api/woql endpoint.
Builds a Triple pattern: subject, predicate, object.
Builds a TypeOf that unifies the type of a node with a variable.
Types
Functions
Builds an And conjunction of sub-queries.
Examples
iex> q = TerminusDB.WOQL.and_([
...> TerminusDB.WOQL.triple("v:S", "rdf:type", "v:T"),
...> TerminusDB.WOQL.eq("v:T", "Person")
...> ])
iex> q.op
:and
Builds an Equals unification: left equals right.
Examples
iex> q = TerminusDB.WOQL.eq("v:Name", "Alice")
iex> q.op
:eq
@spec execute(TerminusDB.Config.t(), t(), keyword()) :: {:ok, map()} | {:error, TerminusDB.Error.t()}
Executes a WOQL query against the database scoped in config.
Returns {:ok, result} where result is a map containing bindings (a list
of maps, one per solution), or {:error, TerminusDB.Error.t()}.
Options
:author- commit author (for write queries).:message- commit message (for write queries).:all_witnesses- check for all errors (defaultfalse).:organization- overridesconfig.organization.:repo- overridesconfig.repo.:branch- overridesconfig.branch.
Examples
iex> config = TerminusDB.Config.new(
...> endpoint: "http://localhost:6363",
...> adapter: fn req ->
...> {req, Req.Response.new(status: 200, body: %{"bindings" => [%{"Name" => "Alice"}]})}
...> end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> q = TerminusDB.WOQL.select(["v:Name"],
...> TerminusDB.WOQL.and_([TerminusDB.WOQL.triple("v:P", "name", "v:Name")])
...> )
iex> {:ok, result} = TerminusDB.WOQL.execute(config, q)
iex> result["bindings"]
[%{"Name" => "Alice"}]
@spec execute!(TerminusDB.Config.t(), t(), keyword()) :: map()
Executes a WOQL query, or raises TerminusDB.Error.
Examples
iex> config = TerminusDB.Config.new(
...> endpoint: "http://localhost:6363",
...> adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"bindings" => []})} end
...> ) |> TerminusDB.Config.with_database("mydb")
iex> q = TerminusDB.WOQL.triple("v:S", "p", "v:O")
iex> TerminusDB.WOQL.execute!(config, q)
%{"bindings" => []}
Deserializes a JSON-LD WOQL query back into a WOQL.Query struct.
Examples
iex> jsonld = %{"@type" => "Triple", "subject" => %{"@type" => "NodeValue", "variable" => "S"}, "predicate" => %{"@type" => "NodeValue", "node" => "name"}, "object" => %{"@type" => "DataValue", "variable" => "N"}}
iex> q = TerminusDB.WOQL.from_jsonld(jsonld)
iex> q.op
:triple
Builds an Or disjunction of sub-queries.
Examples
iex> q = TerminusDB.WOQL.or_([
...> TerminusDB.WOQL.eq("v:Name", "Alice"),
...> TerminusDB.WOQL.eq("v:Name", "Bob")
...> ])
iex> q.op
:or
Builds a ReadDocument that reads a document by ID into a variable.
Examples
iex> q = TerminusDB.WOQL.read_document("Person/Alice", "v:Doc")
iex> q.op
:read_document
Builds a Select that projects the given variables from a sub-query.
vars is a list of variable names (e.g. ["v:Name", "v:Person"]).
Examples
iex> q = TerminusDB.WOQL.select(["v:Name"],
...> TerminusDB.WOQL.and_([
...> TerminusDB.WOQL.triple("v:Person", "name", "v:Name")
...> ])
...> )
iex> q.op
:select
Serializes a WOQL.Query to the JSON-LD wire format expected by the
/api/woql endpoint.
Examples
iex> q = TerminusDB.WOQL.triple("v:S", "name", "v:N")
iex> jsonld = TerminusDB.WOQL.to_jsonld(q)
iex> jsonld["@type"]
"Triple"
Builds a Triple pattern: subject, predicate, object.
Any argument can be a variable ("v:Name") or a constant.
Examples
iex> q = TerminusDB.WOQL.triple("v:Person", "name", "v:Name")
iex> q.op
:triple
Builds a TypeOf that unifies the type of a node with a variable.
Examples
iex> q = TerminusDB.WOQL.type_of("v:Person", "v:Type")
iex> q.op
:type_of