Object Types
Object types are the primary components of a Gel schema. They are analogous to SQL tables or ORM models, and consist of properties and links.
Properties
Properties are used to attach primitive/scalar data to an object type. For the full documentation on properties, see Properties.
type Person {
email: str;
}
Using in a query:
select Person {
email
};
Links
Links are used to define relationships between object types. For the full documentation on links, see Links.
type Person {
email: str;
best_friend: Person;
}
Using in a query:
select Person {
email,
best_friend: {
email
}
};
ID
There's no need to manually declare a primary key on your object types. All
object types automatically contain a property id
of type UUID
that's
required, globally unique, readonly, and has an index on it.
The id
is assigned upon creation and cannot be changed.
Using in a query:
select Person { id };
select Person { email } filter .id = <uuid>'123e4567-e89b-...';
Abstract types
Object types can either be abstract or non-abstract. By default all object types are non-abstract. You can't create or store instances of abstract types (a.k.a. mixins), but they're a useful way to share functionality and structure among other object types.
abstract type HasName {
first_name: str;
last_name: str;
}
Inheritance
Object types can extend other object types. The extending type (AKA the subtype) inherits all links, properties, indexes, constraints, etc. from its supertypes.
abstract type HasName {
first_name: str;
last_name: str;
}
type Person extending HasName {
email: str;
best_friend: Person;
}
Using in a query:
select Person {
first_name,
email,
best_friend: {
last_name
}
};
Multiple Inheritance
Object types can extend more than one type — that's called multiple inheritance. This mechanism allows building complex object types out of combinations of more basic types.
Gel's multiple inheritance should not be confused with the multiple inheritance of C++ or Python, where the complexity usually arises from fine-grained mixing of logic. Gel's multiple inheritance is structural and allows for natural composition.
last_name: str; } abstract type HasEmail { email: str; } type Person extending HasName { type Person extending HasName, HasEmail { email: str; best_friend: Person; }
If multiple supertypes share links or properties, those properties must be of the same type and cardinality.
Defining object types
This section describes the syntax to declare object types in your schema.
Syntax
[abstract] type TypeName [extending supertype [, ...] ]
[ "{"
[ annotation-declarations ]
[ property-declarations ]
[ link-declarations ]
[ constraint-declarations ]
[ index-declarations ]
...
"}" ]
Description
This declaration defines a new object type with the following options:
- abstract
-
If specified, the created type will be abstract.
- TypeName
-
The name (optionally module-qualified) of the new type.
- extending supertype [, ...]
-
Optional clause specifying the supertypes of the new type.
Use of
extending
creates a persistent type relationship between the new subtype and its supertype(s). Schema modifications to the supertype(s) propagate to the subtype.References to supertypes in queries will also include objects of the subtype.
If the same link name exists in more than one supertype, or is explicitly defined in the subtype and at least one supertype, then the data types of the link targets must be compatible. If there is no conflict, the links are merged to form a single link in the new type.
These sub-declarations are allowed in the Type
block:
- annotation-declarations
-
Set object type annotation to a given value.
- property-declarations
-
Define a concrete property for this object type.
- link-declarations
-
Define a concrete link for this object type.
- constraint-declarations
-
Define a concrete constraint for this object type.
- index-declarations
-
Define an index for this object type.
DDL commands
This section describes the low-level DDL commands for creating, altering, and dropping object types. You typically don't need to use these commands directly, but knowing about them is useful for reviewing migrations.
Create type
Define a new object type.
[ with with-item [, ...] ]
create [abstract] type name [ extending supertype [, ...] ]
[ "{" subcommand; [...] "}" ] ;
where subcommand is one of
create annotation annotation-name := value
create link link-name ...
create property property-name ...
create constraint constraint-name ...
create index on index-expr
Description
The command create type
defines a new object type for use in the
current branch.
If name is qualified with a module name, then the type is created in that module, otherwise it is created in the current module. The type name must be distinct from that of any existing schema item in the module.
Parameters
Most sub-commands and options of this command are identical to the SDL object type declaration, with some additional features listed below:
- with with-item [, ...]
-
Alias declarations.
The
with
clause allows specifying module aliases that can be referenced by the command. See With block for more information.
The following subcommands are allowed in the create type
block:
- create annotation annotation-name := value
-
Set object type annotation-name to value.
See
create annotation
for details. - create link link-name ...
-
Define a new link for this object type. See
create link
for details. - create property property-name ...
-
Define a new property for this object type. See
create property
for details. - create constraint constraint-name ...
-
Define a concrete constraint for this object type. See
create constraint
for details. - create index on index-expr
-
Define a new index using index-expr for this object type. See
create index
for details.
Alter type
Change the definition of an object type.
[ with with-item [, ...] ]
alter type name
[ "{" subcommand; [...] "}" ] ;
[ with with-item [, ...] ]
alter type name subcommand ;
where subcommand is one of
rename to newname
extending parent [, ...]
create annotation annotation-name := value
alter annotation annotation-name := value
drop annotation annotation-name
create link link-name ...
alter link link-name ...
drop link link-name ...
create property property-name ...
alter property property-name ...
drop property property-name ...
create constraint constraint-name ...
alter constraint constraint-name ...
drop constraint constraint-name ...
create index on index-expr
drop index on index-expr
Description
The command alter type
changes the definition of an object type.
name must be a name of an existing object type, optionally qualified
with a module name.
Parameters
- with with-item [, ...]
-
Alias declarations.
The
with
clause allows specifying module aliases that can be referenced by the command. See With block for more information. - name
-
The name (optionally module-qualified) of the type being altered.
- extending parent [, ...]
-
Alter the supertype list. The full syntax of this subcommand is:
extending parent [, ...] [ first | last | before exparent | after exparent ]
This subcommand makes the type a subtype of the specified list of supertypes. The requirements for the parent-child relationship are the same as when creating an object type.
It is possible to specify the position in the parent list using the following optional keywords:
-
first
– insert parent(s) at the beginning of the parent list, -
last
– insert parent(s) at the end of the parent list, -
before <parent>
– insert parent(s) before an existing parent, -
after <parent>
– insert parent(s) after an existing parent.
-
- alter annotation annotation-name;
-
Alter object type annotation annotation-name. See
alter annotation
for details. - drop annotation annotation-name
-
Remove object type annotation-name. See
drop annotation
for details. - alter link link-name ...
-
Alter the definition of a link for this object type. See
alter link
for details. - drop link link-name
-
Remove a link item from this object type. See
drop link
for details. - alter property property-name ...
-
Alter the definition of a property item for this object type. See
alter property
for details. - drop property property-name
-
Remove a property item from this object type. See
drop property
for details. - alter constraint constraint-name ...
-
Alter the definition of a constraint for this object type. See
alter constraint
for details. - drop constraint constraint-name;
-
Remove a constraint from this object type. See
drop constraint
for details. - drop index on index-expr
-
Remove an index defined as index-expr from this object type. See
drop index
for details.
All the subcommands allowed in the create type
block are also
valid subcommands for the alter type
block.
Drop type
Remove the specified object type from the schema.
drop type name ;
Description
The command drop type
removes the specified object type from the
schema. All subordinate schema items defined on this type,
such as links and indexes, are removed as well.
Example
Remove the User
object type:
drop type User;