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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement