tanweerdev

tanweerdev

Fat_ecto - dynamically build queries

fat_ecto provides methods for dynamically building queries according to the parameters it receives.
Currently it supports the following query functions :

  • where
  • select
  • joins
  • order_by
  • include

Example

    query_opts = %{
    "$select" => %{
      "$fields" => ["name", "location", "rating"],
      "fat_rooms" => ["beds", "capacity"]
    },
    "$order" => %{"id" => "$desc"},
    "$where" => %{"rating" => 4},
    "$group" => "nurses",
    "$include" => %{
      "fat_doctors" => %{
        "$include" => ["fat_patients"],
        "$where" => %{"name" => "ham"},
        "$order" => %{"id" => "$desc"},
        "$join" => "$right"
      }
    },
    "$right_join" => %{
      "fat_rooms" => %{
        "$on_field" => "id",
        "$on_join_table_field" => "hospital_id",
        "$select" => ["beds", "capacity", "level"],
        "$where" => %{"incharge" => "John"}
      }
    }

and the output will be

    iex > FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
    #Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms",
    on: f0.id == f1.hospital_id, right_join: f2 in assoc(f0, :fat_doctors),
    where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true,
    group_by: [f0.nurses], order_by: [desc: f0.id],
    select: merge(map(f0, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}]), %{^:fat_rooms => map(f1, [:beds, :capacity, :level])}),
    preload: [fat_doctors: #Ecto.Query<from f0 in FatEcto.FatDoctor, left_join: f1 in assoc(f0, :fat_patients), where: f0.name == ^"ham" and ^true, order_by: [desc: f0.id], limit: ^10, offset: ^0, preload: [:fat_patients]>]>

Most Liked

michalmuskala

michalmuskala

This looks like a great library, but with such a powerful interface, I think it should be used extremely carefully. It’s a very easy way to expose serious security vulnerabilities in your application by exposing to the client data that shouldn’t be exposed - a naive example could be a password_hash field on some User or Account schema that could be requested either directly or going through some association. I believe there should be at least a whitelist defining what fields the function is allowed to act upon, exposing just anything seems rather reckless to me.

OvermindDL1

OvermindDL1

GraphQL is good for that. Taking, say, Absinthe’s GraphQL parser and parsing out a set of ecto commands would be quite useful and much safer using it’s schema definitions.

tanweerdev

tanweerdev

totally agree with you. currently its being used in admin panels. What do you propose if I try to support this. what’s the right path/approach for me?

tanweerdev

tanweerdev

Whitelisting mechanism I believe is little complex because this library not only supports include but also custom joins with custom names. I would love to hear your thoughts

Where Next?

Popular in Libraries Top

scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
cjen07
parameterized pipe in elixir: |n&gt; edit: negative index in |n&gt; and mixed usage with |&gt; are supported example: use ParamP...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
mcrumm
If you would like to migrate away from node/npm/webpack while still using sass, the dart_sass package provides a installer and runner for...
New
michalmuskala
Another small library today. PersistentEts Hex: persistent_ets | Hex GitHub: GitHub - michalmuskala/persistent_ets Ets table backed by...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New

Other popular topics Top

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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement