Intercept Identities Behaviour
I have this code
elixir
Incase of a unique error by email found using
eager_check_with
. rerun change again and upsert the record raising the error ? so that the API never errors out.7 Replies
🤔 I think
eager_check_with
wouldn't be interceptable like that
but I think if you did pre_check_with
then you could do it
You might need to do some manual work on this one, but what you could try is using pre_check_with
instead of eager_check_with
and then in your change adding a after_transaction
that looks for your specific kind of error
Nice let me try this
Update my code to this
elixir
The problem is
pre_check_with
clearly states it can only be used in a before_action
hook. The above solution does not work . I can only see the error being thrown by the database("Postgres ") itself.pre_check_with
should check once when the action is submitted. You’re not seeing that?@Zach Daniel Are we taking the correct approach? We're trying to build an upsert-style action that either creates a customer or updates the customer, depending on whether the
email
matches a record in the database.
I see this section on "upserts" in the Identities documentation: https://ash-hq.org/docs/guides/ash/latest/identities#using-upserts
And that suggests we should be using it. Would that make a big difference in the approach? I'm still trying to understand pre_check_with
, and what it does, and how to use it correctly.
Or does using pre_check_with
become moot if we make this an actual upsert?yes 🙂
So
pre_check_with
and eager_check_with
allow you to basically show a validation error early on unique constraint conflicts
like if you wanted a sign up form to show an error on email being taken early on you would do eager_check_with
to have it validated on every validate. If you did pre_check_with
we'd check before submitting the action.
So if you want to upsert, you'd likely do something like this:
And if you want to accept additional fields to be used only in creation, you'd do:
That would create a user w/ email, confirmation_token, foo and bar, and if the user already exists with that email, it would update their confirmation_token onlyIt's working!
I had your solution in place by the time you sent it to me
Thanks @Zach Daniel !
Part of the problem was that we were using
after_action()
incorrectly. The anon fn was matching the wrong pattern.ah, yeah
that works for
after_transaction
after_action
only runs on successes and just gets changeset, result