Types

The entire type system of EdgeDB is reflected in the e object, including scalar types, object types, and enums. These types are used in queries for thinks like casting and declaring parameters.

  1. e.str;
  2. e.bool;
  3. e.int16;
  4. e.int32;
  5. e.int64;
  6. e.float32;
  7. e.float64;
  8. e.bigint;
  9. e.decimal;
  10. e.datetime;
  11. e.duration;
  12. e.bytes;
  13. e.json;
  14. e.cal.local_datetime;
  15. e.cal.local_date;
  16. e.cal.local_time;
  17. e.cal.relative_duration;
  18. e.cal.date_duration;
  19. e.Movie; // user-defined object type
  20. e.Genre; // user-defined enum

You can construct array and tuple types, as in EdgeQL.

  1. e.array(e.bool);
  2. // array<bool>
  3. e.tuple([e.str, e.int64]);
  4. // tuple<str, int64>
  5. e.tuple({
  6. name: e.str,
  7. age: e.int64
  8. });
  9. // tuple<name: str, age: int64>

Casting

These types can be used to cast one expression to another type.

  1. e.cast(e.json, e.int64('123'));
  2. // <json>'123'
  3. e.cast(e.duration, e.str('127 hours'));
  4. // <duration>'127 hours'

Scalar types like e.str serve a dual purpose. They can be used as functions to instantiate literals (e.str("hi")) or used as variables (e.cast(e.str, e.int64(123))).

Custom literals

You can use e.literal to create literals corresponding to collection types like tuples, arrays, and primitives. The first argument expects a type, the second expects a value of that type.

  1. e.literal(e.str, "sup");
  2. // equivalent to: e.str("sup")
  3. e.literal(e.array(e.int16), [1, 2, 3]);
  4. // <array<int16>>[1, 2, 3]
  5. e.literal(e.tuple([e.str, e.int64]), ['baz', 9000]);
  6. // <tuple<str, int64>>("Goku", 9000)
  7. e.literal(
  8. e.tuple({name: e.str, power_level: e.int64}),
  9. {name: 'Goku', power_level: 9000}
  10. );
  11. // <tuple<name: str, power_level: bool>>("asdf", false)

Parameters

Types are also necessary for declaring query parameters.

Pass strongly-typed parameters into your query with e.params.

  1. const query = e.params({name: e.str}, params =>
  2. e.op(e.str("Yer a wizard, "), "++", params.name)
  3. );
  4. await query.run(client, {name: "Harry"});
  5. // => "Yer a wizard, Harry"

The full documentation on using parameters is here.

Polymorphism

Types are also used to write polymorphic queries. For full documentation on this, see Polymorphism in the e.select documentation.

  1. e.select(e.Content, content => ({
  2. title: true,
  3. ...e.is(e.Movie, { release_year: true }),
  4. ...e.is(e.TVShow, { num_seasons: true }),
  5. }));