cloedu87

cloedu87

Hi there,
I’m working with ash now a couple of weeks and I’m quite a big fan :slight_smile: but now I’m struggling with a - so it seems - basic usecase.

I’m using the ash framework to manage my resources in the database (postgres).

I would like to store structs which are from a foreign library, namely instances of Ash.Query, to the datasbase. like:

attributes do
    attribute :query, Ash.Query
    ...
end

Of course using Ash.Query directly as type is not possible. My first intention was to solve this with a custom Ash.Type and use…

@impl true
  def storage_type, do: :map

…to tell the data layer I would like to store it as a :map on the data layer. but this did not work.

My question is: how can I define attributes on my ash resources, which are able to store structs of foreign libraries (structs which are NOT part of my code/project) and if it is only possible over custom Ash.Type’s, how can I serialize those structs properly, so I can write and read them like regular attributes?

Thanks for any hints :slight_smile:
Claudio

Showing Posts 1 to 6

zachdaniel

zachdaniel

Creator of Ash

Well, I would very much advise against storing that kind of data in your database to be honest. But, if you must, use the :term type, which uses binary_to_term and term_to_binary to encode/decode to binary storage.

Why do you want to store an Ash.Query in a database?

cloedu87

cloedu87 OP

Thx for the reply!

I got a data export feature. for each export (which runs async as oban jobs) I have to define specific filter criteria, so I can select data dynamically according to various situations.
to run, re-run and schedule those exports for later execution I have to - somehow - persist those filters/query criterias.

such a query/criteria basically looks like:

MyRecord
    |> Ash.Query.load(somestuff: [:id])
    |> Ash.Query.filter(
      somestuff.id == ^id and
        not is_nil(an_attribute) and
...

I honestly couldn’t find another solution to properly serialize those filters to re-use them later…

Best, C.

zachdaniel

zachdaniel

Creator of Ash

I would suggest using the map syntax that Ash filters support. For example:

Ash.Query.filter(%{id: %{eq: ^id}})

Then you can persist the map :slight_smile:

cloedu87

cloedu87 OP

This would be awesome… :slight_smile: but how is it with relations?

like I do with

Ash.Query.load(somestuff: [:id])

I have to do queries over my related resources, can I dynamically load relations, according to a query?

zachdaniel

zachdaniel

Creator of Ash

Loading related data and referencing related data in filters is actually unrelated. You don’t need to load something to reference it. In the case of the map syntax, it would be %{some_stuff: %{id: %{eq: id}}}

cloedu87

cloedu87 OP

yes, right.. will do it this way, thank you very much :slight_smile:

and thx for the awesome framework. it’s simply great :+1:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews