TurtleAI / derive

An Event Sourcing and CQRS solution.
0 stars 1 forks source link

Accept a query struct as a selector on Derive.Ecto.Selector #22

Closed rwillians closed 1 year ago

rwillians commented 1 year ago

Derive.Ecto.Selector should accept a %Query{} (ideally, any query-able supported by Ecto) as selector. https://github.com/TurtleAI/derive/blob/master/lib/derive/ecto/selector.ex#L23-L31

A common use case is changing multiple rows with a single query. For example:

def handle_event(%InvoiceIssued{} = event) do
  [
    [ids: event.line_item_ids]
    |> Query.invoice_line_items() # returns a %Query{} struct
    |> update(%{status: :invoiced})
  ]
end

A possible implementation (in https://github.com/TurtleAI/derive/blob/master/lib/derive/ecto/selector.ex#L23-L31) is:

def selector_query(%Query{} = query), do: query

Doing multiple operations is likely -- although I didn't measure it -- performance impact.

venkatd commented 1 year ago

@rwillians my current preference is to keep selectors as simple as possible. There are a number of places where selector is used where we can't use an arbitrary query in place of a selector. I can't remember where off the top of my head.

We do support dropping down to a regular %Ecto.Query in cases where we want more complex behaviors. If there is an Ecto query, Derive assumes it's an update statement and runs it as-is.

For example: https://github.com/TurtleAI/turtle-api/blob/master/lib/turtle/project/reducer.ex#L96-L122

How we handle Turtle.Event.CardDeleted is most similar to what you're trying to accomplish.

LMK if that works. If not can re-open.