Creates a common set of interface APIs for ecto-based models. By including this functionality you can get a lot of free functionality, including (but not limited to):
list_.../0
returns all records for a schemaget_.../1
returns a singular record by it's primary keycreate_.../1
inserts a new record into the databaserandom_.../
returns a random record for a schema
and many more.
All you need to do is use(EctoInterface)
on your context modules:
defmodule Core.Users do
use EctoInterface, source: Core.Users.Account, plural: :accounts, singular: :account
end
The first argument is the ecto Schema module, the second is the plural name for the record, and finally the singular.
Additionally if you have slugy
installed you can use:
defmodule Core.Users do
use EctoInterface, source: Core.Users.Account, plural: :accounts, singular: :account, slug: :username
end
which gives Core.Users.get_account_by_slug("kurtis-rainbolt-greene")
(the slug is slugified on query so it doesn't need to be in slug form).
Also, we have a simple interface for tags:
defmodule Core.Users do
use EctoInterface, source: Core.Users.Account, plural: :accounts, singular: :account, tagged: :tags
end
For useful functions like Core.Users.list_accounts_with_tags(["friendly", "sporty])
.
Another interface is the PubSub
interface:
defmodule Core.Users do
use EctoInterface, source: Core.Users.Account, plural: :accounts, singular: :account, pubsub: true
end
For useful functions like Core.Users.broadcast_account_change(account)
and Core.Users.subscribe_to_accounts()
.
If available in Hex, the package can be installed
by adding ecto_interface
to your list of dependencies in mix.exs
:
def deps do
[
{:ecto_interface, "~> ..."}
]
end
And finally in your config/config.exs
:
config :ecto_interface, default_repo: Core.Repo, default_pubsub: Core.PubSub
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_interface.