tanweerdev

tanweerdev

Prepare_query wont pass updated options to preloaded sub sequent calls from main query

I am trying to implement soft delete using this library ecto_soft_delete/lib/ecto/soft_delete_repo.ex at master · revelrylabs/ecto_soft_delete · GitHub 's module This has prepare_query method implemented which will not fetch all soft deleted records which is fine. But the problem is, it will also not fetch preloaded records if they are soft deleted. eg if I am a user with salary of type monthly. if monthly salary type got soft deleted. it will not show/fetch any salary type at all… Main query shouldn’t fetch soft deleted records which is fine but all preloaded records despite of soft deleted or not should be fetched…
so I tried to modify this query a bit

def prepare_query(_operation, query, opts) do
        schema_module = get_schema_module_from_query(query)
        fields = if schema_module, do: schema_module.__schema__(:fields), else: []
        soft_deactivateable? = Enum.member?(fields, :deactivated_at)

        if has_include_deactivated_at_clause?(query) || opts[:with_deactivated] ||
             !soft_deactivateable? do
          {query, opts}
        else
          query = from(x in query, where: is_nil(x.deactivated_at))
          IO.inspect opts
          # I am trying put with_deactivated for all sub-sequent calls but sub-sequent calls dont receive this option
          opts = Keyword.put(opts, :with_deactivated, false)
          {query, opts}
        end
      end

prepare_query will get called two times. one for the main user query and one for salary_type… but second query(which is for preload) should always fetch record means with with_deactivated: false but i am unable to differentiate which query is the main one and which query is for preload in prepare_query

Most Liked

al2o3cr

al2o3cr

That project hasn’t seem much change lately, and there are 2-year-old somewhat-contradictory issues that seem related:

https://github.com/revelrylabs/ecto_soft_delete/issues/98

https://github.com/revelrylabs/ecto_soft_delete/issues/100

What you’re looking for sounds like it would be better achieved by explicitly filtering on deleted_at in the main query and preloading normally otherwise; approaches like prepare_query are hard to opt-out of.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement