I have an odd requirement that I have not yet stum...
# exposed
t
I have an odd requirement that I have not yet stumbled upon, since I'm more of a front-end orienteered fellow. How would you turn this into Exposed objects and Kotlin interfaces? • In the UI there's a list, where you can add items to. The items are supposed to be turned into rows in the database table(s) • The problem: ◦ The items have fields, like
Name
,
Tag
,
Value
. The items also have a
Type
field. The
Type
field dictates the rest of the fields for the item. ▪︎ So the
Name
,
Tag
and
Value
are independent of the
Type
field. ▪︎ But if the value of
Type
is
Type A
, a few more fields are added for the item:
IP
,
Port
,
Channel
. However, if the
Type
has the value
Type B
, the item has other fields added to it:
Address
,
Body
,
Method
The question: ◦ What kind of table(s) with what kinds of fields would you turn this spec into? ▪︎ One option would be to create one table where all
Type
dependent values (
IP
,
Port
,
Channel
,
Address
,
Body
,
Method
) are nullable. ▪︎ Another option would be to create add a field into table for the item such as
TypeDependentFields
, which would then be an object/class which would contain either
IP
,
Port
,
Channel
or
Address
,
Body
,
Method
. • For example in TypeScript this could be achieved with union types:
TypeDependentFields: TypeAFields | TypeBFields
, but Kotlin doesn't have union types. • It'd be nice to have this kind of datamodel atleast between the frontend (React TypeScript) and the backend for simplicity • Tech stack: Kotlin, Ktor, Exposed, PostgreSQL
e
1. you can use an entity-attribute-value schema for this 2. you can use exposed to read up raw attributes with values 3. you need the different types defined as classes somewhere, and some factory methods which can take the exposed result and turn them to an actual type instance (at least that's what I'd do after a brief first read)
t
Thanks! Someone else recommended this at another Slack as well, and it seems like this is the way to go 🙂 I might ask for some more details later when I get to play around with the idea some more, but at least this helps me to get going!