Fork me on GitHub
#xtdb
<
2021-06-28
>
jlmr09:06:30

Hi, I’m trying different approaches to modelling my entities in Crux. If you have an “id-attribute” per entity type, for example :person/id , :car/id and :house/id , is it sensible to then use the same value (some uuid) for :crux.db/id. So it would look like this:

{:crux.db/id 0022-1111
 :person/id 0022-1111}

{:crux.db/id 0033-1111
 :house/id 0033-1111}

{:crux.db/id 0044-1111
 :car/id 0044-1111}

jarohen09:06:11

Assuming those are either UUIDs or otherwise guaranteed not to clash between types, looks good to me 🙂

jlmr09:06:09

Yes those will be actual UUIDs. Thanks for the quick reply!

🙏 3
jlmr11:06:40

Another question, does crux have something similar to Datomic’s idents?

malcolmsparks11:06:39

No, there are no uniqueness constraints on attribute values other than :crux db/id

malcolmsparks11:06:14

You could build a tx fn to enforce uniqueness of attribute values on ingest, if you really needed. You would have to query those entities with q, there are no idents or lookup refs.

malcolmsparks11:06:11

Crux separates constraints and schema from the data, this is by design.

malcolmsparks11:06:47

If you can, try to leverage :crux.db/id. You're not limited to longs. I use it for URIs

jlmr11:06:08

Do your URI’s somehow encode the entity type?

malcolmsparks11:06:16

They certainly can, many URIs have path segments that do just that.

jlmr11:06:31

That’s an interesting alternative to the approach I’m contemplating now: using the same “id-value” twice on each entity. Once on :crux.db/id and once on a type-specific id such as user/id

malcolmsparks11:06:40

However, I prefer to separate identity from type. Can you be certain that an entity won't ever change type (or, more likely, that you may need to rename a type as you refine your doman language)

jlmr11:06:37

True, but then when/where/how would you encode type?

malcolmsparks11:06:27

I have an attribute that specifies the type, when I want an explicit type. Also, that works for situations where I want multiple types.

jlmr11:06:07

So something like :entity/type :user ?

👍 6
malcolmsparks11:06:11

If I need to query all instances of a type, I query on that attribute

malcolmsparks11:06:24

And :entity/type #{:user :vip}

jlmr11:06:31

Does your code check whether something is of a specific type before performing certain actions? Maybe I’m thinking about this the wrong way, but I guess type and identity are still pretty complected in my head… 😉

malcolmsparks11:06:52

Yes, sometimes I use the type in multimethods to determine behaviour.

jlmr11:06:25

Ok that helps 🙂

malcolmsparks11:06:27

Mostly, my actions are simple puts into the db. Sometimes removes

jlmr11:06:00

Thanks, I’ll consider what the best way forward is with this info

malcolmsparks11:06:04

You can even write Datalog query rules that work on types. So you can have a rule that can do type inference. For example, if all employees are users, and users can change their passwords, and Jack is an employee, then Jack can change his password.

👍 3
malcolmsparks11:06:16

YMMV of course, but I have found it that separating identity from type (the who from the what) delivers a lot of flexibility

malcolmsparks11:06:27

I have an attribute that specifies the type, when I want an explicit type. Also, that works for situations where I want multiple types.

Steven Deobald18:06:49

I wonder if the "type" vs. "identity" discussion is a worthy addition to this mini-doc? https://opencrux.com/community/faq.html#_modeling_types

👍 5