emmett-framework / rest

REST extension for Emmett framework
BSD 3-Clause "New" or "Revised" License
14 stars 2 forks source link

complex query #23

Open josejachuf opened 7 months ago

josejachuf commented 7 months ago

I have the following:

class A(Model):
    a1 = Field.string(length=50)
    a2 = Field.string(length=50)}
    has_many({'b: 'B'})

class B(Model):
    belongs_to({'a': 'A'})
    b1 = Field.string(length=50)
    b2 = Field.string(length=50)
    has_many({'c': 'C'})

class C(Model):
    belongs_to({'b': 'B'})
    c1 = Field.string(length=50)
    c2 = Field.string(length=50)

My Endpoint uses model B. What would be the simplest way to use the where feature, if you can consult by:

a1 c1

He had thought about creating a view and then a model mapping with view. Any better option?

The other thing that occurs to me would be to use this and do everything manually:

@tasks.get_dbset
def fetch_tasks():
    return Task.where(lambda t: t.is_deleted == False)
gi0baro commented 7 months ago

I think if you can build a view easily, mapping a model with it is the easiest and most performance way to do this.

The other approach as you correctly guessed is to handle the querying manually; I'd personally make a subclass of JSONQueryPipe (https://github.com/emmett-framework/rest/blob/v1.5.2/emmett_rest/queries/helpers.py#L25) which implements a custom parser (https://github.com/emmett-framework/rest/blob/v1.5.2/emmett_rest/queries/parser.py#L157) that parse the specific keys adding the join queries. But given the current implementation, this is far from being simple to implement. I might consider refactoring the parser to better support custom parsers out of the box in the future.