How do perform a graph query and join?

94 views Asked by At

I apologize for the title, I don't exactly know how to word it. But essentially, this is a graph-type query but I know RavenDB's graph functionality will be going away so this probably needs to be solved with Javascript.

Here is the scenario:

I have a bunch of documents of different types, call them A, B, C, D. Each of these particular types of documents have some common properties. The one that I'm interested in right now is "Owner". The owner field is an ID which points to one of two other document types; it can be a Group or a User.

The Group document has a 'Members' field which contains an ID which either points to a User or another Group. Something like this

enter image description here

It's worth noting that the documents in play have custom IDs that begin with their entity type. For example Users and Groups begin with user: and group: respectively. Example IDs look like this: user:[email protected] or group:the-nights-watch. This comes into play later.

What I want to be able to do is the following type of query: "Given that I have either a group id or a user id, return all documents of type a, b, or c where the group/user id is equal to or is a descendant of the document's owner."

In other words, I need to be able to return all documents that are owned by a particular user or group either explicitly or implicitly through a hierarchy.

I've considered solving this a couple different ways with no luck. Here are the two approaches I've tried:

Using a function within a query

With Dejan's help in an email thread, I was able to devise a function that would walk it's way down the ownership graph. What this attempted to do was build a flat array of IDs which represented explicit and implicit owners (i.e. root + descendants):

declare function hierarchy(doc, owners){
    owners = owners || [];
    while(doc != null) {
        let ownerId = id(doc)
        if(ownerId.startsWith('user:')) {
            owners.push(ownerId);
        } else if(ownerId.startsWith('group:')) {
            owners.push(ownerId);
            doc.Members.forEach(m => {
                let owner = load(m, 'Users') || load(m, 'Groups');
                owners = hierarchy(owner, owners);
            });
        }
    }

    return owners;
}

I had two issues with this. 1. I don't actually know how to use this in a query lol. I tried to use it as part of the where clause but apparently that's not allowed:

from @all_docs as d
where hierarchy(d) = 'group:my-group-d'

// error: method hierarchy not allowed

Or if I tried anything in the select statement, I got an error that I have exceeded the number of allowed statements.

As a custom index

I tried the same idea through a custom index. Essentially, I tried to create an index that would produce an array of IDs using roughly the same function above, so that I could just query where my id was in that array

map('@all_docs', function(doc) {

    function hierarchy(n, graph) {
        while(n != null) {
            let ownerId = id(n);
            if(ownerId.startsWith('user:')) {
                graph.push(ownerId);

                return graph;
                
            } else if(ownerId.startsWith('group:')){
                graph.push(ownerId);
                n.Members.forEach(g => {
                    let owner = load(g, 'Groups') || load(g, 'Users');
                    hierarchy(owner, graph);
                });

                return graph;
            }
        }
    }

    function distinct(value, index, self){ return self.indexOf(value) === index; }
    
    let ownerGraph = []
    if(doc.Owner) {
        let owner = load(doc.Owner, 'Groups') || load(doc.Owner, 'Users');
        ownerGraph = hierarchy(owner, ownerGraph).filter(distinct);
    }

    return { Owners: ownerGraph };
})

// error: recursion is not allowed by the javascript host

The problem with this is that I'm getting an error that recursion is not allowed.

So I'm stumped now. Am I going about this wrong? I feel like this could be a subquery of sorts or a filter by function, but I'm not sure how to do that either. Am I going to have to do this in two separate queries (i.e. two round-trips), one to get the IDs and the other to get the docs?

Update 1

I've revised my attempt at the index to the following and I'm not getting the recursion error anymore, but assuming my queries are correct, it's not returning anything

// Entity/ByOwnerGraph
map('@all_docs', function(doc) {

    function walkGraph(ownerId) {
        let owners = []
        let idsToProcess = [ownerId]
        while(idsToProcess.length > 0) {
            let current = idsToProcess.shift();
            if(current.startsWith('user:')){
                owners.push(current);
            } else if(current.startsWith('group:')) {
                owners.push(current);
                let group = load(current, 'Groups')
                if(!group) { continue; }
                idsToProcess.concat(group.Members)
            }
        }

        return owners;
    }

    let owners = [];
    if(doc.Owner) {
        owners.concat(walkGraph(doc.Owner))
    }
    
    return { Owners: owners };
})

// query (no results)
from index Entity/ByOwnerGraph as x
where x.Owners = "group:my-group-id"

// alternate query (no results)
from index Entity/ByOwnerGraph as x
where x.Owners ALL IN ("group:my-group-id")

I still can't use this approach in a query either as I get the same error that there are too many statements.

0

There are 0 answers