zachdaniel

zachdaniel

Creator of Ash

Hey folks! We’ve just released the beta 0.1.0 version of ash_sqlite. Take a look at the guide here: https://hexdocs.pm/ash_sqlite/get-started-with-sqlite.html

Keep in mind, this is an initial beta release! I’ve tried it out, it’s been smooth for me, but there will almost certainly be some issues that need to be addressed.

Happy hacking!

Big thanks to @warmwaffles for getting some changes in and cutting a release so that we could launch!

Showing Posts 1 to 5

warmwaffles

warmwaffles

If you encounter and issues, feel free to open tickets.

mayl

mayl

Exciting!

As far as the Ash ecosystem goes, were there any features of Ash Postgres which Ash Sqlite wasn’t able to support? Or are they full one to one replacements for each other?

zachdaniel

zachdaniel OP

Creator of Ash

There are various features we could not support. The big ones are aggregates and certain expressions do not have a parallel (or one that we cared to build in the first release) in SQLite3. For example, concat_ws([list, of, things]) is not supported.

Aggregates are a pretty big feature and not being able to use them may or may not affect a given use case. You can always use calculations to extract similar data, but it likely won’t be usable in filters and sorts unless you write it as a fragment that does a subquery.

We will most likely be able to support all aggregate types except for list at some point, but we couldn’t use the implementation from ash_postgres because it uses lateral joins which are not available in SQLite3. Something new will need to be devised for AshSqlite.

Another issue w/ the current implementation is that the migration generator currently is essentially fully copied from AshPostgres but AshPostgres uses alter column and that is not supported by SQLite3, meaning that those migrations need to be adjusted by hand. We should fix the migration generator to have it do something like warn to the user and drop some comments in the migration file, or something like that. Still TBD.

While I’m using AshSqlite3 In https://ash-hq.org I will likely not need any advanced feature support at any point in the near future, so hopefully those that do use it can champion pushing it forward in the above regards.

Neophen

Neophen

Hey people I’m new to ash. any ideas how i could do this would be really nice.
I have a table like this.
Image

I want to create a read action something like:

Posts.read_translated(:some_locale)

This action would return the title/content as string from the json column, extra credit if it could default to some defined locale if the current locale is empty. here’s the resource code:

defmodule Octafest.Blog.Post do
  use Ash.Resource,
    data_layer: AshSqlite.DataLayer

  sqlite do
    table "posts"

    repo Octafest.Repo
  end

  code_interface do
    define_for Octafest.Blog
    define :create, action: :create
    define :read_all, action: :read
    define :update, action: :update
    define :destroy, action: :destroy
    define :get_by_id, args: [:id], action: :by_id
  end

  actions do
    defaults ~w(create read update destroy)a

    read :by_id do
      argument :id, :uuid, allow_nil?: false
      get? true

      filter expr(id == ^arg(:id))
    end

    read :translated do
      argument :locale, :atom do
        allow_nil? false
      end

      pagination offset?: true, countable: :by_default
    end
  end

  attributes do
    uuid_primary_key :id

    attribute :title, Octafest.Shared.Translated
    attribute :content, Octafest.Shared.Translated
  end
end
Neophen

Neophen

Here’s the fix:

defmodule Octafest.Api.Post do
  use Ash.Resource,
    data_layer: AshSqlite.DataLayer

  sqlite do
    table "posts"

    repo Octafest.Repo
  end

  attributes do
    uuid_primary_key :id

    attribute :title, Octafest.Shared.Translated
    attribute :content, Octafest.Shared.Translated
  end

  actions do
    defaults ~w(create read update destroy)a

    read :by_id do
      argument :id, :uuid, allow_nil?: false
      get? true

      filter expr(id == ^arg(:id))
    end

    read :translated do
      argument :locale, :atom, allow_nil?: false

      prepare build(
                load: [
                  {:t_title, locale: arg(:locale)},
                  {:t_content, locale: arg(:locale)}
                ]
              )
    end
  end

  calculations do
    calculate :t_title, :string, {TranslateAttribute, attribute: :title} do
      argument :locale, :atom do
        default :en
      end
    end

    calculate :t_content, :string, {TranslateAttribute, attribute: :content} do
      argument :locale, :atom do
        default :en
      end
    end
  end

  code_interface do
    define_for Octafest.Api
    define :create, action: :create
    define :read_all, action: :read
    define :update, action: :update
    define :destroy, action: :destroy

    define :get_by_id, args: [:id], action: :by_id
    define :translated, args: [:locale], action: :translated
  end
end

defmodule TranslateAttribute do
  use Ash.Calculation

  @impl true
  def select(_, opts, _), do: [opts[:attribute]]

  @impl true
  @spec calculate(any(), any(), %{:locale => atom(), optional(any()) => any()}) :: list()
  def calculate(records, opts, %{locale: locale}) do
    records
    |> Enum.map(fn record ->
      record |> Map.get(opts[:attribute]) |> Map.get(locale)
    end)
  end
end

And this is how you call it:

defmodule OctafestWeb.AdminLive do
  use OctafestWeb, :admin_live_view
  alias Octafest.Api.Post

  def mount(_params, _session, socket) do
    posts = Post.translated!(:en)

    locales = ~w(en lt)a

    create_form = get_post_create_form()

    socket =
      assign(socket,
        posts: posts,
        locales: locales,
        locale: :en
      )

    {:ok, socket}
  end
end

But i’ve decided against using this, as this is quite a bit of code and while i could most likely add an extension it feels somewhat magical.

— All posts loaded —

Where Next? Top

Trending in News & Updates Top

kip
I’m a bit excited to announce that Localize and friends are now at release 1.0. Even though it’s a 1.0 release, it stands on 8 years of w...
New
bartblast
After building Hologram and sharing updates across various places, I’ve realized there’s a lot happening that doesn’t always make it to t...
New
bartblast
Hologram v0.11 is out! Two headline features this release. First, Elixir regexes now run in the browser. They were server-only until now,...
New
nseaSeb
Just published search_ash 0.5.0 (with search_core 0.4.0) on Hex. What’s new: synonyms You can now declare a synonym dictionary per lang...
New
mudasobwa
After years of struggling I made StreamData dependency optional. Finitomata.ExUnit got testing primitives for Persistence. Full back co...
New
mudasobwa
MdexMultilineCells is an MDEx plugin enabling multi-line cells in Markdown tables with full inline/block Markdown rendering and automati...
New
sullyMusty
ActiveMemory 0.8.0 — in-memory tables that speak Ecto ActiveMemory 0.8.0 is now on Hex. ActiveMemory is an in-memory store built on ETS ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews