Search code examples
orientdb

ORIENTDB: traverse / edges with attributes


I have a application permission related question in a graph database. The structure is as follows ... and I have to read the role of a user related to a node. The nodes are hierarchically organized and the roles are provided as Edges containing a type attribute and the relation User -> UserRole {type:xyz} -> Node

create class User extends V    
create class Node extends V  
create class has_father extends E
create class UserRole extends E

create vertex User set name = 'u1'
create vertex User set name = 'u2'
create vertex User set name = 'u3'   



create vertex Node set name = 'n1'
create vertex Node set name = 'n11'
create vertex Node set name = 'n12'
create vertex Node set name = 'n111'    
create vertex Node set name = 'n112'
create vertex Node set name = 'n1111'
create vertex Node set name = 'n1112'
create vertex Node set name = 'n11111'
create vertex Node set name = 'n11112'

create edge has_father from (select from Node where name = 'n11') to (select from Node where name = 'n1')
create edge has_father from (select from Node where name = 'n12') to (select from Node where name = 'n1')
create edge has_father from (select from Node where name = 'n111') to (select from Node where name = 'n11')
create edge has_father from (select from Node where name = 'n112') to (select from Node where name = 'n11')
create edge has_father from (select from Node where name = 'n1111') to (select from Node where name = 'n111')
create edge has_father from (select from Node where name = 'n1112') to (select from Node where name = 'n111')
create edge has_father from (select from Node where name = 'n11111') to (select from Node where name = 'n1111')
create edge has_father from (select from Node where name = 'n11112') to (select from Node where name = 'n1111')

create edge UserRole from (select from User where name = 'u1') to (select from Node where name = 'n1') set type = 'admin'
create edge UserRole from (select from User where name = 'u1') to (select from Node where name = 'n11') set type = 'read'
create edge UserRole from (select from User where name = 'u2') to (select from Node where name = 'n111') set type = 'write'
create edge UserRole from (select from User where name = 'u1') to (select from Node where name = 'n11111') set type = 'test'

actually i have to read now all children from "n" and get the Node and the UserRole of User "u1":

asking for "u1" and "n1":

n11 read
n12 admin

asking for "u1" and "n1111":

n11111 test
n11112 read

Meaning that there is max 1 UserRole assigned and the value might get overridden within the tree. A user might be admin for root level and only read from level n11 on.

How can I read the children Nodes passing the father @rid (or a filter) and the calculated Roles for a special User?


Solution

  • try this SQL for your 2nd example:

    select name as Name, $a.type[0] as Role from (select expand(in('has_father')) from (select from Node where name = 'n1111') unwind in)
    
    let $a=(select inE('UserRole').type as type from (
                traverse out('has_father'),in('UserRole') from $parent.$current while out('UserRole').@rid <> #21:0
                ) where in('UserRole').@rid = #21:0
                limit 1
            )
    

    where #21:0 is the u1 @rid.

    As long this isn't so much pretty, you could insert this in JS function and parametrise it.


    enter image description here enter image description here