menu

EdgeDB

Bringing state-of-the-art database technology to the masses.

Channels
Team

Is it possible to declare scalar type with additional properties?

January 28, 2020 at 8:31pm

Is it possible to declare scalar type with additional properties?

January 28, 2020 at 8:31pm
I've address field which consists of street, city, county, country, state, zipcode, country. I need this address to be added to many other Object types. What's best way of doing it? I don't want Address to be it's own type so that people can start inserting into it.

January 28, 2020 at 8:34pm
One way is to use inheritance and abstract types:
abstract type WithAddress {
property city -> str;
property zipcode -> str;
...
}
type A extending WithAddress {
property smth -> int64;
...
}
The type "A" in the above example would inherit the address fields from the "WithAddress" type. But the "WithAddress" type cannot be used for storing data.
Edited
  • reply
  • like
Also, if you don't want to make the address an object type, then then next best way would be to use a named tuple:
type User {
property address -> tuple<street: str, city: str, ...>
};
And then:
INSERT User { address := (street := 'California Street', city := 'San Francisco') };
  • reply
  • like
Can I re-use named tuple across different object types?
  • reply
  • like
We don't have the ability to give aliases to tuple types, but the mixin method by above would work.
  • reply
  • like
Can an object type extends from multiple other types?
  • reply
  • like
yes.
abstract type WithAddress {
property city -> str;
property zipcode -> str;
...
}
abstract type WithEmail {
property email -> str;
}
type A extending WithAddress, WithEmail {
property smth -> int64;
...
}
  • reply
  • like
Ok thanks for the ideas. I'll go with abstract type.
like-fill
1
  • reply
  • like
Also take a look at https://edgedb.com/docs/edgeql/overview#polymorphic-queries to learn more about polymorphism in edgeql/edgedb
  • reply
  • like