Skip to content

Commit 867ca80

Browse files
authored
Add transform function support to sync and sync_render (#99)
Fixes #53
1 parent 36825f5 commit 867ca80

33 files changed

+1416
-78
lines changed

apps/plug_sync/.formatter.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Used by "mix format"
2+
[
3+
import_deps: [:plug, :ecto, :phoenix_sync],
4+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
5+
]

apps/plug_sync/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# If the VM crashes, it generates a dump, let's ignore it too.
14+
erl_crash.dump
15+
16+
# Also ignore archive artifacts (built via "mix archive.build").
17+
*.ez
18+
19+
# Ignore package tarball (built via "mix hex.build").
20+
plug_sync-*.tar
21+
22+
# Temporary files, for example, from tests.
23+
/tmp/
24+
25+
/persistent/

apps/plug_sync/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PlugSync
2+
3+
A test Plug app for easy validation of Phoenix.Sync features within a real app.
4+
5+
Use `PHOENIX_SYNC_MODE` to set the interaction mode, either `"http"` or `"embedded"`.
6+
7+
In `http` mode, you should run an Electric server on port 3000.

apps/plug_sync/config/config.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Config
2+
3+
import_config "#{config_env()}.exs"

apps/plug_sync/config/dev.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Config
2+
3+
config :plug_sync, ecto_repos: [PlugSync.Repo], start_server: false
4+
5+
case System.get_env("PHOENIX_SYNC_MODE", "embedded") do
6+
"http" ->
7+
IO.puts("Starting in HTTP mode")
8+
9+
config :phoenix_sync,
10+
mode: :http,
11+
env: config_env(),
12+
url: "http://localhost:3000"
13+
14+
config :plug_sync, PlugSync.Repo,
15+
username: "postgres",
16+
password: "password",
17+
hostname: "localhost",
18+
database: "electric",
19+
port: 54321
20+
21+
_ ->
22+
IO.puts("Starting in embedded mode")
23+
24+
config :phoenix_sync,
25+
mode: :embedded,
26+
env: config_env(),
27+
repo: PlugSync.Repo
28+
29+
config :plug_sync, PlugSync.Repo,
30+
username: "postgres",
31+
password: "password",
32+
hostname: "localhost",
33+
database: "plug_sync",
34+
port: 55555
35+
end

apps/plug_sync/config/prod.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import Config

apps/plug_sync/config/test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Config
2+
3+
config :phoenix_sync, mode: :sandbox, env: config_env()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule Mix.Tasks.PlugSync.Insert do
2+
use Mix.Task
3+
4+
@shortdoc "Inserts sample data into the database for testing PlugSync"
5+
6+
alias PlugSync.Repo
7+
alias PlugSync.Tasks.Task
8+
alias PlugSync.Tasks.Step
9+
10+
def run(args) do
11+
{opts, _, _} = OptionParser.parse(args, strict: [rows: :integer, data_size: :integer])
12+
13+
# {:ok, repo} = Repo.start_link()
14+
Application.ensure_all_started(:plug_sync) |> dbg
15+
16+
data_size = Keyword.get(opts, :data_size, 1024)
17+
rows = Keyword.get(opts, :rows, 1024)
18+
19+
Repo.transaction(fn ->
20+
21+
items = Stream.repeatedly(fn -> item(data_size) end) |> Enum.take(rows)
22+
Repo.insert_all(PlugSync.Item, items)
23+
24+
end)
25+
26+
IO.puts("Sample data inserted successfully.")
27+
end
28+
29+
defp item(data_size) do
30+
%{
31+
value: Enum.random(1..100_000),
32+
data: :crypto.strong_rand_bytes(div(data_size , 2) ) |> Base.encode16(),
33+
inserted_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
34+
updated_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
35+
}
36+
end
37+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Mix.Tasks.PlugSync.Server do
2+
use Mix.Task
3+
4+
@shortdoc "Starts a PlugSync server"
5+
6+
def run(_args) do
7+
Application.put_env(:plug_sync, :server, true, persistent: true)
8+
Mix.Tasks.Run.run(run_args())
9+
end
10+
11+
defp run_args do
12+
if iex_running?(), do: [], else: ["--no-halt"]
13+
end
14+
15+
defp iex_running? do
16+
Code.ensure_loaded?(IEx) and IEx.started?()
17+
end
18+
end

apps/plug_sync/lib/plug_sync.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule PlugSync do
2+
@moduledoc """
3+
Documentation for `PlugSync`.
4+
"""
5+
6+
@doc """
7+
Hello world.
8+
9+
## Examples
10+
11+
iex> PlugSync.hello()
12+
:world
13+
14+
"""
15+
def hello do
16+
:world
17+
end
18+
end

0 commit comments

Comments
 (0)