Phoenix 입문 #02

5654 단어 MySQLElixirPhoenix
Phoenix 입문 #01 계속

Elixir를 전혀 만진 적 없는 내가 Phoenix 입문한 로그

Phoenix 입문의 파트 2입니다.
이번에는 CRUD를 만듭니다.

이전 준비



모델 주위에는 ecto라는 라이브러리가 사용되고 있다.ecto 커맨드를 이용하면 DB의 작성 등을 할 수 있다.
이번 이용하는 DB, MySQL을 인스톨 해 둔다.
$ apt-get install mysql-server

설정



{app_name}/config 아래에 각 환경별 설정이 있다.
이번에는 dev.exstest.exs의 비밀번호만 변경했다.

config/dev.exs
# Configure your database
config :phoenix_blog, PhoenixBlog.Repo,
  adapter: Ecto.Adapters.MySQL,
  username: "root",
  password: "password", # <-- ここ!!!
  database: "phoenix_blog_dev",
  size: 10 # The amount of database connections in the pool

다음으로 ecto 명령으로 DB를 작성한다.
만약 명령이 없는 것 같은 에러가 나왔다면, 생성한 phoenix-app의 디렉토리 바로 아래로 이동하면 된다.
$ pwd
/vagrant/phoenix_blog
$ mix ecto.create
The database for PhoenixBlog.Repo has been created.

Scaffold 같은 남자



phoenix에는 몇 가지 generator가 준비되어 있다.
mix phoenix.gen.channel # Generates a Phoenix channel
mix phoenix.gen.html    # Generates controller, model and views for an HTML-based resource
mix phoenix.gen.json    # Generates a controller and model for an JSON-based resource
mix phoenix.gen.model   # Generates an Ecto model
phoenix.gen.html 가 Rails에서 말하는 scaffold 같다.

인수는 모델 이름 테이블 이름 열을 지정하는 것 같습니다.
$ mix phoenix.gen.html Post posts title:string body:text

* creating priv/repo/migrations/20150607191442_create_post.exs
* creating web/models/post.ex
* creating test/models/post_test.exs
...

Add the resource to the proper scope in web/router.ex:

    resources "/posts", PostController

and then update your repository by running migrations:

    $ mix ecto.migrate
web/router.ex 로 라우팅 설정.

web/router.ex
  scope "/", PhoenixBlog do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    resources '/posts', PostController
  end
ecto 에서 migrate 한다.
$ mix ecto.migrate
Compiled web/models/post.ex
Generated phoenix_blog app
[info] == Running PhoenixBlog.Repo.Migrations.CreatePost.change/0 forward
[info] create table posts
[info] == Migrated in 0.0s

하나만 주의할 때 싶다고 생각한 것이 타임스탬프.
Rails에서는 생성되는 필드가 created_at , updated_at 이었지만,
Phoenix에서는 inserted_at , updated_at 이었다.
mysql> desc posts;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255)        | YES  |     | NULL    |                |
| body        | text                | YES  |     | NULL    |                |
| inserted_at | datetime            | NO   |     | NULL    |                |
| updated_at  | datetime            | NO   |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Rails에서 말하는 views는 web/templates 로 여기의 파일을 참조시키려면 web/views
지금까지 기본적인 CRUD를 구현할 수 있었다.



Phoenix의 특징 같은 곳은 전혀 접하지 않고 코드도 전혀 쓰지 않으므로 앞으로 좀 더 공부해 보려고 생각합니다.

좋은 웹페이지 즐겨찾기