{"id":17478150,"url":"https://github.com/brunolouvem/ecto_auto_filter","last_synced_at":"2025-04-22T11:24:21.458Z","repository":{"id":48361667,"uuid":"328834400","full_name":"brunolouvem/ecto_auto_filter","owner":"brunolouvem","description":"Automatic Filters based Ecto Schemas","archived":false,"fork":false,"pushed_at":"2021-07-30T00:57:46.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-18T23:21:54.894Z","etag":null,"topics":["ecto","ecto-schemas","elixir"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brunolouvem.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-12T01:07:32.000Z","updated_at":"2021-07-30T00:57:48.000Z","dependencies_parsed_at":"2022-09-14T15:51:51.674Z","dependency_job_id":null,"html_url":"https://github.com/brunolouvem/ecto_auto_filter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunolouvem%2Fecto_auto_filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunolouvem%2Fecto_auto_filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunolouvem%2Fecto_auto_filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunolouvem%2Fecto_auto_filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brunolouvem","download_url":"https://codeload.github.com/brunolouvem/ecto_auto_filter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250229658,"owners_count":21396146,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecto","ecto-schemas","elixir"],"created_at":"2024-10-18T20:11:28.666Z","updated_at":"2025-04-22T11:24:21.438Z","avatar_url":"https://github.com/brunolouvem.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EctoAutoFilter\n\n**Automatic Filters based Ecto Schemas**\n\nEctoAutoFilter is a helper for projects that use Ecto Schemas and segregate the queries in entity repository modules. EctoAutoFilter inject the `filter/3` function that by default has a pattern matching for each field of declared entity.\n\n## Installation\n\nEctoAutoFilter can be installed\nby adding `ecto_auto_filter` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:ecto_auto_filter, \"~\u003e 1.0.0\"}\n  ]\nend\n```\n\n## Get Started\nAfter the installation of dependency, we will add the configuration below so that EctoAutoFilter knows what `repo` module it should execute on queries.\n\n```elixir\n# config.exs\nconfig :ecto_auto_filter,\n  repo: MyApp.Repo,\n```\n\nThis configuration apply the `MyApp.Repo` for all entity repositories that use EctoAutoFilter. There are also the possibility of injecting a different `repo` setup for a specific entity repository, for example:\n\n```elixir\n# config.exs\ndefmodule UserRepository do\n  use EctoAutoFilter,\n    repo: MyApp.AnotherRepo,\n    schema: MyApp.User\nend\n```\nNow we will declare the entity repository. Consider the `User` schema.\n\n```elixir\ndefmodule MyApp.User do\n  use Ecto.Schema\n\n  schema \"users\" do\n    field(:name, :string)\n    field(:email, :string)\n    field(:age, :integer)\n  end\n  ...\nend\n```\nDeclare the UserRepository using EctoAutoFilter, passing in `schema` parameter the `User` schema and here we will consider that we configured a global `repo`.\n\n```elixir\ndefmodule UserRepository do\n  use EctoAutoFilter,\n    schema: MyApp.User\nend\n```\n\nDone! We just need to test now.\n\n```elixir\niex\u003e user_one = Repo.insert!(%User{name: \"John Doe\", email: \"john@doe.com\", age: 30})\niex\u003e user_two = Repo.insert!(%User{name: \"Jane Doe\", email: \"jane@doe.com\", age: 28})\niex\u003e UserRepository.filter(%{age: {30, \"\u003e=\"}})\n{:ok, \n  [%User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 30,\n    email: \"john@doe.com\",\n    id: 1,\n    name: \"John Doe\"\n  }]\n}\n```\n\n## The `filter/3` function\n```elixir\n@spec filter(map | list, atom, module) :: \n  {:ok, one_result | list} | \n  {:error, :not_found | :filter_not_found | :unsupported_filter } \ndef filter(filter, result_format \\\\ :many, query \\\\ @base_schema)\n```\n- `filter`: The first parameter is a map or keywordlist for the filter declaration;\n- `result_format`: It's an atom that can be `:one`, `:first` or `:many`, defined by default `:many`;\n- `query`: Queriable structure that by default is the base entity.\n\n\n#### Filter structure\n\nThe structure of the filter is a map or a keywordlist in which the keys are field names of the schema to be filtered, e.g: `filter(%{name: \"John Doe\"})`.\nFor each key in the filter parameter, EctoAutoFilter concatenate a `where` clause to the `and` logical connector, thus it is possible to compose more complex filters, such as: `filter(%{name: \"John Doe\", age: 30})`. The filters can receive values directly to an exact value comparison, or values with functions that varing between binary operators or functions such as: `like`, `ilike`, `not_in` or `in`.\n\n- {list, \"in | not_in\"}: This clause is composed by a tuple with a list and a string `\"in\"`;\n- {value, \"\u003e | \u003c | \u003e= | \u003c= | == | !=\"}: Arithmetical and logical operators;\n- {value, \"like | ilike\"}: Partial search for strings.\n\n\n### Returning only one value\n```elixir\niex\u003e UserRepository.filter(%{age: {28, \"==\"}}, :one)\n{:ok, \n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 28,\n    email: \"jane@doe.com\",\n    id: 2,\n    name: \"Jane Doe\"\n  }\n}\n```\n\n### Returning the first found value\n```elixir\niex\u003e UserRepository.filter(%{email: {\"%doe.com\", \"like\"}}, :first)\n{:ok,\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 30,\n    email: \"john@doe.com\",\n    id: 1,\n    name: \"John Doe\"\n  }\n}\n```\n\n### Returning all the found values\n```elixir\niex\u003e UserRepository.filter(%{email: {\"%doe.com\", \"like\"}})\n{:ok,[\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 30,\n    email: \"john@doe.com\",\n    id: 1,\n    name: \"John Doe\"\n  },\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 28,\n    email: \"jane@doe.com\",\n    id: 2,\n    name: \"Jane Doe\"\n  }\n]}\n```\n## Filters\n\n### By value\n```elixir\niex\u003e UserRepository.filter(%{age: {28, \"==\"}})\n{:ok,[\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 28,\n    email: \"jane@doe.com\",\n    id: 2,\n    name: \"Jane Doe\"\n  }\n]}\n```\n### Between filters\nOne key of filter can receive a tuple list in which the EctoAutoFilter concatenate by `and` logical operator, such as the example below where we filter the users from 27 to 29 years old.\n\n```elixir\niex\u003e user_three = Repo.insert!(%User{name: \"Tom Doe\", email: \"tom@doe.com\", age: 26})\niex\u003e UserRepository.filter(%{age: [{30, \"\u003c\"}, {26, \"\u003e\"}]})\n{:ok,[\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 28,\n    email: \"jane@doe.com\",\n    id: 2,\n    name: \"Jane Doe\"\n  }\n]}\n```\n\n### `in` \u0026 `not_in` filter\nOne key of filter can receive a tuple with a list of values in which the EctoAutoFilter uses `in` or `not_in` to verify the existence or inexistence of the key inside the values from the list.\n\n```elixir\niex\u003e UserRepository.filter(%{id: {[2, 3], \"in\"}})\n{:ok,[\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 28,\n    email: \"jane@doe.com\",\n    id: 2,\n    name: \"Jane Doe\"\n  },\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 26,\n    email: \"tom@doe.com\",\n    id: 3,\n    name: \"Tom Doe\"\n  }\n]}\n\niex\u003e UserRepository.filter(%{id: {[2, 3], \"not_in\"}})\n{:ok,[\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 30,\n    email: \"john@doe.com\",\n    id: 1,\n    name: \"John Doe\"\n  }\n]}\n```\n\n### `like` \u0026 `ilike` filters\n```elixir\niex\u003e user_four = Repo.insert!(%User{name: \"Norton Doe\", email: \"norton@doe.com\", age: 60})\niex\u003e UserRepository.filter(%{name: {\"%to%\", \"ilike\"}})\n{:ok,[\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 26,\n    email: \"tom@doe.com\",\n    id: 3,\n    name: \"Tom Doe\"\n  },\n  %User{\n    __meta__: #Ecto.Schema.Metadata\u003c:loaded, \"users\"\u003e,\n    age: 60,\n    email: \"norton@doe.com\",\n    id: 4,\n    name: \"Norton Doe\"\n  }\n]}\n```\n\n## Errors\n- `{:error, :unsupported_filter}`: It occurs when an unknown key is passed inside a filter, returning this error, even if the other keys are correct.\n\n- `{:error, :filter_not_found}`: It is returned in case the filter is empty. \n\n- `{:error, :unsupported_filter_operator}`: It is returned if one tuple value is passed with an unknown operator.\n\n\n## Custom Filters\nEctoAutoFilter can be extended through its macro `add_filter/4`, see more in [`add_filter/4`](https://hexdocs.pm/ecto_auto_filter/EctoAutoFilter.html#add_filter/4)\n\n\n## Contributing\n\nFeedback, feature requests, and fixes are welcomed and encouraged. Please make appropriate use of [Issues](https://github.com/brunolouvem/ecto_auto_filter/issues) and [Pull Requests](https://github.com/brunolouvem/ecto_auto_filter/pulls). All code should have accompanying tests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrunolouvem%2Fecto_auto_filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrunolouvem%2Fecto_auto_filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrunolouvem%2Fecto_auto_filter/lists"}