Chapter 05

PhoenixのコンテキストなどにIExでアクセスする

toyoshi
toyoshi
2021.12.30に更新

Phoenixの環境を持ったIExを起動することでコンテキストにアクセスできます。

$iex -S mix phx.server

#一覧表示
iex(9)> Blog.Articles.list_articles                                                    
[debug] QUERY OK source="articles" db=0.3ms queue=0.1ms idle=1743.6ms
SELECT a0."id", a0."body", a0."title", a0."inserted_at", a0."updated_at" FROM "articles" AS a0 []
[
  %Blog.Articles.Article{
    __meta__: #Ecto.Schema.Metadata<:loaded, "articles">,
    body: "Hello everyone",
    id: 1,
    inserted_at: ~N[2021-12-30 12:18:05],
    title: "First article",
    updated_at: ~N[2021-12-30 12:18:05]
  }
]

#レコードの作成
iex(8)> Blog.Articles.create_article(%{title: "First article", body: "Hello everyone"}) 
[debug] QUERY OK db=0.3ms idle=448.1ms
INSERT INTO "articles" ("body","title","inserted_at","updated_at") VALUES (?,?,?,?) RETURNING "id" ["Hello everyone", "First article", "2021-12-30T12:18:05", "2021-12-30T12:18:05"]
{:ok,
 %Blog.Articles.Article{
   __meta__: #Ecto.Schema.Metadata<:loaded, "articles">,
   body: "Hello everyone",
   id: 1,
   inserted_at: ~N[2021-12-30 12:18:05],
   title: "First article",
   updated_at: ~N[2021-12-30 12:18:05]
 }}