Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple relationships definitions in Schema #6

Open
celrenheit opened this issue Apr 20, 2014 · 1 comment
Open

Simple relationships definitions in Schema #6

celrenheit opened this issue Apr 20, 2014 · 1 comment

Comments

@celrenheit
Copy link

It would be nice to handle some simple relationships handling between models.

I propose this configuration:

var UserSchema = new Schema({
    name: String,
    email: String,
    username: String
});


UserSchema.relationships = {
    'createdArticles': {   
        model: 'Article',
        direction: 'OUT',
        edgeLabel: 'created'
    },
    'follows': {
        model: 'User',
        direction: 'OUT',
        edgeLabel: 'follows'
    },
    'followedBy': {
        model: 'User',
        direction: 'IN',
        edgeLabel: 'follows'
    }
};

// AND/OR

UserSchema.relationships.add('createdArticles', {   
    model: 'Article',
    direction: 'OUT',
    edgeLabel: 'created'
})

What do you think ?

@jbmusso
Copy link
Owner

jbmusso commented Apr 22, 2014

I've been playing with this in the past. I was thinking about adding 'Reference' property classes to models that could be either 'SingleReference' or 'MultiReference' properties (the latter two would inherit from a ReferenceProperty class). Such properties would be defined in the raw object passed to the Schema constructor.

var PersonSchema = new Schema({
  name: String
  spouse: PersonSchema
  children: [PersonSchema]
});

In this example, spouse would be a SingleReferenceProperty while children would be a MultiReferenceProperty. There would be issues though when naming edge labels, for not all edge labels can be inferred by solely looking at the property name (and users may want customization as well).

A more complex API could be (maybe optionally):

var PersonSchema = new Schema({
  name: String
  spouse: {
    type: PersonSchema,
    cardinality: 'OneToOne'
    label: 'spouseOf'
  }
  children: {
    type: PersonSchema,
    cardinality: 'OneToMany'
    label: 'parentOf'
  }
});

in relationship could be inferred from out relationships, or explicitly set. I don't know :).

The idea is to abstract as much thing as possible if needed, but still allow the user to fine tune and manipulate lower level concepts as well.

Reference properties would then be populated when models are fetched, and maybe only some of them (ie fetch(populate: ['spouse', 'children']), for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants