Types
The properties of e that corresond to types—e.str, e.int64, etc— serve a dual purpose.
- They can be used as functions to instantiate literals:
e.str("hi"). - They are also used to represent the type itself for certain type operations like casting and declaring parameters.
Reflected types
The entire type system of EdgeDB is reflected in the e object, including scalar types, object types, and enums.
1. e.str;
2. e.bool;
3. e.int16;
4. e.int32;
5. e.int64;
6. e.float32;
7. e.float64;
8. e.bigin;
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;
19. e.Movie; // user-defined object type
20. e.Genre; // user-defined enum
Collection types
Construct array and tuple types.
1. e.array(e.bool);
2. // array<bool>
4. e.tuple([e.str, e.int64]);
5. // tuple<str, int64>
7. e.tuple({
8. name: e.str,
9. age: e.int64
10. });
11. // 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'
4. e.cast(e.duration, e.str('127 hours'));
5. // <duration>'127 hours'
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")
4. e.literal(e.array(e.int16), [1, 2, 3]);
5. // <array<int16>>[1, 2, 3]
7. e.literal(e.tuple([e.str, e.int64]), ['baz', 9000]);
8. // <tuple<str, int64>>("Goku", 9000)
10. e.literal(
11. e.tuple({name: e.str, power_level: e.int64}),
12. {name: 'Goku', power_level: 9000}
13. );
14. // <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. );
5. await query.run(client, {name: "Harry"});
6. // => "Yer a wizard, Harry"
The full documentation on using parameters is here.
