Globals

⚠️ Only available in EdgeDB 2.0 or later.

This section describes the DDL commands pertaining to global variables.

Create global

Declare a new global variable.

  1. [ with with-item [, ...] ]
  2. create [{required | optional}] [single]
  3. global name -> type
  4. [ "{" subcommand; [...] "}" ] ;
  5. Computed global variable form:
  6. [ with with-item [, ...] ]
  7. create [{required | optional}] [{single | multi}]
  8. global name := expression;
  9. where subcommand is one of
  10. set default := expression
  11. create annotation annotation-name := value

Description

There two different forms of global declaration, as shown in the syntax synopsis above. The first form is for defining a global variable that can be set in a session. The second form is not directly set, but instead it is computed based on an expression, potentially deriving its value from other global variables.

Parameters

Most sub-commands and options of this command are identical to the SDL global variable declaration. The following subcommands are allowed in the create global block:

set default := expression

Specifies the default value for the global variable as an EdgeQL expression. The default value is used by the session if the value was not explicitly specified or by the reset command.

create annotation annotation-name := value

Set global variable annotation-name to value.

See create annotation for details.

Examples

Define a new global property current_user_id:

  1. create global current_user_id -> uuid;

Define a new computed global property current_user based on the previously defined current_user_id:

  1. create global current_user := (
  2. select User filter .id = global current_user_id
  3. );

Alter global

Change the definition of a global variable.

  1. [ with with-item [, ...] ]
  2. alter global name
  3. [ "{" subcommand; [...] "}" ] ;
  4. where subcommand is one of
  5. set default := expression
  6. reset default
  7. rename to newname
  8. set required
  9. set optional
  10. reset optionalily
  11. set single
  12. set multi
  13. reset cardinality
  14. set type typename reset to default
  15. using (computed-expr)
  16. create annotation annotation-name := value
  17. alter annotation annotation-name := value
  18. drop annotation annotation-name

Description

The command alter global changes the definition of a global variable.

Parameters

name

The name of the global variable to modify.

The following subcommands are allowed in the alter global block:

reset default

Remove the default value from this global variable.

rename to newname

Change the name of the global variable to newname.

set required

Make the global variable required.

set optional

Make the global variable no longer required (i.e. make it optional).

reset optionalily

Reset the optionality of the global variable to the default value (optional).

set single

Change the maximum cardinality of the global variable to one.

set multi

Change the maximum cardinality of the global variable set to greater than one. Only valid for computed global variables.

reset cardinality

Reset the maximum cardinality of the global variable to the default value (single), or, if the property is computed, to the value inferred from its expression.

set type typename reset to default

Change the type of the global variable to the specified typename. The reset to default clause is mandatory and it specifies that the variable will be reset to its default value after this command.

using (computed-expr)

Change the expression of a computed global variable. Only valid for computed variables.

alter annotation annotation-name;

Alter global variable annotation annotation-name. See alter annotation for details.

drop annotation annotation-name;

Remove global variable annotation-name. See drop annotation for details.

All the subcommands allowed in the create global block are also valid subcommands for alter global block.

Examples

Set the description annotation of global variable current_user:

  1. alter global current_user
  2. create annotation description :=
  3. 'Current User as specified by the global ID';

Make the current_user_id global variable required:

  1. alter global current_user_id {
  2. set required;
  3. # A required global variable MUST have a default value.
  4. set default := <uuid>'00ea8eaa-02f9-11ed-a676-6bd11cc6c557';
  5. }

Drop global

Remove a global variable from the schema.

  1. [ with with-item [, ...] ]
  2. drop global name ;

Description

The command drop global removes the specified global variable from the schema.

Example

Remove the current_user global variable:

  1. drop global current_user;

See also

Schema > Globals

SDL > Globals