FedericoAlcantara

FedericoAlcantara

Aurora CTX - A DSL to reduce code boilerplate for context / Ecto schemas

Aurora.Ctx is a code generation library that implements the Context pattern in Phoenix applications.

It automatically generates standardized database interaction functions following Phoenix’s architectural guidelines, while maintaining full compatibility with Ecto’s query composition and changesets.

The library supports both standard CRUD operations and advanced query features like filtering, sorting, and pagination.

See the documentation in hex

https://github.com/wadvanced/aurora_ctx

Most Liked

Asd

Asd

I never understood the use-case for these libraries, but I’ve seen a lot of them in my career.

First of all, it is not saving any typing, essentially providing oneliners replacing other onelines

list_products()                    # Repo.all(Products)
list_products(opts)                # Repo.all(where(Products, ...))
list_products_paginated()          # Repo.paginate(Products)
list_products_paginated(opts)      # Repo.paginate(where(Products))
count_products()                   # Repo.aggregate(Products, :count)
count_products(opts)               # Repo.aggregate(where(Products, ...), :count)

# Create operations
create_product(%{name: "Item"})    # Repo.insert(Product.changeset(...))
create_product!(%{name: "Item"})   # Repo.insert!(Product.changeset(...))
create_product()                   # Repo.insert(%Product{})
create_product!()                  # Repo.insert!(%Product{})

# Read operations
get_product(1)                     # Repo.get(Product, 1)
get_product!(1)                    # Repo.fetch!(Product, 1)
get_product(1, preload: [:items])  # Repo.get(Product, 1) |> Repo.preload()
get_product!(1, preload: [:items]) # Repo.fetch!(Product, 1) |> Repo.preload()

# Update operations
update_product(product, attrs)     # Repo.update(Product.changeset(product, attrs))
update_product(product)            # Update without new attributes (??????? why would anyone needs this)
change_product(product, attrs)     # Product.changeset(product, attrs)
change_product(product)            # Product.changeset(product, attrs)

# Delete operations
delete_product(product)            # Product.delete(product)
delete_product!(product)           # Product.delete!(product)

# Initialize operations
new_product()                      # %Product{}
new_product(attrs)                 # struct(Product, attrs)
new_product(attrs, opts)           # ??????

Second, it makes it impossible to search for definition. When I will go to Product module, I won’t be able to find what the list_products function does, because I can’t see it

Third, every time I used this approach, it quickly became unmaintainable, because CRUD operations which just do plain CRUD are extremely rare, and most of them usually contain some business logic which is reflected in query (like, for example, product ownership checks, soft-deletion checks, category checks, normalized form preloading, etc.)

So every time I used one of these libraries, they became a huge frustration for me and other devs

FedericoAlcantara

FedericoAlcantara

Just published a new version. With two minor enhancement:

  • The generation of get_by function for the schema.
  • Custom changeset can also be defined by function reference.

:slightly_smiling_face:

See the documentation at hex
or the code in GitHub wadvanced/aurora_ctx

FedericoAlcantara

FedericoAlcantara

Lawik

These context functions are to be used, for example, in Phoenix applications. In that way, the view (Phoenix) does not have to know about how the data is accessed (Repo).

That is one intended use case.

Where Next?

Popular in Announcing Top

danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
tmbb
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
mikehostetler
I’m excited to announce Jido, a framework providing foundational primitives for building autonomous agent systems in Elixir. While develo...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10954 107
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement