Query Parameters​

EdgeQL queries can reference parameters with $ notation. The value of these parameters are supplied externally.

  1. select <str>$var;
  2. select <int64>$a + <int64>$b;
  3. select BlogPost filter .id = <uuid>$blog_id;

Note that we provided an explicit type cast before the parameter. This is required, as it enables EdgeDB to enforce the provided types at runtime.

Usage with clients​

REPL​

When you include a parameter reference in an EdgeDB REPL, you’ll be prompted interactively to provide a value or values.

  1. select 'I ❤️ ' ++ <str>$var ++ '!';
  1. Parameter <str>$var: EdgeDB
  2. {'I ❤️ EdgeDB!'}

Python​

  1. await client.query(
  2. "select 'I ❤️ ' ++ <str>$var ++ '!';",
  3. var="lamp")
  4. await client.query(
  5. "select <datetime>$date;",
  6. date=datetime.today())

JavaScript​

  1. await client.query("select 'I ❤️ ' ++ <str>$var ++ '!';", {
  2. name: "rock and roll"
  3. });
  4. await client.query("select <datetime>$date;", {
  5. date: new Date()
  6. });

Go​

  1. var result string
  2. err = db.QuerySingle(ctx,
  3. `select 'I ❤️ ' ++ <str>$var ++ '!';"`,
  4. &result, "Golang")
  5. var date time.Time
  6. err = db.QuerySingle(ctx,
  7. `select <datetime>$date;`,
  8. &date, time.Now())

Refer to the Datatypes page of your preferred client library to learn more about mapping between EdgeDB types and language-native types.

Parameter types and JSON​

Parameters can only be scalars or arrays of scalars. This may seem limiting at first, but in actuality this doesn’t impose any practical limitation on what can be parameterized. To pass complex structures as parameters, use EdgeDB’s built-in JSON functionality.

  1. with data := <json>$data
  2. insert User {
  3. name := <str>data['name'],
  4. age := <int64>data['age'],
  5. };
  1. Parameter <json>$data: {"name":"Fido", "legs": 4}
  2. {default::Dog {id: 8d286cfe-3c0a-11ec-aa68-3f3076ebd97f}}

Optional parameters​

By default, query parameters are required; the query would fail if parameter value is an empty set. You can use optional modifier inside the type cast if the parameter is optional.

  1. select <optional str>$name;
  1. Parameter <str>$name (Ctrl+D for empty set `{}`):
  2. {}

When using a client library, pass the idiomatic null pointer for your language: null, None, nil, etc.

The <required foo> type cast is also valid (though redundant) syntax.

  1. select <required str>$name;

What can be parametrized?​

Any data manipulation language (DML) statement can be parametrized: select, insert, update, and delete.

Schema definition language (SDL) and configure statements cannot be parametrized. Data definition language (DDL) has limited support for parameters, but it’s not a recommended pattern. Some of the limitations might be lifted in the future versions.