baethon/kex

Add `get${relationName}` methods to model

radmen opened this issue · 2 comments

const User = kex.createModel('User', {
  relations: {
    messages: new HasMany('Message')
  }
})

This would generate the getMessages(id) method which generates a query to the Message model with corresponding WHERE statement.

The idea evolved a little.

Instead of getter methods, create a query method, bound only to model (not query builder).

Something like this:

await User.messages(userId) // here you can start chaining query builder methods for the related query

It seems to be more consistent with other methods.

One thing though, to avoid the risk of method collisions (with scopes) I think that the scopes proxy should be removed. They should be available only when using the query builder.

I started working on this one and bumped into an issue with the way how queries are built and what they're returning. This is a case mostly for BelongsToMany which joins the pivot table and selects the foreign key from that table.

I'd like to avoid adding unnecessary data to the results, however, this can't be done in a decent way for this feature. I decided to wait a moment and rethink the architecture for this. Quite possibly, I should finish #5 first.