repo: GitHub - fcevado/ecto_api
This is one idea that I’ve been experiment with and I’m making it public so there can be some discussion to improve these ideas.
Although the name initially might drive someone to think that it’s only for http api, the way i’m envisioning this is that it can be used with any sort of communication protocol.
Even asynchronous protocols might fit here.
Proposed API
I’m thinking t mimic Ecto.Repo
for EctoApi
with some restrictions and additions:
-
insert_all
andupdate_all
aren’t planned to be available. -
all
might happen, but not soon. I’m still uncertain if it’s better to support a limitted version ofEcto.Query
or build a proper DSL for this. -
get_by
fromEcto.Repo
usually just return a single entry so for the current lack ofall
I’m addingget_all
. - I’m currently working on how
preload
should work. - so the available functions are
insert
,update
,delete
,get
,get_by
,get_all
.
Example:
defmodule User do
use Ecto.Schema
@schema_prefix "http://reqres.in/api"
schema "/users" do
field(:email, :string)
field(:first_name, :string)
field(:last_name, :string)
field(:avatar, :string)
end
end
usage:
#insert
%User{}
|> User.changeset(%{first_name: "John", last_name: "Doe"})
|> EctoApi.insert(resolver: EctoApi.Resolvers.Rest, client: EctoApi.Clients.Http, builder: User)
#update
{:ok, user} =
%User{}
|> User.changeset(%{first_name: "John", last_name: "Doe"})
|> EctoApi.insert(resolver: EctoApi.Resolvers.Rest, client: EctoApi.Clients.Http, builder: User)
user
|> User.changeset(%{first_name: "Janet", last_name: "Doe"})
|> EctoApi.update(resolver: EctoApi.Resolvers.Rest, client: EctoApi.Clients.Http, builder: User)
#delete
{:ok, user} =
%User{}
|> User.changeset(%{first_name: "John", last_name: "Doe"})
|> EctoApi.insert(resolver: EctoApi.Resolvers.Rest, client: EctoApi.Clients.Http, builder: User)
user
|> EctoApi.delete(resolver: EctoApi.Resolvers.Rest, client: EctoApi.Clients.Http, builder: User)
#get
User
|> EctoApi.get(6, resolver: EctoApi.Resolvers.Rest, client: EctoApi.Clients.Http, builder: User)
i’d like to get some opinions on what have been done so far and some solutions for the issues that I’m having so far(they’re described in the repo readme). I’d like to keep the discussions over the issues of the repo, but I’ll be checking this thread for sure.