EctoApi - a library to use Ecto to describe external api resources

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 and update_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 of Ecto.Query or build a proper DSL for this.
  • get_by from Ecto.Repousually just return a single entry so for the current lack of all I’m adding get_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.

11 Likes

I suggest that you mention the intent of the library more clearly and faster.

When looking at the readme, there’s a lot of text (you first state you have an idea you want to make public, then talk about the API, the issues you had, etc.) while it’s still not clear what the library offers.

I think you should first mention what this library does for you and what it saves you (as I think the goal is to write less code?).

3 Likes