With blocks
During the query rendering step, the number of occurrences of each expression are tracked. All expressions that are referenced more than once and are not explicitly defined in a WITH block (with e.with), are extracted into the nearest WITH block that encloses all usages of the expression.
1. const a = e.set(e.int64(1), e.int64(2), e.int64(3));
2. const b = e.alias(a);
4. e.select(e.plus(a, b)).toEdgeQL();
5. // WITH
6. // a := {1, 2, 3},
7. // b := a
8. // SELECT a + b
This hold for expressions of arbitrary complexity.
1. const newActor = e.insert(e.Person, {
2. name: "Colin Farrell"
3. });
5. const newMovie = e.insert(e.Movie, {
6. title: "The Batman",
7. cast: newActor
8. });
10. const query = e.select(newMovie, ()=>({
11. id: true,
12. title: true,
13. cast: { name: true }
14. }));
To embed WITH statements inside queries, you can short-circuit this logic with a “dependency list”. It’s an error to pass an expr to multiple e.with``s, and an error to use an expr passed to ``e.with outside of that WITH block in the query.
1. const newActor = e.insert(e.Person, {
2. name: "Colin Farrell"
3. });
5. e.insert(e.Movie, {
6. cast: e.with([newActor], // list "dependencies";
7. e.select(newActor, ()=>({
8. id: true,
9. title: true,
10. }))
11. )
12. })
