Search...
ctrl/
Light
Dark
System
Sign in

Aliases​

You can think of aliases as a way to give schema names to arbitrary EdgeQL expressions. You can later refer to aliases in queries and in other aliases.

Aliases are functionally equivalent to expression aliases defined in EdgeQL statements in with block, but are available to all queries using the schema and can be introspected.

Like computed properties, the aliased expression is evaluated on the fly whenever the alias is referenced.

Copy
# in your schema:
alias digits := {0,1,2,3,4,5,6,7,8,9};

Later, in some query:

Copy
select count(digits);

The name of a given object type (e.g. User) is itself a pointer to the set of all User objects. After declaring the alias below, you can use User and UserAlias interchangeably:

Copy
alias UserAlias := User;

Object type aliases can include a shape that declares additional computed properties or links:

Copy
type Post {
  required title: str;
}

alias PostWithTrimmedTitle := Post {
  trimmed_title := str_trim(.title)
}

Later, in some query:

Copy
select PostWithTrimmedTitle {
  trimmed_title
};

Aliases can correspond to any arbitrary EdgeQL expression, including entire queries.

Copy
# Tuple alias
alias Color := ("Purple", 128, 0, 128);

# Named tuple alias
alias GameInfo := (
  name := "Li Europan Lingues",
  country := "Iceland",
  date_published := 2023,
  creators := (
    (name := "Bob Bobson", age := 20),
    (name := "Trina Trinadóttir", age := 25),
  ),
);

type BlogPost {
  required title: str;
  required is_published: bool;
}

# Query alias
alias PublishedPosts := (
  select BlogPost
  filter .is_published = true
);

All aliases are reflected in the database's built-in GraphQL schema.

Define a new alias corresponding to the more explicit DDL commands.

alias alias-name := alias-expr ;

alias alias-name "{"
    using alias-expr;
    [ annotation-declarations ]
"}" ;

Where:

alias-name

The name (optionally module-qualified) of an alias to be created.

alias-expr

The aliased expression. Must be a Stable EdgeQL expression.

The valid SDL sub-declarations are listed below:

annotation-declarations

Set alias annotation to a given value.

This section describes the low-level DDL commands for creating and dropping aliases. You typically don't need to use these commands directly, but knowing about them is useful for reviewing migrations.

Define a new alias in the schema.

[ with with-item [, ...] ]
create alias alias-name := alias-expr ;

[ with with-item [, ...] ]
create alias alias-name "{"
    using alias-expr;
    [ create annotation attr-name := attr-value; ... ]
"}" ;

where with-item is:

[ module-alias := ] module module-name

Most sub-commands and options of this command are identical to the SDL alias declaration, with some additional features listed below:

[ module-alias := ] module module-name

An optional list of module alias declarations to be used in the alias definition.

create annotation annotation-name := value;

An optional list of annotation values for the alias. See create annotation for details.

Create a new alias:

Copy
create alias Superusers := (
    select User filter User.groups.name = 'Superusers'
);

Remove an alias from the schema.

[ with with-item [, ...] ]
drop alias alias-name ;
alias-name

The name (optionally qualified with a module name) of an existing expression alias.

Remove an alias:

Copy
drop alias SuperUsers;