lamp-town-guy
lamp-town-guy
AEAsh Elixir
Created by lamp-town-guy on 3/27/2023 in #support
Override resource field before validation
I have a slug field that is based on a name. I want to be able to set it from title by something like slugify in Django. But I'm not able to override nil values of fields before they get validated in create action. I've used change but it is not called when slug is nil. I don't want to do it before calling create action because it just doesn't feel right and dry. If I enable this action through an API and web interface than it will be 2 places to convert title to slug. In case you don't know what slug is, here's wikipedia article. https://en.wikipedia.org/wiki/Slug_%28publishing%29 It is used in URL to uniquely identify a resource and this term comes from publishing. URL safe title is not fast to write is it? Django is my background where this term is frequently used.
5 replies
AEAsh Elixir
Created by lamp-town-guy on 3/16/2023 in #support
How to replace Ecto.Schema modifications?
So I have my custom ULID implementation. It takes UUID format as well as BASE64 url encoded and transforms it to UUID in database layer. When taking out of DB it's transformed to BASE64 again. It creates shorter URLs which is nice. With ecto I needed just 2 lines of code to have all database IDs in this format. But I don't know what's the best way to do it in ash. Also I've modified timestamps to be utc_datetime_usec instead of naive datetime ones. It's easier to convert them to different timezones that way. Here's the code:
defmodule App.Schema do
defmacro __using__(_env) do
quote do
use Ecto.Schema

@timestamps_opts [
type: :utc_datetime_usec
]
@primary_key {:id, App.Schema.ULID, autogenerate: true}
@foreign_key_type App.Schema.ULID

import Ecto.Changeset
import Ecto.Query
end
end
end
defmodule App.Schema do
defmacro __using__(_env) do
quote do
use Ecto.Schema

@timestamps_opts [
type: :utc_datetime_usec
]
@primary_key {:id, App.Schema.ULID, autogenerate: true}
@foreign_key_type App.Schema.ULID

import Ecto.Changeset
import Ecto.Query
end
end
end
All I needed to do then was to use App.Schema and it was done. Is there a similar way to do it in Ash? Or at least a way to add ULID in this convenient as a separate data type? In case anyone is interested into my ULID implementation here's gist https://gist.github.com/lamp-town-guy/306694cdb510bd93894c51bb1aaf917e
18 replies
AEAsh Elixir
Created by lamp-town-guy on 2/8/2023 in #support
module AshPostgres.DataLayer.CustomIndexes is not loaded and could not be found
I'm trying Ash Postgres tutorial and I've encountered this error. Maybe it's my mistake because I already have Phoenix in my project and I'm not starting from scratch. But I've never seen this error before even when I've tried to migrate existing Elixir app. I've got this error when I've tired to create migrations and run them. So I should have all my Postgres/Ash setup ready to roll. Base Ash without Postgres worked as expected.
== Compilation error in file lib/app/support/resources/representative.ex ==
** (CompileError) lib/app/support/resources/representative.ex:4: module AshPostgres.DataLayer.CustomIndexes is not loaded and could not be found. This may be happening because the module you are trying to load directly or indirectly depends on the current module
(ash_postgres 1.3.8) expanding macro: AshPostgres.DataLayer.postgres/1
lib/app/support/resources/representative.ex:4: App.Support.Representative (module)
== Compilation error in file lib/app/support/resources/representative.ex ==
** (CompileError) lib/app/support/resources/representative.ex:4: module AshPostgres.DataLayer.CustomIndexes is not loaded and could not be found. This may be happening because the module you are trying to load directly or indirectly depends on the current module
(ash_postgres 1.3.8) expanding macro: AshPostgres.DataLayer.postgres/1
lib/app/support/resources/representative.ex:4: App.Support.Representative (module)
My other deviation from from tutorial is not calling the app Helpdesk but App. I don't think this should cause it but in case I've caused some weirdness I mention it. It's 1AM for me and I'm going to bed so I won't respond for next couple of hours.
5 replies