Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON output to shape params #1945

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-foxes-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/elixir-client": patch
---

Allow for outputting columns as list in shape parameters
21 changes: 17 additions & 4 deletions packages/elixir-client/lib/electric/client/shape_definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,26 @@ defmodule Electric.Client.ShapeDefinition do
end

@doc false
@spec params(t()) :: Electric.Client.Fetch.Request.params()
def params(%__MODULE__{} = shape) do
@spec params(t(), [{:format, :query | :json}]) :: Electric.Client.Fetch.Request.params()
def params(%__MODULE__{} = shape, opts \\ []) do
%{where: where, columns: columns} = shape
table_name = url_table_name(shape)
format = Keyword.get(opts, :format, :query)

%{table: table_name}
%{"table" => table_name}
|> Util.map_put_if("where", where, is_binary(where))
|> Util.map_put_if("columns", fn -> Enum.join(columns, ",") end, is_list(columns))
|> Util.map_put_if(
"columns",
fn -> params_columns_list(columns, format) end,
is_list(columns)
)
end

defp params_columns_list(columns, :query) when is_list(columns) do
Enum.join(columns, ",")
end

defp params_columns_list(columns, :json) when is_list(columns) do
columns
end
end
2 changes: 1 addition & 1 deletion packages/elixir-client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@core/elixir-client",
"private": true,
"version": "0.1.0",
"version": "0.1.1",
"scripts": {
"publish:hex": "mix do deps.get, hex.publish --yes"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,29 @@ defmodule Electric.Client.ShapeDefinitionTest do
)
end
end

describe "params/2" do
test "format: :query returns column names joined by comma" do
assert {:ok, shape} = ShapeDefinition.new("my_table", columns: ["id", "size", "cost"])

assert ShapeDefinition.params(shape, format: :query) == %{
"columns" => "id,size,cost",
"table" => "my_table"
}

assert ShapeDefinition.params(shape) == %{
"columns" => "id,size,cost",
"table" => "my_table"
}
end

test "format: :json returns column names as a list" do
assert {:ok, shape} = ShapeDefinition.new("my_table", columns: ["id", "size", "cost"])

assert ShapeDefinition.params(shape, format: :json) == %{
"columns" => ["id", "size", "cost"],
"table" => "my_table"
}
end
end
end
Loading