Ash Framework

AF

Ash Framework

The Elixir backend framework for unparalleled productivity. Declarative tools that let you stop wasting time. Use with Phoenix LiveView or build APIs in minutes for your front-end of choice.

Join

support

showcase

today-i-learned

ash_oban discarded jobs with `trigger_no_longer_applies`

I'm using ash_oban, and I keep seeing quite a few jobs getting discarded with ** (Oban.PerformError) MyApp.SomeDomain.SomeResource.AshOban.Worker.SomeTrigger failed with {:discard, :trigger_no_longer_applies}. It's quite odd because as far as I can tell, it seems like the jobs are running when I'd expect them to, however I see my oban jobs littered with many of these discarded jobs. I'm curious if this is something that is common/expected in ash_oban, or if it could potentially signal some deeper issue with my setup. I've started observing some very strange timing bugs recently (the details of which I won't go into here) which are making me suspect these errors could be indicative of something wrong on a deeper level. I'm hoping to at least get a feel for how to interpret these errors, which I can then use to go debug the setup I have....
Solution:
It happens if jobs are completed in between the scheduler running it's read query and scheduling the jobs. https://hexdocs.pm/ash_oban/dsl-ashoban.html#oban-triggers-trigger-trigger_once? and when the second job runs the read action doesn't return the record anymore because the previous job already set the state to something else, which makes the where condition not apply anymore. If that happens the job is discarded...

Tenancy create schema concurrency errors on Tests

```elixir 64) test Organization resource actions can create organization without slug (no validation currently) (.Accounts.OrganizationTest) test//accounts/organization_test.exs:49 match (=) failed code: assert {:ok, organization} = Accounts.create_organization(attrs, authorize?: false)...

Is it possible to use LangChain.NativeTools?

I tried creating the following resource to build a simple lookup prompt action. The idea is that someone can put some search term into it and have the LLM look up the NPI registry information using a Google search. The LangChain docs show a usage example here and I tried recreating it with an Ash resource here: ```elixir defmodule Prism.NPI.Lookup do use Ash.Resource, extensions: [AshAi], domain: Prism.NPI...
Solution:
It's slightly more verbose but converting it to a generic action wasn't so bad: ```elixir defmodule Prism.NPI.Lookup do use Ash.Resource, extensions: [AshAi], domain: Prism.NPI...

In ash oban infer/bypass tenancy for action

Hello, I have a AshOban trigger that runs an update action. In the update action you cannot specify multitenancy :bypass so it errors out with changesets require a tenant to be specified this also if I'm specifying a read action that bypasses the tenancy. What is the best practice here allow global true in the multitenancy block?...

Using a relationship in a validation (atomicly)

I couldn't easily find this in the docs, but can use refer to relationships (easily) in validations? For example, we have widgets with a width and x property. Widgets belong_to a Scene that have a width property. We want to make sure that a Widget's x + width property is less than it's parent Scene's width? I assume you need to use a custom validation for this. But can you make that custom validation atomic somehow?...

How to use `after_action` when soft delete is enabled in global `Change`

Hi, Sorry! I have a global change that i am using inside my many resources! the problem i have when i use soft delete Ash.destroy!(Ash.get!(MishkaCms.Runtime.Site, id)) it is not triggerd It does not work ```elixir defmodule MishkaCms.Runtime.Resources.Changes.SendToOban do...
Solution:
So it could be that it uses the atomic callback depending on how you call your action and your atomic implementation isn't doing anything. But as your change only has an after_action and that's allowed in the context of atomics you can try this: ```elixir defmodule MishkaCms.Runtime.Resources.Changes.SendToOban do use Ash.Resource.Change...

Drop unneeded column via codegen

Hi! I'm refactoring a resource and removed all references to office_id from my ParkingLot resource, replacing it with a belongs_to :site relationship. However, when I run mix ash.codegen, the migration generator is renaming the column instead of dropping the old one and creating a new one: ``` Like this instead of drop/add rename table(:parking_lots), :office_id, to: :site_id...
Solution:
When I clicked no, the generator added the new column but left the old one, which is fine to be honest. What I ended up doing is first removing all references of the old relationship, then running codegen. This removed the old column. Then I added the new relationships to the new table and ran codegen again. Worked perfect, and I kinda like that it forced me to make more granular migrations to drop and then create.

Redirect on successful authentication

This seems like a trivial issue but I can't seem to figure out how to do it. Suppose: 1. A user clicks the account button in the navbar but is currently logged out. 2. Ash authentication helpers redirect them to /sign-in. 3. They log in successfully....

How to implement Authentication with Multiple OAuth2 Providers?

I have have already implemented email/password auth and now I want to add OAuth2 login with multiple providers (Google and Facebook). I saw the guide for Google, but not sure how to set up multiple providers together.

Is It Possible to Track Create and Update Operations at the Domain Level for AshOban?

Hi, sorry, Is it possible to track create and update operations at the domain level of all resources under this domain, so that whenever such actions occur, their information is sent to AshOban for further processing? Something like ```elixir...
Solution:
Yeah nothing builtin to help you do that. Your best bet is likely to have custom Oban jobs for the compilation you want to do and then to use global changes to start those jobs in the same transaction on each resource.

Upsert with ON CONFLICT DO NOTHING

Is it possible to configure an upsert to do ON CONFLICT DO NOTHING?
Solution:
Yeah that would likely do it 🙂

Ash update action inside Ecto transaction, trying to not rollback if transaction is rolled back

I have a validation that (probably?) happens in a transaction outside of Ash, and I'd like to run an Ash action if validation fails. But right now when the transaction is rolled back my Ash update action is being rolled back too, even if wrapped in a new Ash.DataLayer.transaction. Any ideas how I could have it not be rolled back?

How can I show all validation errors (including nested) at once?

I'm using manage_relationship/4 in a top-level action that accepts nested fields via arguments. Right now, the nested validation errors only show after the top-level data is valid. Is there a way to surface all validation errors (parent + nested) simultaneously? I'd like users to see everything in one go
Solution:
If you have time to make a PR, what needs to happen is something like this: ```elixir form |> AshPhoenix.Form.raw_errors(for_path: :all)...

Do you have special settings for Credo for Ash projects?

I just added Credo to a project that is getting big, the amount of fixes are huge, a good part of it due to automatic code generations. The gepeto suggest: ```elixir...

Losing entries for nested forms on validate

To reproduce: - Have form for update action with manage_relationship(:resource, type: :direct_control). - Have a button to add new form: ``` def handle_event("add-form", %{"path" => path}, socket) do...
Solution:
I think you're missing pattern matching the actual params out

AshAuthentication with non-Postgres Data Layers?

I am writing a small "microservice" which takes a JSON post from an internal system and does a side effect (i.e. validates it, processes it based on if CAD drawings in a directory exist or not, then leaves a custom CSV in a directory being watched by vendor software). I have it working without Ash (just using OpenAPISpex and phx gen.auth api keys) but I need to rewrite it so the release will run as a Windows Service for performance reasons (don't ask!! 😰) and thought this would be a great very easy way to start learning some Ash resources, actions, and json_api at least on a simple easy project! The API and side effect generating stuff works great (simple/embedded data layer) I just need to add api key authentication so I added AshAuthentication. Looking at the code it seems agnostic to the data layer but every example online all assumes AshPostgres and this may be the only way to get it to work. This service is not publicly-accessible and of course HTTPS is used but I still want some sort of key to be checked even if the hash is stored in an env var and read by runtime config then checked. Or in SQLite data layer or a JSON/CSV file or something. I just don't want to add the complexity of adding PGSQL on Windows to this simple deployment....

AshJsonAPI - How to pass a tenant?

I am running curl commands against my AshJsonAPI endpoint and, as expected, I receive a 'tenant must be set' error as I am not passing the tenant.
curl -XPOST -H 'Content-Type: application/vnd.api+json' localhost:4000/api/json/items -d '{"data": {"type": "item", "attributes": {"name": "test", "provider_id": "01985d4d-f0c4-73b5-8919-29946afeb08c", "deploys": []}}}'
curl -XPOST -H 'Content-Type: application/vnd.api+json' localhost:4000/api/json/items -d '{"data": {"type": "item", "attributes": {"name": "test", "provider_id": "01985d4d-f0c4-73b5-8919-29946afeb08c", "deploys": []}}}'
...

How to use Heroicons?

Hi everyone, I have a brand new Ash project and have the following import in mix.exs: ``` {:heroicons, github: "tailwindlabs/heroicons",...
Solution:
I just had to read the docs!! :p https://hexdocs.pm/heroicons/Heroicons.html...

Error tenant required in calculation on tenant resource

Hello! I'm getting an error I don't really know how to solve. It might be a dumb error from my side. I'm trying to count the number of rows of a resource inside a tenant (the 'items' table) and have it as a calculation on my tenant (the 'project' resource). This way I can know from the outside of the multi-tenancy world how many items are inside a project....

Convenience functions for many_to_many relationships?

I'm trying to wrap my head around creating convenience functions for many_to_many relationships. Given the resources below, what I'd like to do is have an add_to_hub action in the User module that takes in a Hub record and creates a corresponding UsersToHubs record. How would I go about setting up that action, and is placing a function like this inside of my User resource module at all idiomatic for Ash? I cut off the TestApp.Hubs.Hub module due to character limit, but I set it up the same way as my User module...
Solution:
I appreciate the very kind "read the manual" haha. Definitely overlooked the many_to_many example when I was reading the docs earlier. So it's basically just: ```elixir update :assign_to_hub do...