meilisearch-ex
meilisearch-ex copied to clipboard
Idea: Enhance piping different actions
Hi! I'm using the library in a personal project, and I kind of missed the ability to use it like this:
:meili
|> Meilisearch.client()
|> Meilisearch.Index.create(%{uid: @index, primaryKey: "uuid"})
|> Meilisearch.Settings.update(
@index,
%{
searchableAttributes: ["name", "category", "short_name"],
displayedAttributes: ["name", "category", "popular", "image", "popular", "price", "uuid"],
filterableAttributes: ["category"],
sortableAttributes: ["popular", "name"],
synonyms: synonyms()
}
)
|> Meilisearch.Document.create_or_replace(@index, data)
Instead, I have to call each action as a different line client + create, client + settings, client + document.
I don't know what I could suggest to make it work, but I just wanted to know if I'm thinking in the right way!
We could achieve this behaviour by changing the API from this:
defmodule Meilisearch.Settings do
def update(client, index, settings), do: {:ok, result}
end
to this:
defmodule Meilisearch.Settings do
def update({:ok, _, client}, index, settings), do: update(client, index, settings)
def update(client, index, settings) when is_atom(client), do: client |> Meilisearch.client() |> update(index, settings)
def update(client, index, settings), do: {:ok, result, client}
end
Which would allow usage like this:
{:ok, sumtask, client} =
:meili
|> Meilisearch.Index.create(%{uid: @index, primaryKey: @primary})
|> Meilisearch.Settings.update(@index, @settings)
|> Meilisearch.Document.create_or_replace(@index, documents)
In this case, you cannot access to the result of Index.create/3 and Settings.update/3 calls.
But this is a breaking change as it changes the result tuple. That could break existing matching.