Skip to content

Instantly share code, notes, and snippets.

View nietaki's full-sized avatar
😌
purposeful

Jacek Królikowski nietaki

😌
purposeful
View GitHub Profile
@nietaki
nietaki / ecto_iex_helpers.ex
Created September 18, 2023 18:30 — forked from pyzlnar/ecto_iex_helpers.ex
A few Ecto helpers that you might want to add to your .iex.exs file. Were created with PSQL in mind
import_if_available(Ecto.Query)
# alias MyApp.Repo
defmodule R do
# Old habits die hard. Take a random of something. You don't care which.
# > R.take(SomeSchema)
def take(schema_or_query) do
schema_or_query
|> limit(1)
|> Repo.one()
$ lspci -nn
00:00.0 Host bridge [0600]: Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor Host Bridge/DRAM Registers [8086:191f] (rev 07)
00:01.0 PCI bridge [0604]: Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor PCIe Controller (x16) [8086:1901] (rev 07)
00:14.0 USB controller [0c03]: Intel Corporation 100 Series/C230 Series Chipset Family USB 3.0 xHCI Controller [8086:a12f] (rev 31)
00:16.0 Communication controller [0780]: Intel Corporation 100 Series/C230 Series Chipset Family MEI Controller #1 [8086:a13a] (rev 31)
00:17.0 SATA controller [0106]: Intel Corporation Q170/Q150/B150/H170/H110/Z170/CM236 Chipset SATA Controller [AHCI Mode] [8086:a102] (rev 31)
00:1b.0 PCI bridge [0604]: Intel Corporation 100 Series/C230 Series Chipset Family PCI Express Root Port #17 [8086:a167] (rev f1)
00:1b.2 PCI bridge [0604]: Intel Corporation 100 Series/C230 Series Chipset Family PCI Express Root Port #19 [8086:a169] (rev f1)
00:1c.0 PCI bridge [0604]: Intel Corporation 100 Series/C23
#!/usr/bin/env bash
if [[ $# -eq 0 ]] ; then
echo 'missing directory'
exit 1
fi
dir="$1"
for filename in $dir/*.wav; do
def category_preloader(category_names) when is_list(category_names) do
# category_names are already deduplicated
Enum.map(category_names, &RemoteService.retrieve_category_info/1)
end
query =
from(p in Post,
where: p.id in ^post_ids,
preload: [category: ^&category_preloader/1]
)
defmodule BlogExample.Schema.Category do
use Ecto.Schema
@primary_key {:name, :string, []}
embedded_schema do
field(:current_popularity, :integer)
end
end
query =
from(p in PostWithVirtualCategory,
where: p.id in ^post_ids
)
posts =
query
|> Repo.all()
|> Enum.map(&PostWithVirtualCategory.populate_category/1)
defmodule BlogExample.Schema.PostWithVirtualCategory do
use Ecto.Schema
schema "posts" do
field(:title, :string, null: false)
field(:contents, :string, null: false)
field(:category_name, :string)
field(:category, :map, virtual: true)
simpler_query = from(c in Comment, where: c.user_id == ^charlie_id)
non_preloaded = Repo.all(simpler_query)
assert Enum.all?(non_preloaded, fn c -> c.post == nil end)
comments_by_charlie = Repo.preload(non_preloaded, [:post])
refute Enum.any?(comments_by_charlie, fn c -> c.post == nil end)
# aliases and imports omitted
query =
from(c in Comment,
where: c.user_id == ^charlie_id,
preload: [:post]
)
comments_by_charlie = Repo.all(query)
refute Enum.any?(comments_by_charlie, fn c -> c.post == nil end)
alias BlogExample.Schema.Comment
import Ecto.Query, only: [from: 2]
query =
from(c in Comment,
where: c.user_id == ^charlie_id
)
comments_by_charlie = Repo.all(query)