@@ -65,6 +65,7 @@ defmodule EctoShorts.Actions do
6565 @ type schema_res :: { :ok , schema ( ) } | { :error , any }
6666
6767 alias EctoShorts . {
68+ Actions ,
6869 Actions.Error ,
6970 CommonFilters ,
7071 CommonSchemas ,
@@ -124,9 +125,9 @@ defmodule EctoShorts.Actions do
124125
125126 ### Filter Parameters
126127
127- When the parameters is a keyword list the options `:repo` and `:replica ` can be set.
128+ When the parameters is a keyword list the options `:repo`, `:replica` and `:query_builder ` can be set.
128129
129- See `EctoShorts.CommonFilters` for more information.
130+ See `EctoShorts.CommonFilters`, `EctoShorts.Actions.QueryBuilder` for more information.
130131
131132 ### Options
132133
@@ -135,19 +136,24 @@ defmodule EctoShorts.Actions do
135136
136137 * `:repo` - A module that uses `Ecto.Repo`.
137138
139+ * `:query_builder` - A module that handles custom filters.
140+
138141 See [Ecto.Repo.all/2](https://hexdocs.pm/ecto/Ecto.Repo.html#c:all/2) for more options.
139142
140143 ### Examples
141144
142145 iex> EctoSchemas.Actions.all(YourSchema, %{id: 1})
143146 iex> EctoSchemas.Actions.all(YourSchema, id: 1, repo: YourApp.Repo)
144147 iex> EctoSchemas.Actions.all(YourSchema, id: 1, replica: YourApp.Repo)
148+ iex> EctoSchemas.Actions.all(YourSchema, id: 1, custom_filter: val, query_builder: YourContext)
145149 iex> EctoSchemas.Actions.all({"source", YourSchema}, %{id: 1})
146150 iex> EctoSchemas.Actions.all({"source", YourSchema}, id: 1, repo: YourApp.Repo)
147151 iex> EctoSchemas.Actions.all({"source", YourSchema}, id: 1, replica: YourApp.Repo)
152+ iex> EctoSchemas.Actions.all({"source", YourSchema}, id: 1, custom_filter: val, query_builder: YourContext)
148153 iex> EctoSchemas.Actions.all(%Ecto.Query{}, %{id: 1})
149154 iex> EctoSchemas.Actions.all(%Ecto.Query{}, id: 1, repo: YourApp.Repo)
150155 iex> EctoSchemas.Actions.all(%Ecto.Query{}, id: 1, replica: YourApp.Repo)
156+ iex> EctoSchemas.Actions.all(%Ecto.Query{}, id: 1, custom_filter: val, query_builder: YourContext)
151157 """
152158 @ spec all (
153159 query :: query ( ) | queryable ( ) | source_queryable ( ) ,
@@ -158,24 +164,17 @@ defmodule EctoShorts.Actions do
158164 end
159165
160166 def all ( query , opts ) do
161- query_params =
162- opts
163- |> Keyword . drop ( [ :repo , :replica ] )
164- |> Map . new ( )
167+ { opts , params } = Keyword . split ( opts , [ :repo , :replica , :query_builder ] )
165168
166- if Enum . any? ( query_params ) do
167- all ( query , query_params , Keyword . take ( opts , [ :repo , :replica ] ) )
168- else
169- all ( query , % { } , Keyword . take ( opts , [ :repo , :replica ] ) )
170- end
169+ all ( query , Map . new ( params ) , opts )
171170 end
172171
173172 @ doc """
174173 Fetches all records matching the given query.
175174
176175 ### Filter Parameters
177176
178- See `EctoShorts.CommonFilters` for more information.
177+ See `EctoShorts.CommonFilters`, `EctoShorts.Actions.QueryBuilder` for more information.
179178
180179 ### Options
181180
@@ -184,21 +183,26 @@ defmodule EctoShorts.Actions do
184183
185184 * `:repo` - A module that uses `Ecto.Repo`.
186185
187- * `:order_by` - Orders the fields based on one or more fields.
186+ * `:query_builder` - A module that handles custom filters.
187+
188+ * `:order_by` - Orders the records based on one or more fields.
188189
189190 See [Ecto.Repo.all/2](https://hexdocs.pm/ecto/Ecto.Repo.html#c:all/2) for more options.
190191
191- ## Examples
192+ ### Examples
192193
193194 iex> EctoSchemas.Actions.all(YourSchema, %{id: 1}, prefix: "public")
194195 iex> EctoSchemas.Actions.all(YourSchema, %{id: 1}, repo: YourApp.Repo)
195196 iex> EctoSchemas.Actions.all(YourSchema, %{id: 1}, replica: YourApp.Repo)
197+ iex> EctoSchemas.Actions.all(YourSchema, %{id: 1, custom_filter: val}, query_builder: YourContext)
196198 iex> EctoSchemas.Actions.all({"source", YourSchema}, %{id: 1}, prefix: "public")
197199 iex> EctoSchemas.Actions.all({"source", YourSchema}, repo: YourApp.Repo)
198200 iex> EctoSchemas.Actions.all({"source", YourSchema}, replica: YourApp.Repo)
201+ iex> EctoSchemas.Actions.all({"source", YourSchema}, %{id: 1, custom_filter: val}, query_builder: YourContext)
199202 iex> EctoSchemas.Actions.all(%Ecto.Query{}, %{id: 1}, prefix: "public")
200203 iex> EctoSchemas.Actions.all(%Ecto.Query{}, repo: YourApp.Repo)
201204 iex> EctoSchemas.Actions.all(%Ecto.Query{}, replica: YourApp.Repo)
205+ iex> EctoSchemas.Actions.all(%Ecto.Query{}, %{id: 1, custom_filter: val}, query_builder: YourContext)
202206 """
203207 @ spec all (
204208 query :: query ( ) | queryable ( ) | source_queryable ( ) ,
@@ -212,6 +216,7 @@ defmodule EctoShorts.Actions do
212216
213217 query
214218 |> CommonFilters . convert_params_to_filter ( params )
219+ |> Actions.Filters . convert_params_to_filter ( params , Keyword . get ( opts , :query_builder , nil ) )
215220 |> Config . replica! ( opts ) . all ( opts )
216221 end
217222
@@ -225,17 +230,22 @@ defmodule EctoShorts.Actions do
225230
226231 * `:repo` - A module that uses `Ecto.Repo`.
227232
233+ * `:query_builder` - A module that handles custom filters (see EctoShorts.Actions.QueryBuilder).
234+
228235 See [Ecto.Repo.all/2](https://hexdocs.pm/ecto/Ecto.Repo.html#c:one/2) for more options.
229236
230237 ### Examples
231238
232239 iex> EctoSchemas.Actions.find(YourSchema, %{id: 1})
240+ iex> EctoSchemas.Actions.find(YourSchema, %{id: 1, custom_filter: val}, query_builder: YourContext)
233241 iex> EctoSchemas.Actions.find({"source", YourSchema}, %{id: 1})
234242 iex> EctoSchemas.Actions.find({"source", YourSchema}, %{id: 1}, repo: YourApp.Repo)
235243 iex> EctoSchemas.Actions.find({"source", YourSchema}, %{id: 1}, replica: YourApp.Repo)
244+ iex> EctoSchemas.Actions.find({"source", YourSchema}, %{id: 1, custom_filter: val}, query_builder: YourContext)
236245 iex> EctoSchemas.Actions.find(%Ecto.Query{}, %{id: 1})
237246 iex> EctoSchemas.Actions.find(%Ecto.Query{}, %{id: 1}, repo: YourApp.Repo)
238247 iex> EctoSchemas.Actions.find(%Ecto.Query{}, %{id: 1}, replica: YourApp.Repo)
248+ iex> EctoSchemas.Actions.find(%Ecto.Query{}, %{id: 1, custom_filter: val}, query_builder: YourContext)
239249 """
240250 @ spec find (
241251 query :: queryable ( ) | source_queryable ( ) ,
@@ -262,6 +272,7 @@ defmodule EctoShorts.Actions do
262272
263273 query
264274 |> CommonFilters . convert_params_to_filter ( params )
275+ |> Actions.Filters . convert_params_to_filter ( params , Keyword . get ( opts , :query_builder , nil ) )
265276 |> Config . replica! ( opts ) . one ( opts )
266277 |> case do
267278 nil ->
@@ -631,16 +642,20 @@ defmodule EctoShorts.Actions do
631642
632643 * `:repo` - A module that uses `Ecto.Repo`.
633644
645+ * `:query_builder` - A module that handles custom filters.
646+
634647 See [Ecto.Repo.stream/2](https://hexdocs.pm/ecto/Ecto.Repo.html#c:stream/2) for more options.
635648
636649 ### Examples
637650
638651 iex> EctoSchemas.Actions.stream(YourSchema, %{id: 1})
639652 iex> EctoSchemas.Actions.stream(YourSchema, %{id: 1}, repo: YourApp.Repo)
640653 iex> EctoSchemas.Actions.stream(YourSchema, %{id: 1}, replica: YourApp.Repo)
654+ iex> EctoSchemas.Actions.stream(YourSchema, %{id: 1, custom_filter: val}, query_builder: YourContext)
641655 iex> EctoSchemas.Actions.stream({"source", YourSchema}, %{id: 1})
642656 iex> EctoSchemas.Actions.stream({"source", YourSchema}, %{id: 1}, repo: YourApp.Repo)
643657 iex> EctoSchemas.Actions.stream({"source", YourSchema}, %{id: 1}, replica: YourApp.Repo)
658+ iex> EctoSchemas.Actions.stream({"source", YourSchema}, %{id: 1, custom_filter: val}, query_builder: YourContext)
644659 """
645660 @ spec stream (
646661 query :: queryable ( ) | source_queryable ( ) ,
@@ -655,6 +670,7 @@ defmodule EctoShorts.Actions do
655670 query
656671 |> CommonSchemas . get_schema_query ( )
657672 |> CommonFilters . convert_params_to_filter ( params )
673+ |> Actions.Filters . convert_params_to_filter ( params , Keyword . get ( opts , :query_builder , nil ) )
658674 |> Config . replica! ( opts ) . stream ( opts )
659675 end
660676
@@ -669,16 +685,20 @@ defmodule EctoShorts.Actions do
669685
670686 * `:repo` - A module that uses `Ecto.Repo`.
671687
688+ * `:query_builder` - A module that handles custom filters.
689+
672690 See [Ecto.Repo.aggregate/4](https://hexdocs.pm/ecto/Ecto.Repo.html#c:aggregate/4) for more options.
673691
674692 ### Examples
675693
676694 iex> EctoSchemas.Actions.aggregate(YourSchema, %{id: 1}, :count, :id)
677695 iex> EctoSchemas.Actions.aggregate(YourSchema, %{id: 1}, :count, :id, repo: YourApp.Repo)
678696 iex> EctoSchemas.Actions.aggregate(YourSchema, %{id: 1}, :count, :id, replica: YourApp.Repo)
697+ iex> EctoSchemas.Actions.aggregate(YourSchema, %{id: 1, custom_filter: val}, :count, :id, query_builder: YourContext)
679698 iex> EctoSchemas.Actions.aggregate({"source", YourSchema}, %{id: 1}, :count, :id)
680699 iex> EctoSchemas.Actions.aggregate({"source", YourSchema}, %{id: 1}, :count, :id, repo: YourApp.Repo)
681700 iex> EctoSchemas.Actions.aggregate({"source", YourSchema}, %{id: 1}, :count, :id, replica: YourApp.Repo)
701+ iex> EctoSchemas.Actions.aggregate({"source", YourSchema}, %{id: 1, custom_filter: val}, :count, :id, query_builder: YourContext)
682702 """
683703 @ spec aggregate (
684704 query :: query ( ) | queryable ( ) | source_queryable ( ) ,
@@ -697,6 +717,7 @@ defmodule EctoShorts.Actions do
697717 query
698718 |> CommonSchemas . get_schema_query ( )
699719 |> CommonFilters . convert_params_to_filter ( params )
720+ |> Actions.Filters . convert_params_to_filter ( params , Keyword . get ( opts , :query_builder , nil ) )
700721 |> Config . replica! ( opts ) . aggregate ( aggregate , field , opts )
701722 end
702723
0 commit comments