概要
__Elixir
の__Databaseラッパー__であり__Queryジェネレータ__であるEcto
でMySQL
__に接続し、データ作成・読み出し・更新・削除(= CRUD
)操作を行っていきます。
-
mix new
でプロジェクト作成し、IEx
で動作確認をします。 - Ectoアダプタには
MyXQL
(Hex)を使用します。
※ 記事執筆時点2020年5月24日における最新バージョン0.4.0
複数回に分けて書きます。
本記事では、Ectoを介したデータベース作成までを行います。
実行環境
バージョン | 備考 | |
---|---|---|
macOS | 10.14.6 | |
Elixir | 1.9.2 | Erlang/OTP 22 |
MySQL | 5.7.29 | ローカル開発環境(localhost / rootユーザー / passwordなし) |
前提
- Elixirインストール済み
- MySQLインストール済み&ローカルDBサーバー起動済み
参考
-
Ecto (Hexdocs)
- こちらのGetting Startedドキュメントをお題としつつ進めていきます。
- ドキュメント本家では
PostgreSQL
使用のところを、当記事ではMySQL
バージョンにアレンジして実装します。 - ディレクトリ名やレコード、手順等も一部変更しています。
- MyXQL (Hexdocs)
- Elixir School (ECTO)
プロジェクト作成とEctoセットアップ
プロジェクト作成
プロジェクト名をfriendsmysql
として、セットアップしていきます。
terminal
$ mix new friendsmysql --sup
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/friendsmysql.ex
* creating lib/friendsmysql/application.ex
* creating test
* creating test/test_helper.exs
* creating test/friendsmysql_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd friendsmysql
mix test
Run "mix help" for more commands.
terminal
$ cd friendsmysql
依存パッケージを追加
mix.exs
defp deps do
[
{:ecto_sql, "~> 3.4"}, -> add
{:myxql, "~> 0.4.0"} -> add
terminal
$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
New:
connection 1.0.4
db_connection 2.2.2
decimal 1.8.1
ecto 3.4.4
ecto_sql 3.4.4
myxql 0.4.0
telemetry 0.4.1
* Getting ecto_sql (Hex package)
* Getting myxql (Hex package)
* Getting db_connection (Hex package)
* Getting decimal (Hex package)
* Getting connection (Hex package)
* Getting ecto (Hex package)
* Getting telemetry (Hex package)
Ectoのリポジトリを作成
データベースの接続と通信を行うために、リポジトリをセットアップします。
terminal
$ mix ecto.gen.repo -r Friendsmysql.Repo
==> connection
Compiling 1 file (.ex)
Generated connection app
===> Compiling telemetry
==> decimal
Compiling 1 file (.ex)
Generated decimal app
==> db_connection
Compiling 14 files (.ex)
Generated db_connection app
==> ecto
Compiling 55 files (.ex)
Generated ecto app
==> myxql
Compiling 15 files (.ex)
Generated myxql app
==> ecto_sql
Compiling 26 files (.ex)
Generated ecto_sql app
==> friendsmysql
* creating lib/friendsmysql
* creating lib/friendsmysql/repo.ex
* creating config/config.exs
Don't forget to add your new repo to your supervision tree
(typically in lib/friendsmysql/application.ex):
{Friendsmysql.Repo, []}
And to add it to the list of ecto repositories in your
configuration files (so Ecto tasks work as expected):
config :friendsmysql,
ecto_repos: [Friendsmysql.Repo]
生成されたファイルのコードを、以下の通りアップデート&追記していきます。
config/config.exs
config :friendsmysql, Friendsmysql.Repo,
adapter: Ecto.Adapters.MyXQL, --> add
database: "friendsmysql_repo",
username: "root", --> update
password: "", --> update
hostname: "localhost" --> update
config :friendsmysql, --> add
ecto_repos: [Friendsmysql.Repo] --> add
lib/friends/application.ex
def start(_type, _args) do
children = [
Friendsmysql.Repo, --> add
lib/friendsmysql/repo.ex
defmodule Friendsmysql.Repo do
use Ecto.Repo,
otp_app: :friendsmysql,
adapter: Ecto.Adapters.MyXQL --> add
end
MySQLのデータベース作成
terminal
$ mix ecto.create
Compiling 3 files (.ex)
Generated friendsmysql app
The database for Friendsmysql.Repo has been created
ここまでで、データベースが作成されているかMySQL側で確認してみます。
terminal(MySQL)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| . |
| . |
| friendsmysql_repo |
| . |
| . |
データベースfriendmysql_repo
が作成できました!
終わり & 次回
ElixirでMySQLに接続し、Ectoを介してデータベースを作成しました。
次回は、マイグレーション〜スキーマ作成の実装から行っていきます。