brianloveswords/streamsql — pull request #16 by andrewhayward (2014-04-17 12:09:15+00:00)
Title: [WIP] Many-to-many relationships

This provides support for many-to-many relationships by allowing `hasMany` relationships to utilise a `via` property.

That is, given the following tables:

```js
db.table('first', {
fields: ['id', ...]
relationships: {
things: {
type: 'hasMany',
local: 'id',
foreign: { table: 'second', key: 'id' }
}
}
})

db.table('second', {
fields: ['id', ...],
relationships: {
things: {
type: 'hasMany',
local: 'id',
foreign: { table: 'first', key: 'id' }
}
}
})
```

we can introduce a third table, and add `via` to our relationships to go through that table instead:

```js
db.table('via', {
fields: ['first_id', 'second_id']
})

db.table('first', {
fields: ['id', ...]
relationships: {
things: {
type: 'hasMany',
local: 'id',
foreign: { table: 'second', key: 'id' },
via: { table: 'via', local: 'first_id', foreign: 'second_id' }
}
}
})
```

This results in a `first.get(...)` request having a `things` property with many `second` items.

====

**TO DO**

- [ ] Tests for sqlite
- [ ] Documentation