Packages

DSL for building JSON APIs fast. Creates endpoint views, renders linked data automatically.

Current section

12 Versions

Jump to

Compare versions

4 files changed
+16 additions
-8 deletions
  @@ -391,19 +391,22 @@ defmodule UserJSON do
391 391
392 392 cache fn user ->
393 393 %{
394 - plan: Plans.get_active_plan(user.id),
395 - team_plan: Plans.get_team_plan(user.id)
394 + comments: Comments.list_by_user_id(user.id),
395 + teams: Teams.list_teams_by_user_id(user.id)
396 396 }
397 397 end
398 398
399 399 links fn user, cached ->
400 - %{PlanJSON => cached.plan.id}
400 + %{
401 + CommentJSON => Enum.map(cached.comments, fn comment -> comment.id end),
402 + TeamJSON => Enum.map(cached.teams, fn team -> team.id end)
403 + }
401 404 end
402 405
403 406 view fn user, cached ->
404 407 %{
405 408 id: hash(user.id),
406 - plan_id: PlanJSON.hash(cached.plan.id)
409 + comment_count: length(cached.comments)
407 410 }
408 411 end
409 412 end
  @@ -31,4 +31,4 @@
31 31 {<<"optional">>,false},
32 32 {<<"repository">>,<<"hexpm">>},
33 33 {<<"requirement">>,<<"~> 3.6">>}]]}.
34 - {<<"version">>,<<"0.5.0">>}.
34 + {<<"version">>,<<"0.5.1">>}.
  @@ -399,6 +399,11 @@ defmodule Carve.View do
399 399 This macro specifies how to format the entity data for JSON output.
400 400 Accepts a 1-arity function, or a 2-arity function when used with `cache`.
401 401
402 + The `id` returned by the function is also used as the entity's envelope id,
403 + so a view can expose a custom identifier (e.g. a slug) instead of the
404 + hashed id. When the function returns no `id`, the envelope falls back to
405 + `hash(data.id)`.
406 +
402 407 ## Parameters
403 408
404 409 - `func`: A function that takes the entity data (and optionally cached data)
  @@ -422,7 +427,7 @@ defmodule Carve.View do
422 427 view = unquote(func).(data, cached)
423 428
424 429 %{
425 - id: hash(data.id),
430 + id: Map.get(view, :id) || hash(data.id),
426 431 type: type_name(),
427 432 data: view
428 433 }
  @@ -435,7 +440,7 @@ defmodule Carve.View do
435 440 view = unquote(func).(data)
436 441
437 442 %{
438 - id: hash(data.id),
443 + id: Map.get(view, :id) || hash(data.id),
439 444 type: type_name(),
440 445 data: view
441 446 }
  @@ -4,7 +4,7 @@ defmodule Carve.MixProject do
4 4 def project do
5 5 [
6 6 app: :carve,
7 - version: "0.5.0",
7 + version: "0.5.1",
8 8 elixir: "~> 1.12",
9 9 start_permanent: Mix.env() == :prod,
10 10 deps: deps(),