Officially out now: The TypeDB 3.0 Roadmap >>

Java query builder

The Java query builder library can be used to programmatically construct TypeQL queries with native Java code.

Import

To use TypeQL, add it as a dependency to the pom.xml file.

The latest version of the typeql-lang can be found in the Vaticle’s public Maven repository.

Replace the {version} placeholder with the exact version of the library.

<repositories>
    <repository>
        <id>repo.typedb.com</id>
        <url>https://repo.typedb.com/public/public-release/maven/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.vaticle.typeql</groupId>
        <artifactId>typeql-lang</artifactId>
        <version>{version}</version>
    </dependency>
</dependencies>

Then, import TypeQL in the source code:

import com.vaticle.typeql.lang.TypeQL;

You are now ready to construct TypeQL queries, using the methods available for the TypeQL class.

Usage

Using the Java query builder library is quite simple as it produces a TypeQL string. See the examples below.

Example 1: A get query

TypeQLGet getQuery = TypeQL.match(
        cVar("u").isa("user").has("full-name", "Kevin Morrison"),
        cVar("p").rel(cVar("u")).rel(cVar("pa")).isa("permission"),
        cVar("o").isa("object").has("path", cVar("fp")),
        cVar("pa").rel(cVar("o")).rel(cVar("va")).isa("access")
).get(cVar("fp"));

As the result of the above example you should get a TypeQL query string in a getQuery variable that can be used for a get query. The resulted string should be similar to the following:

String getQuery = "match $u isa user, has full-name 'Kevin Morrison'; $p($u, $pa) isa permission; " +
                  "$o isa object, has path $fp; $pa($o, $va) isa access; get $fp;";

In versions prior to 2.18.0 the var() keyword was used instead of cVar() as there were no value variables.

Send the query:

readTransaction.query().get(getQuery);

Example 2: A get query with additional parameters

The following example showcases the usage of sorting, offsetting and limiting a get query.

TypeQLGet getQuery = TypeQL.match(
        cVar("u").isa("user").has("full-name", "Kevin Morrison"),
        cVar("p").rel(cVar("u")).rel(cVar("pa")).isa("permission"),
        cVar("o").isa("object").has("path", cVar("fp")),
        cVar("pa").rel(cVar("o")).rel(cVar("va")).isa("access"),
        cVar("va").isa("action").has("name", "view_file")
).get(cVar("fp")).sort(cVar("fp")).offset(0).limit(5);

Example 3: Value variables

TypeQLGet query = match(
        cVar("x").isa("triangle").has("base", cVar("b")).has("height", cVar("h")),
        vVar("area").assign(Expression.constant(0.5).mul(cVar("b").mul(cVar("h"))))))
);

Example 4: An insert query

The following example showcases the usage of insert query.

insertQuery = TypeQL.match(
        cVar("f").isa("file").has("path", filepath),
        cVar("vav").isa("action").has("name", "view_file")
)
.insert(cVar("pa").rel(cVar("vav")).rel(cVar("f")).isa("access"));