Expressions​

The query builder is exact what it sounds like: a way to declare EdgeQL queries with code. By convention, it’s imported from the directory where it was generated as a single, default import called e.

  1. import e from "./dbschema/edgeql-js";

The e variable provides everything you need to build any EdgeQL query. All EdgeQL commands, standard library functions, and types are available as properties on e.

  1. import e from "./dbschema/edgeql-js";
  2. // commands
  3. e.select;
  4. e.insert;
  5. e.update;
  6. e.delete;
  7. // types
  8. e.str;
  9. e.bool;
  10. e.int64;
  11. e.duration;
  12. e.cal.local_date;
  13. // functions
  14. e.str_upper;
  15. e.len;
  16. e.count;
  17. e.math.stddev;

These building blocks are used to define expressions. Everything you create using the query builder is an expression. Expressions have a few things in common.

Expressions produce EdgeQL. Below is a number of expressions and the EdgeQL they produce. (The actual EdgeQL the create may look slightly different, but it’s equivalent.) You can extract an EdgeQL representation of any expression calling the .toEdgeQL() method.

  1. e.str("Hello world!").toEdgeQL();
  2. // "Hello world"
  3. e.set(1, 2, 3).toEdgeQL();
  4. // {1, 2, 3}
  5. e.count(e.Movie).toEdgeQL();
  6. // count(Movie)
  7. e.insert(e.Movie, { title: "Iron Man "}).toEdgeQL();
  8. // insert Movie { title := "Iron Man" }
  9. e.select(e.Movie, () => ({ id: true, title: true })).toEdgeQL();
  10. // select Movie { id, title }

Expressions are runnable. Expressions can be executed with the .run method.

  1. import * as edgedb from "edgedb";
  2. const client = edgedb.createClient();
  3. const myQuery = e.str("Hello world")
  4. const result = await myQuery.run(client)
  5. // => "Hello world"

Note that the .run method accepts an instance of Client() (or Transaction) as it’s first argument. See Creating a Client for details on creating clients. The second argument is for passing $parameters, more on that later.

  1. .run(client: Client | Transaction, params: Params): Promise<T>

Expressions have a type and a cardinality. Just like sets in EdgeQL, all expressions are associated with a type and a cardinality. The query builder is extremely good at inferring these. You can see the values of these with the special __element__ and __cardinality__ properties.

  1. const q1 = e.str("Hello");
  2. q1.__element__: // e.str
  3. q1.__cardinality__: // "One"
  4. const q2 = e.Movie;
  5. q2.__element__: // e.Movie
  6. q2.__cardinality__: // "Many"