This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [[ $# -eq 0 ]] ; then | |
echo 'missing directory' | |
exit 1 | |
fi | |
dir="$1" | |
for filename in $dir/*.wav; do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BlogExample.Schema.Category do | |
use Ecto.Schema | |
@primary_key {:name, :string, []} | |
embedded_schema do | |
field(:current_popularity, :integer) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query = | |
from(p in PostWithVirtualCategory, | |
where: p.id in ^post_ids | |
) | |
posts = | |
query | |
|> Repo.all() | |
|> Enum.map(&PostWithVirtualCategory.populate_category/1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder