Skip to main content

Packages

Packages are a mechanism to reuse code in RAW SQL.

For example, assume a file at github://demo-org/demo-repository/package1.rql with the following contents:

f(v: int) := "v times two is " + (v * 2);

Once the package is created, it can be imported and used in a query as shown:

Example:

FROM `github://demo-org/demo-repository/package1.rql` IMPORT f;

f(1) // Result is: "v times two is 2"

If required, the declarations being imported from the packaged can be aliased using AS.

Example:

FROM `github://demo-org/demo-repository/package1.rql` IMPORT f AS times_two;

times_two(1) // Result is: "v times two is 2"

Packages can contain multiple top-level function definitions, and, optionally, a top-level expression.

For instance, assume the package github://demo-org/demo-repository/package2.rql with the following contents:

times2(v: int) := v * 2;

times4(v: int) := v * 4;

"hello!"

This package contains two top-level functions (times2 and times4) and a top-level expression (the string “hello!”).

As show above, to import one of the declarations in the package use the FROM <package name> IMPORT <declaration name> [AS <final name>].

To import the top-level expression, use IMPORT <package name> and then refer to <package name> in the query.

Example:

IMPORT `github://demo-org/demo-repository/package2.rql`;

"Package says: " + `github://demo-org/demo-repository/package2.rql` // Result is: "Package says: hello!"

Aliases can be used in IMPORT.

Example:

FROM `github://demo-org/demo-repository/package2.rql` IMPORT a, b;

IMPORT `github://demo-org/demo-repository/package2.rql` AS c;

(c, a(2), b(2)) // Result is: ("hello!", 4, 8)

Any valid URL can be used as a location, but note that URLs must be escaped with back ticks as shown in the examples. Refer to Locations for the locations supported and the URL syntax to use.