brikteknologier / seraph-model

thin model layer for seraph/neo4j (node.js)
MIT License
111 stars 28 forks source link

Use relationship keys on included models #123

Closed andyrichardson closed 7 years ago

andyrichardson commented 8 years ago

When including non-composed models in read queries, it would make sense if the resulting 'compositions'/relationships could be keyed like traditional compositions.

Current result

"0": [
    {
        name: 'Andy'
    }
],
name: 'joe'

Desired result

{
    name: 'Joe',
    friends: [
        { name: 'andy' }
    ]
}

Implementation

const opts = {
    limit: 1,
    include: [{
        model: model,
        rel: 'has_friend',
        key: 'friends', // <- key declaration here
        direction: 'in',
        many: true
    }]
};

model.where(id, opts function(){
...
TimothyMischief commented 7 years ago

I may be misunderstanding here, (first day using seraph came to look at expanding the features of includes and found this) but isn't this achievable by defining your includes in an object rather than an array?

Using your example:

const opts = {
    limit: 1,
    include: {
        friends: { // <-key declaratioin here
            model: model,
            rel: 'has_friend',
            direction: 'in',
            many: true
        }
    }]
};

model.where(id, opts function(){
...

This is what I've been doing to structure my includes and it has worked.

andyrichardson commented 7 years ago

Thanks for the response, I've gotten past this issue for the time being. When it comes to refactoring, I'll be sure to check out your solution!