Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
90f029e
feat: allow passing Ecto query and preloads as list_... function args
vincentvanbush Feb 4, 2025
7b00130
feat: allow calling list function with schema module
vincentvanbush Feb 4, 2025
ae98381
Fix formatter indications
vincentvanbush Feb 4, 2025
644faf2
feat: add preloads to get_* and create get_*_by function
vincentvanbush Feb 4, 2025
45116a8
feat: add query builder module
vincentvanbush Feb 5, 2025
ca6339a
chore: add ecto dependency
vincentvanbush Feb 5, 2025
aea208c
chore: import formatter settings from ecto
vincentvanbush Feb 5, 2025
83029ee
feat: add query building based on plain keyword lists
vincentvanbush Feb 5, 2025
d9c913f
feat: query builder can now take nil as seeked values
vincentvanbush Feb 5, 2025
03cba0c
feat: add doc to query builder
vincentvanbush Feb 5, 2025
d6957ae
chore: update credo
vincentvanbush Feb 5, 2025
ff90ab1
chore: fix credo indication in Tracer
vincentvanbush Feb 5, 2025
afd57b6
feat: rework get_*_by and list_* API to allow preloads, queries and c…
vincentvanbush Feb 5, 2025
930e0fa
chore: update deps to suppress warnings
vincentvanbush Feb 5, 2025
9240e02
add specs
vincentvanbush Feb 5, 2025
decb073
add framework for Ecto testing, tests for list functions
vincentvanbush Feb 5, 2025
f219d61
add tests for preloads together with condition lists
vincentvanbush Feb 5, 2025
5e063ad
separate test section for list_*/2
vincentvanbush Feb 5, 2025
1c2c20f
add test and fixes to get_*_by functions
vincentvanbush Feb 6, 2025
85283b3
fix and add tests to get_* functions
vincentvanbush Feb 6, 2025
e61348f
update docs for get and list functions
vincentvanbush Feb 6, 2025
50b5a67
chore: add postrges to CI
vincentvanbush Feb 6, 2025
6bc91cc
chore: fix credo indications
vincentvanbush Feb 6, 2025
321a6e3
remove unused functions
vincentvanbush Feb 7, 2025
60b1c97
update readme
vincentvanbush Feb 7, 2025
dd56b7c
feat: add order_by, limit and offset options
vincentvanbush Feb 10, 2025
6cc5095
feat: add association counters
vincentvanbush Feb 13, 2025
40a0210
WIP: tests
vincentvanbush Feb 13, 2025
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
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
import_deps: [:ecto]
]
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ jobs:
elixir-version: ${{ env.elixir_version }}
otp-version: ${{ env.otp_version }}

- name: Set up Postgres
run: |
sudo apt-get update
sudo apt-get install -y postgresql
sudo service postgresql start
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres';"

- name: Restore dependencies and _build cache
uses: actions/cache@v2
with:
Expand All @@ -48,6 +55,11 @@ jobs:
- name: Check formatting
run: mix format --check-formatted

- name: Create test database
run: mix do ecto.create, ecto.migrate
env:
MIX_ENV: test

- name: Run tests
run: mix test

Expand Down
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.14.4
erlang 25.3
elixir 1.18.2
erlang 27.2.1
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ Read more about `Contexted.Delegator` and its options in [docs](https://hexdocs.

### Don't repeat yourself with CRUD operations

In most web apps CRUD operations are very common. Most of these, have the same pattern. Why not autogenerate them?
In most web apps CRUD operations are very common. Most of these, have the same pattern. Most of the time, they are used with preloading associated resources as well as filtering based on conditions such as search, pagination, etc.
Why not autogenerate them?

Here is how you can generate common CRUD operations for `App.Account.Users`:

Expand All @@ -242,14 +243,60 @@ iex> App.Accounts.Users.__info__(:functions)
delete_user!: 1,
get_user: 1,
get_user!: 1,
get_user_by: 1,
get_user_by!: 1,
get_user_by: 2,
get_user_by!: 2,
list_users: 0,
list_users: 1,
list_users: 2,
update_user: 1,
update_user: 2,
update_user!: 1,
update_user!: 2
]
```

Generated creation and updating functions default to the corresponding schema's `changeset/1` and `changeset/2` functions, respectively, whereas list and get functions provide a means to manipulate the result by:

* filtering conditions (via plain exact match condition lists or by passing an Ecto.Query)
* preloads
* orderings
* limits
* offsets

Examples:

```elixir
# List all users with posts preloaded
iex> App.Accounts.Users.list_users(preload: [:posts])

# Use an Ecto.Query to filter users, and a keyword list of options to manipulate the result
iex> App.Accounts.Users.list_users(
App.Accounts.User |> where([u], u.status == "active"),
preload: [:posts],
order_by: [desc: :inserted_at],
limit: 10,
offset: 0
)

# Use a keyword list of exact match conditions and manipulation options
iex> App.Accounts.Users.list_users(
status: "active",
subscription: [plan: "free"],
order_by: [desc: :inserted_at]
)

# Get a user by ID with subscription preloaded
iex> App.Accounts.Users.get_user!(10, preload: [:subscription])

# Get a user by profile email with profile and subscription preloaded
iex> App.Accounts.Users.get_user_by!(profile: [email: "user@example.com"], preload: [:profile, :subscription])

# Use an Ecto.Query to get a specific user
iex> App.Accounts.Users.get_user_by!(App.Accounts.User |> where([u], u.id == 10), preload: [:profile, :subscription])
```

Read more about `Contexted.CRUD` and its options in [docs](https://hexdocs.pm/contexted/Contexted.CRUD.html).

<br/>
Expand Down
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Config

import_config "#{Mix.env()}.exs"
3 changes: 3 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Config

# no-op: we don't need to configure anything for the dev environment
13 changes: 13 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Config

config :contexted, ecto_repos: [Contexted.TestApp.Repo]

config :contexted, Contexted.TestApp.Repo,
database: "contexted_test",
username: "postgres",
password: "postgres",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox,
show_sensitive_data_on_connection_error: true,
pool_size: 10,
log: false
Loading
Loading