spicychickensauce

spicychickensauce

Support both sqlite and postgres in ash application

I’m evaluating migrating an existing application to ash and it’s looking pretty good so far.

However, I got stuck at this: My app currently supports both sqlite and postgres, configured via a compile time setting. (I need sqlite support because my app is also bundled on android, postgres is used on servers)

I cannot figure out how to make that work with ash.
The problem is in the resources. I tried this:

  if App.Config.use_sqlite?() do
    use Ash.Resource,
      domain: App,
      data_layer: AshSqlite.DataLayer
  else
    use Ash.Resource,
      domain: App,
      data_layer: AshPostgres.DataLayer
  end

  # same conditional for the `postgres` and `sqlite` expressions

  actions do
  ...

However, I get a compile error:

    error: undefined function actions/1 (there is no such import)
    │
 12 │   actions do

This is very weird, since when I manually comment out the appropriate use it compiles just fine.

What is happening here? Why does this conditional compilation logic not work with ash?

It used to work just fine when I did that in my ecto repo to configure the appropriate adapter…

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

:thinking: try this instead:

use mix ash.gen.base_resource MyApp.Resource and then modify it to look something like this:

  # use it in the resources like this
  use MyApp.Resource

  defmodule MyApp.Resource do
    defmacro __using__(opts) do
       data_layer = 
         if App.Config.use_sqlite?() do
           AshSqlite.DataLayer
         else
           AshPostgres.DataLayer
         end

       opts = Keyword.put_new(opts, :data_layer, data_layer)

        quote do
          use Ash.Resource, unquote(opts)
        end
      end
    end

Also Liked

zachdaniel

zachdaniel

Creator of Ash

Simpler would be this:

 defmodule MyApp.Resource do
    defmacro __using__(opts) do
       {data_layer, extension_to_add} = 
         if App.Config.use_sqlite?() do
           {AshSqlite.DataLayer, AshPostgres.DataLayer}
         else
           {AshPostgres.DataLayer, AshSqlite.DataLayer}
         end

      {table, opts} = Keyword.pop(opts, :table)

       opts = 
         opts
         |> Keyword.put_new(:data_layer, data_layer)
         |> Keyword.update(:extensions, [extension_to_add], &[extension_to_add | &1])

        quote do
          use Ash.Resource, unquote(opts)
          
           postgres do
              table unquote(table)
              repo YourRepo
           end

           sqlite do
             table unquote(table)
             repo YourRepo
           end
        end
      end
    end

You can use the postgres and sqlite extensions multiple times, and can add it as an extension without making it the data layer so that when you have special sqlite or postgres specific things in a resource, you can just do postgres do ... and not include table.

Last Post!

spicychickensauce

spicychickensauce

Great, then I’ll go with that. Seems the most flexible and DRY approach.
Thank you very much for the help!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement