neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

add relation to a model as a nested field #353

Open almeynman opened 8 years ago

almeynman commented 8 years ago

Hello,

Suppose I have a model Player:

const Player = thinky.createModel('Player', {
    email: type.string().email(),
    password: type.string()
});

And I have a relationships:

Player.hasMany(Game, 'hostedGames', 'id', 'hostId');
Player.hasAndBelongsToMany(Game, 'visitedGames', 'id', 'id', {type: 'visitedGames'});

So now response is going to look like:

{
  email: 'johndoe@gmail.com',
  password: 'aasdsa',
  hostedGames: [...],
  visitedGames: [...]
}

But what If I want is for the relational fields to be nested, i.e.:

{
  email: 'johndoe@gmail.com',
  password: 'aasdsa',
  football: {
    hostedGames: [...],
    visitedGames: [...]
  }
}

Is it possible to define relationship, so that they will be nested in some object with thinky?

Thanks

neumino commented 8 years ago

This is currently not possible. Joined documents have to lie on the top level. This prevents weird/undesired edge cases like if football contains other fields, things get complicated.