Its very common to use externally tracked ids in dgraph. See here and here for a bit of information there, but basically every update will then be an upsert of the form:
upsert {
query{
me as var(func: eq(email,"me@them.org"))
}
mutation {
set {
uid(me) <myfield> "myvalue" .
uid(me) <email> "me@them.org" .
}
}
}
… which will create a new node with that email
and myfield
if it does not exist, and if it does, it will apply myfield=myvalue to it. This is done atomically, so no need to worry about duplicates. You may also want to mark the field being used as an external id as @upsert
in the schema to ensure uniqueness. See here for more on @upsert
.
Note the external id (email
in this case) is being inserted along with the mutation. You need to set this in the case of a new entry being made. Conditional mutations can gate this if you want to avoid writing the same value over and over, but that is purely an optimization.
- Downside: slower, but how much? maybe you wont notice, depends on many things.
- Upside: using integers as ids is awkward and this is much better.