pepsico-ecommerce / snowflex

elixir snowflake client
Apache License 2.0
52 stars 10 forks source link

V1.0.0 rc1 #56

Closed PM-Pepsico closed 1 year ago

PM-Pepsico commented 2 years ago

This reconfigures most of our connection handling to follow the Ecto.Adapters.SQL.Connection behavior. This allows queries to be constructed using Ecto schemas and the Ecto.Query macros.

An Ecto Repo for querying can now be created by defining a module like so:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Snowflex.EctoAdapter
end

Compatibility with previous versions of snowflake is possible by adding an execute function to this Repo module which will use the new API and translate the results to how they were returned previously like so:

    def execute(query, params \\ [], opts \\ []) do
      %Snowflex.Result{columns: columns, rows: rows} = __MODULE__.query!(query, params, opts)

      bin_headers =
        columns
        |> Enum.map(fn header -> header |> to_string() |> String.downcase() end)
        |> Enum.with_index()

      Enum.map(rows, fn row ->
        Enum.reduce(bin_headers, %{}, fn {col, index}, map ->
          data =
            row
            |> Enum.at(index)

          Map.put(map, col, data)
        end)
      end)
    end
PM-Pepsico commented 1 year ago

I incorrectly named this branch. I am starting a proper v1.0.0 branch with tags which will be the new correct branch to work from.