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

Introduction of the adapter pattern for flexible datasources #608

Merged
merged 71 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
cffd41d
install nimble_options
pehbehbeh Sep 20, 2024
9d0f9a6
add adapters docs group
pehbehbeh Sep 20, 2024
72bcfaa
implement nimble_options validation for LiveResource
pehbehbeh Sep 20, 2024
5150fb4
adjust all demo resources to new adapter_config syntax
pehbehbeh Sep 20, 2024
1ffeca4
add basic upgrade guide
pehbehbeh Sep 20, 2024
53100e6
start implementation of the adapter pattern and a basic ecto adapter
pehbehbeh Sep 20, 2024
fa64768
improve migration guide
pehbehbeh Sep 20, 2024
5dc9d39
Merge branch 'develop' into feature/adapters
pehbehbeh Sep 26, 2024
63f0111
implement get and get! adapter callbacks and move item_query
pehbehbeh Sep 26, 2024
d0c79b4
implement lost adapter callbacks and move more stuff
pehbehbeh Sep 26, 2024
7e137db
remove duplicate code
pehbehbeh Sep 26, 2024
e183212
implement count adapter callback and move more stuff
pehbehbeh Sep 26, 2024
754ade2
remove deprecated Resource.delete function
pehbehbeh Sep 27, 2024
53b5b4e
remove deprecated Resource.metric_data function
pehbehbeh Sep 27, 2024
e194a4d
simplify call_changeset_function
pehbehbeh Sep 27, 2024
6a18042
first steps towards moving insert/update/change stuff to adapters
pehbehbeh Sep 27, 2024
8342bf6
move update_all to adapter
pehbehbeh Sep 27, 2024
e2cf868
please credo
pehbehbeh Sep 27, 2024
928e384
change primary_key to be a live_resource setting
pehbehbeh Oct 4, 2024
5993ca2
implement very basic draft of an ash adapter
pehbehbeh Oct 4, 2024
89dbafe
fix unused alias
pehbehbeh Oct 4, 2024
2224ba9
fix Resource.get
pehbehbeh Oct 4, 2024
a7978c5
add workaround for ecto seeds
pehbehbeh Oct 4, 2024
4c90ac7
Merge branch 'develop' into feature/adapters
pehbehbeh Oct 18, 2024
f1a7839
implement init_order changes from #628 for new structure
pehbehbeh Oct 18, 2024
258579a
add warnings to adapter and ash adapter
pehbehbeh Oct 18, 2024
edee0ea
change blog category back to ecto
pehbehbeh Oct 22, 2024
f292ba0
improve docs
pehbehbeh Oct 22, 2024
9b500df
add ash helpdesk demo
pehbehbeh Oct 22, 2024
dad4b74
please the linters
pehbehbeh Oct 22, 2024
8c2df14
fix formatting
pehbehbeh Oct 22, 2024
64b23e1
Format migration
Flo0807 Oct 25, 2024
2718d0a
Include migration in demo `.formatter.exs`
Flo0807 Oct 25, 2024
a76273d
Remove subdirectories from backpex `.formatter.exs`
Flo0807 Oct 25, 2024
9dc332b
Update ID for helpdesk sidebar section
Flo0807 Oct 25, 2024
228486f
Update ticket alert colors
Flo0807 Oct 25, 2024
e688cda
Update film reviews alert colors
Flo0807 Oct 25, 2024
8a1af81
Update ticket alert text
Flo0807 Oct 25, 2024
8a91363
Use guard instead of adding another function pattern
Flo0807 Oct 25, 2024
d7fa361
Seed categories again
Flo0807 Oct 25, 2024
56d8541
Update adapter warning text
Flo0807 Oct 25, 2024
316f9d7
Update ash adapter docs
Flo0807 Oct 25, 2024
6672096
Disable ordering in ash resource
Flo0807 Oct 25, 2024
521c9df
Move ticket seeding
Flo0807 Oct 25, 2024
09cd3b0
Add missing doc comments
Flo0807 Oct 25, 2024
672c314
Add note for anonymous functions for init order configuration
Flo0807 Oct 25, 2024
7bc03bf
Change ash `delete_all` to not implemented
Flo0807 Oct 25, 2024
e2fa7af
Fix warning in upgrade guide
Flo0807 Oct 25, 2024
2cb3385
Add missing backquote
Flo0807 Oct 25, 2024
d7c1ec6
Add default for `after_save_fun` param
Flo0807 Oct 25, 2024
df157c7
Update pubsub configuration
Flo0807 Oct 25, 2024
28b5b16
Fix index edit does not work
Flo0807 Oct 25, 2024
5b13efe
Update lib/backpex/adapters/ash.ex
Flo0807 Oct 25, 2024
8c2014e
Add warning to ecto adapter doc
Flo0807 Oct 25, 2024
bcc866a
Update pubsub configuration
Flo0807 Oct 25, 2024
e765249
Remove backpex prefix in pubsub docs
Flo0807 Oct 25, 2024
158a750
Add upgrade guide to extras
Flo0807 Oct 28, 2024
96ff2d4
Improve upgrade guide
Flo0807 Oct 28, 2024
8a669d5
Pass assocs to changeset functions
Flo0807 Oct 28, 2024
49b9bae
Rename datalayer to data layer
Flo0807 Oct 28, 2024
4eeeda8
Fix index edit
Flo0807 Oct 28, 2024
939a54a
Use correct primary key for has many through
Flo0807 Oct 28, 2024
d44b3c0
Update docs
Flo0807 Oct 28, 2024
c0f3f84
Ensure Ash is loaded in ash adapter
Flo0807 Oct 28, 2024
4ce88ea
Update list function signature
Flo0807 Oct 28, 2024
e39615f
Update count function signature
Flo0807 Oct 28, 2024
d24de8b
Update item query guide
Flo0807 Oct 28, 2024
df0be0e
Update ordering guide
Flo0807 Oct 28, 2024
d64704f
Update upgrade guide
Flo0807 Oct 28, 2024
ad75a39
Update upgrade guide
Flo0807 Oct 28, 2024
1a596e0
Update upgrade guide
Flo0807 Oct 28, 2024
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
6 changes: 3 additions & 3 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
strict: true,
parse_timeout: 5000,
color: true,
checks: %{
disabled: []
}
checks: [
{Credo.Check.Design.TagTODO, false}
]
}
]
}
3 changes: 1 addition & 2 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ locals_without_parens = [

[
line_length: 120,
import_deps: [:ecto, :phoenix],
import_deps: [:ecto, :phoenix, :ash, :ash_postgres],
plugins: [TailwindFormatter, Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"],
subdirectories: ["priv/*/migrations"],
locals_without_parens: locals_without_parens,
export: [locals_without_parens: locals_without_parens]
]
5 changes: 3 additions & 2 deletions demo/.formatter.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
line_length: 120,
import_deps: [:ecto, :phoenix, :backpex],
import_deps: [:ecto, :phoenix, :backpex, :ash, :ash_postgres],
plugins: [TailwindFormatter, Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"]
inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"],
subdirectories: ["priv/*/migrations"]
]
7 changes: 6 additions & 1 deletion demo/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Config

config :demo,
namespace: Demo,
ecto_repos: [Demo.Repo],
ecto_repos: [Demo.Repo, Demo.RepoAsh],
ash_domains: [Demo.Helpdesk],
generators: [binary_id: true]

config :demo, DemoWeb.Endpoint,
Expand Down Expand Up @@ -48,6 +49,10 @@ config :phoenix, :json_library, Jason

config :demo, DemoWeb.Gettext, default_locale: "en"

config :ash, include_embedded_source_by_default?: false

config :ash, :policies, no_filter_static_forbidden_reads?: false

config :backpex, :translator_function, {DemoWeb.CoreComponents, :translate_backpex}

config :backpex, :error_translator_function, {DemoWeb.CoreComponents, :translate_error}
Expand Down
10 changes: 9 additions & 1 deletion demo/config/runtime.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Config
import System, only: [get_env: 1, get_env: 2, fetch_env!: 1]
import String, only: [to_integer: 1, to_atom: 1, to_existing_atom: 1, split: 2]
import String, only: [to_integer: 1, to_atom: 1, to_existing_atom: 1]

config :demo, analytics: get_env("ANALYTICS", "false") |> to_existing_atom()

Expand All @@ -12,6 +12,14 @@ config :demo, Demo.Repo,
port: get_env("DB_PORT", "5432") |> to_integer(),
pool_size: get_env("DB_POOL_SIZE", "5") |> to_integer()

config :demo, Demo.RepoAsh,
hostname: get_env("DB_HOSTNAME", "postgres"),
username: get_env("DB_USERNAME", "postgres"),
password: get_env("DB_PASSWORD", "postgres"),
database: get_env("DB_DATABASE", "postgres"),
port: get_env("DB_PORT", "5432") |> to_integer(),
pool_size: get_env("DB_POOL_SIZE", "5") |> to_integer()

config :demo, DemoWeb.Endpoint,
http: [
port: get_env("PORT", "4000") |> to_integer()
Expand Down
1 change: 1 addition & 0 deletions demo/lib/demo/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Demo.Application do
{Cluster.Supervisor, [topologies, [name: Sourceboat.ClusterSupervisor]]},
{Phoenix.PubSub, name: Demo.PubSub},
Demo.Repo,
Demo.RepoAsh,
DemoWeb.Telemetry,
{DemoWeb.MetricsStorage, DemoWeb.Telemetry.metrics()},
DemoWeb.Endpoint
Expand Down
8 changes: 8 additions & 0 deletions demo/lib/demo/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Demo.Factory do

alias Demo.Address
alias Demo.Category
alias Demo.Helpdesk.Ticket
alias Demo.Post
alias Demo.Product
alias Demo.ShortLink
Expand Down Expand Up @@ -91,6 +92,13 @@ defmodule Demo.Factory do
}
end

def ticket_factory do
%Ticket{
subject: Faker.Lorem.sentence(),
body: Faker.Lorem.paragraph()
}
end

defp boolean do
Enum.random(0..1) == 1
end
Expand Down
24 changes: 24 additions & 0 deletions demo/lib/demo/helpdesk/ticket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Demo.Helpdesk.Ticket do
@moduledoc false

use Ash.Resource,
domain: Demo.Helpdesk,
data_layer: AshPostgres.DataLayer

postgres do
repo Demo.RepoAsh
table "tickets"
end

actions do
defaults [:read]
end

attributes do
uuid_primary_key :id
attribute :subject, :string, allow_nil?: false
attribute :body, :string, allow_nil?: false
create_timestamp :inserted_at
update_timestamp :updated_at
end
end
8 changes: 8 additions & 0 deletions demo/lib/demo/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ defmodule Demo.Repo do
otp_app: :demo,
adapter: Ecto.Adapters.Postgres
end

defmodule Demo.RepoAsh do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rename Demo.Repo to Demo.RepoEcto accordingly.

use AshPostgres.Repo, otp_app: :demo

def installed_extensions do
["ash-functions"]
end
end
9 changes: 9 additions & 0 deletions demo/lib/demo/support.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Demo.Helpdesk do
@moduledoc false

use Ash.Domain

resources do
resource Demo.Helpdesk.Ticket
end
end
6 changes: 6 additions & 0 deletions demo/lib/demo_web/components/layouts/admin.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
<Backpex.HTML.CoreComponents.icon name="hero-tag" class="h-5 w-5" /> Tags
</Backpex.HTML.Layout.sidebar_item>
</Backpex.HTML.Layout.sidebar_section>
<Backpex.HTML.Layout.sidebar_section id="helpdesk">
<:label>Helpdesk</:label>
<Backpex.HTML.Layout.sidebar_item current_url={@current_url} navigate="/admin/tickets">
<Backpex.HTML.CoreComponents.icon name="hero-book-open" class="h-5 w-5" /> Tickets
</Backpex.HTML.Layout.sidebar_item>
</Backpex.HTML.Layout.sidebar_section>
</:sidebar>
<Backpex.HTML.Layout.flash_messages flash={@flash} />
<%= @inner_content %>
Expand Down
6 changes: 2 additions & 4 deletions demo/lib/demo_web/item_actions/soft_delete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ defmodule DemoWeb.ItemActions.SoftDelete do

socket =
try do
%{assigns: %{repo: repo, schema: schema, pubsub: pubsub}} = socket

{:ok, _items} =
Backpex.Resource.update_all(items, repo, schema, [set: [deleted_at: datetime]], "deleted", pubsub)
updates = [set: [deleted_at: datetime]]
{:ok, _items} = Backpex.Resource.update_all(items, updates, "deleted", socket.assigns.live_resource)

socket
|> clear_flash()
Expand Down
18 changes: 11 additions & 7 deletions demo/lib/demo_web/live/address_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule DemoWeb.AddressLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.Address,
repo: Demo.Repo,
update_changeset: &Demo.Address.update_changeset/3,
create_changeset: &Demo.Address.create_changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Address,
repo: Demo.Repo,
update_changeset: &Demo.Address.update_changeset/3,
create_changeset: &Demo.Address.create_changeset/3,
pubsub: Demo.PubSub,
topic: "addresses",
event_prefix: "address_",
pubsub: [
name: Demo.PubSub,
topic: "addresses",
event_prefix: "address_"
],
fluid?: true

@impl Backpex.LiveResource
Expand Down
18 changes: 11 additions & 7 deletions demo/lib/demo_web/live/category_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule DemoWeb.CategoryLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.Category,
repo: Demo.Repo,
update_changeset: &Demo.Category.update_changeset/3,
create_changeset: &Demo.Category.create_changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Category,
repo: Demo.Repo,
update_changeset: &Demo.Category.update_changeset/3,
create_changeset: &Demo.Category.create_changeset/3,
pubsub: Demo.PubSub,
topic: "categories",
event_prefix: "category_"
pubsub: [
name: Demo.PubSub,
topic: "categories",
event_prefix: "category_"
]

@impl Backpex.LiveResource
def singular_name, do: "Category"
Expand Down
20 changes: 12 additions & 8 deletions demo/lib/demo_web/live/film_review_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule DemoWeb.FilmReviewLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.FilmReview,
repo: Demo.Repo,
update_changeset: &Demo.FilmReview.update_changeset/3,
create_changeset: &Demo.FilmReview.create_changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.FilmReview,
repo: Demo.Repo,
update_changeset: &Demo.FilmReview.update_changeset/3,
create_changeset: &Demo.FilmReview.create_changeset/3,
pubsub: Demo.PubSub,
topic: "film_reviews",
event_prefix: "film_reviews_",
pubsub: [
name: Demo.PubSub,
topic: "film_reviews",
event_prefix: "film_reviews_"
],
full_text_search: :generated_tsvector

@impl Backpex.LiveResource
Expand All @@ -19,7 +23,7 @@ defmodule DemoWeb.FilmReviewLive do
@impl Backpex.LiveResource
def render_resource_slot(assigns, :index, :before_page_title) do
~H"""
<div class="alert my-4 bg-blue-100 text-sm text-blue-800">
<div role="alert" class="alert alert-info my-4 text-sm">
<Backpex.HTML.CoreComponents.icon name="hero-information-circle" class="h-5 w-5" />
<span>
This resource uses the full-text search functionality. The search accepts web search query operators. For example, a dash (-) excludes words.
Expand Down
18 changes: 11 additions & 7 deletions demo/lib/demo_web/live/invoice_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule DemoWeb.InvoiceLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.Invoice,
repo: Demo.Repo,
update_changeset: &Demo.Invoice.update_changeset/3,
create_changeset: &Demo.Invoice.create_changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Invoice,
repo: Demo.Repo,
update_changeset: &Demo.Invoice.update_changeset/3,
create_changeset: &Demo.Invoice.create_changeset/3,
pubsub: Demo.PubSub,
topic: "invoices",
event_prefix: "invoice_"
pubsub: [
name: Demo.PubSub,
topic: "invoices",
event_prefix: "invoice_"
]

@impl Backpex.LiveResource
def singular_name, do: "Invoice"
Expand Down
18 changes: 11 additions & 7 deletions demo/lib/demo_web/live/post_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule DemoWeb.PostLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.Post,
repo: Demo.Repo,
update_changeset: &Demo.Post.update_changeset/3,
create_changeset: &Demo.Post.create_changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Post,
repo: Demo.Repo,
update_changeset: &Demo.Post.update_changeset/3,
create_changeset: &Demo.Post.create_changeset/3,
pubsub: Demo.PubSub,
topic: "posts",
event_prefix: "post_",
pubsub: [
name: Demo.PubSub,
topic: "posts",
event_prefix: "post_"
],
fluid?: true

@impl Backpex.LiveResource
Expand Down
18 changes: 11 additions & 7 deletions demo/lib/demo_web/live/product_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
defmodule DemoWeb.ProductLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.Product,
repo: Demo.Repo,
update_changeset: &Demo.Product.changeset/3,
create_changeset: &Demo.Product.changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Product,
repo: Demo.Repo,
update_changeset: &Demo.Product.changeset/3,
create_changeset: &Demo.Product.changeset/3,
pubsub: Demo.PubSub,
topic: "products",
event_prefix: "product_"
pubsub: [
name: Demo.PubSub,
topic: "products",
event_prefix: "product_"
]

@impl Backpex.LiveResource
def singular_name, do: "Product"
Expand Down
19 changes: 12 additions & 7 deletions demo/lib/demo_web/live/short_link_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ defmodule DemoWeb.ShortLinkLive do
use DemoWeb, :verified_routes

use Backpex.LiveResource,
adapter_config: [
schema: Demo.ShortLink,
repo: Demo.Repo,
update_changeset: &Demo.ShortLink.changeset/3,
create_changeset: &Demo.ShortLink.changeset/3
],
primary_key: :short_key,
layout: {DemoWeb.Layouts, :admin},
schema: Demo.ShortLink,
repo: Demo.Repo,
update_changeset: &Demo.ShortLink.changeset/3,
create_changeset: &Demo.ShortLink.changeset/3,
pubsub: Demo.PubSub,
topic: "short_links",
event_prefix: "short_link_"
pubsub: [
name: Demo.PubSub,
topic: "short_links",
event_prefix: "short_link_"
]

@impl Backpex.LiveResource
def singular_name, do: "Short Link"
Expand Down
19 changes: 12 additions & 7 deletions demo/lib/demo_web/live/tag_live.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
defmodule DemoWeb.TagLive do
use Backpex.LiveResource,
adapter_config: [
schema: Demo.Tag,
repo: Demo.Repo,
update_changeset: &Demo.Tag.update_changeset/3,
create_changeset: &Demo.Tag.create_changeset/3
],
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Tag,
repo: Demo.Repo,
update_changeset: &Demo.Tag.update_changeset/3,
create_changeset: &Demo.Tag.create_changeset/3,
pubsub: Demo.PubSub,
topic: "tags",
event_prefix: "tag_"
pubsub: [
name: Demo.PubSub,
topic: "tags",
event_prefix: "tag_"
],
init_order: %{by: :name, direction: :desc}

@impl Backpex.LiveResource
def singular_name, do: "Tag"
Expand Down
Loading
Loading