graph-databasesdgraph

How to model similar named predicates or attributes for nodes in dgraph?


I am trying to use Dgraph as my primary database. I have a simple system that has two domain entities viz. User and Product. They both have certain properties represented as edges/attributes in Dgraph. They both have a common property name which is a string. If I use the same predicate name for both the nodes then it creates a problem when I am using a has function to find all the users with a name edge. The has function also returns Product nodes with name edge. This is not desirable.

In this situation, what is the right approach or recommendation when modeling the domain entities? I can think of two approaches:

  1. Have a common edge type for all the nodes to uniquely identify similar nodes. Here the value of type would be User or Product. This is approximately similar to a traditional table/column analogy where type represents the table and edges as columns with a context localized to type property.
  2. Have a separate predicate for each node type. So, instead of having name, prefer two predicates like user_name and product_name.

I believe this problem only exists for RDF/Triplestore databases like Dgraph and not for property graphs like Neo4j since each node contains its own properties.


Solution

  • Good news! In Dgraph v1.1, types were introduced.

    You may assign a type User and Product to your entities and filter at query time by doing:

    {
      q(func: type(User)) {
        uid
        name
      }
    }
    

    Before v1.1

    For the sake of simplicity, I would assign unique names as you describe at point 2.

    Should you still like to have a shared property label like name, doing what you propose at point 1 will be ok.

    Query could look like this:

    {
      q(func: has(kind)) {
        pred @filter(user) {
          uid
          name
        }
      }
    }