Edgedb Enums
[tr] Türkçe Oku 2023-04-11
Edgedb allows users to define enums and use them in data modeling. Enums are one of the data types we use to store data. An enum is a data structure that contains a specific set of constant values. These constants can be used to limit the values of a field in the data model. This can help prevent storing incorrect or invalid data in the model and provide application developers with a more precise and secure data modeling experience.
In Edgedb, enums are defined as a special data type, and the following syntax is used to define an enum
enum <enum_name> {
<enum_value_1> := <value_1>,
<enum_value_2> := <value_2>,
...
}
In this example, the <enum_name>
element specifies the name of the enum to be defined. The <enum_value_1>
and <enum_value_2>
elements specify the names of the enum constants and their values are specified using the := symbol.
The following example defines an enum called user_role
, which contains three different constant values: admin, editor, and viewer:
enum user_role {
admin := 'admin',
editor := 'editor',
viewer := 'viewer'
}
Enum constants can be used as the default value of a field, and that field can only take the values of the enum constants. The following example defines an object type called User, which includes a field called role. This field takes a constant value from the user_role enum:
type User {
required property name -> str;
required property email -> str;
property role -> user_role;
}
This example specifies that the role field of the User object type can only take the values of admin, editor, or viewer, which are the constants of the user_role enum.
Enums are a very useful tool to ensure consistency in data modeling operations. Thanks to its support for enums, Edgedb can prevent storing incorrect data and provide application developers with a more precise and secure data modeling experience, making the data easier to understand.