edgedb

Computed property based on presence of non-required properties


We know we can create a computed property based on other properties like so as per the docs,

type Person {
  property first_name -> str;
  property last_name -> str;
  property full_name := .first_name ++ ' ' ++ .last_name;
}

I'd like to create a computed property that shows whether a given combination of non-required properties are all present or all absent, conceptually like this,

type Person {
  property first_name -> str;
  property last_name -> str;
  property has_name := .first_name is not null and .last_name is not null;
}

However, EdgeDB does not have a concept of null so this does not work. Is it possible to accomplish this? If so, could the resulting computed property itself be required?


Solution

  • You can check optional properties for emptiness using the exists operator:

    type Person {
      property first_name -> str;
      property last_name -> str;
      property has_name := exists .first_name and exists .last_name;
    }