brikteknologier / seraph-model

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

Request: Relationships to return array regardless of size #114

Closed andyrichardson closed 8 years ago

andyrichardson commented 8 years ago

Retreiving nodes When retrieving nodes from the database, regardless of the number of nodes returned, the result is always in an array.

No results results = [ ]

One result results = [ {id: 10, name: 'Joe'} ]

Many results results = [ {id: 10, name: 'Joe'}, {id: 11, name: 'Jess'} ]

Retrieving nodes with relationships

Unfortunately, when working with relationships it's not as simple. Consider the following examples.

No friend relations

results[0] = {
    id: 10,
    name: 'Joe',
    friends: undefined
}

One friend relation

results[0] = {
    id: 10,
    name: 'Joe',
    friends: {
        id: 11,
        name: 'Jess'
    }
}

Many friend relations

results[0] = {
    id: 10,
    name: 'Joe',
    friends: [{
        id: 11,
        name: 'Jess'
    },
    {
        id: 12,
        name: 'Jared'
    }]
}

In the above, there is inconsistency in the type of relationship data being returned. A relationship can either be undefined, an object, or an array of objects. Semantically this doesn't make sense, especially in the second case where a related node is defined as the relationship itself rather than a member of a collection of relationships.

Added to this, extra conditional statements are required to determine the type of data being returned.

Let me know if I'm missing something and keep up the good work!

jonpacker commented 8 years ago

So, if I'm understanding this correctly, this is in the context of composed models, right?

When you set up your composition, set the many flag to true, like this:

Person.compose(Person, 'friends', 'has_friend', {many: true})

Then the composition will behave this way; it will always be an array.

Please reopen and let me know if I've misinterpreted the problem here! :)

andyrichardson commented 8 years ago

Thanks @jonpacker - solved my problem!