Object types​

Define an abstract type:

  1. abstract type HasImage {
  2. # just a URL to the image
  3. required property image -> str;
  4. index on (.image);
  5. }

Define a type extending from the abstract:

  1. type User extending HasImage {
  2. required property name -> str {
  3. # Ensure unique name for each User.
  4. constraint exclusive;
  5. }
  6. }

Define a type with constraints and defaults for properties:

  1. type Review {
  2. required property body -> str;
  3. required property rating -> int64 {
  4. constraint min_value(0);
  5. constraint max_value(5);
  6. }
  7. required property flag -> bool {
  8. default := False;
  9. }
  10. required link author -> User;
  11. required link movie -> Movie;
  12. required property creation_time -> datetime {
  13. default := datetime_current();
  14. }
  15. }

Define a type with a property that is computed from the combination of the other properties:

  1. type Person extending HasImage {
  2. required property first_name -> str {
  3. default := '';
  4. }
  5. required property middle_name -> str {
  6. default := '';
  7. }
  8. required property last_name -> str;
  9. property full_name :=
  10. (
  11. (
  12. (.first_name ++ ' ')
  13. if .first_name != '' else
  14. ''
  15. ) ++
  16. (
  17. (.middle_name ++ ' ')
  18. if .middle_name != '' else
  19. ''
  20. ) ++
  21. .last_name
  22. );
  23. property bio -> str;
  24. }

Define an abstract links:

  1. abstract link crew {
  2. # Provide a way to specify some "natural"
  3. # ordering, as relevant to the movie. This
  4. # may be order of importance, appearance, etc.
  5. property list_order -> int64;
  6. }
  7. abstract link directors extending crew;
  8. abstract link actors extending crew;

Define a type using abstract links and a computed property that aggregates values from another linked type:

  1. type Movie extending HasImage {
  2. required property title -> str;
  3. required property year -> int64;
  4. # Add an index for accessing movies by title and year,
  5. # separately and in combination.
  6. index on (.title);
  7. index on (.year);
  8. index on ((.title, .year));
  9. property description -> str;
  10. multi link directors extending crew -> Person;
  11. multi link actors extending crew -> Person;
  12. property avg_rating := math::mean(.<movie[is Review].rating);
  13. }

Define an auto-incrementing scalar type and an object type using it as a property:

  1. scalar type TicketNo extending sequence;
  2. type Ticket {
  3. property number -> TicketNo {
  4. constraint exclusive;
  5. }
  6. }

See also

Schema > Object types

SDL > Object types

DDL > Object types

Introspection > Object types