Elixir Ecto: 模型的嵌入(Embed)

 

Postgres 9.4 及其以上版本可以存儲類似 arrays , json , jsonb 這樣的非結構化數據. Ecto 作為一個 Elixir 的數據封裝器, 提供了這些非結構話的數據到 Elixir 原生數據類型的序列化和反序列化. 嵌入的記錄具有所有常規模型所具有的東西, 比如結構化的字段, 生命周期回調, 變更集. 下面研究如何把結構嵌入到 Ecto 模型當中.

使用 embeds_one 嵌入單個結構

可以使用 embeds_one 嵌入單個結構到 Ecto 模型當中. 嵌入的字段必須是 :map 這種非結構化的的數據類型. 在 Postgres 中, Ecto 使用了 jsonb 作為底層數據庫字段的數據類型.

20160430043543_create_table_account.exs

defmodule EctoTest.Repo.Migrations.CreateTableAccount do
  use Ecto.Migration
  def change do
    create table(:accounts) do
      add :name, :string
      add :settings, :map  # 遷移腳本里面數據類型需要設置為 :map,
                           # 這樣 Postgrex 適配器才能正確的把它映射到 jsonb[] 數據類型
    end
  end
end

執行項目目錄中的 ./migrate 腳本, 內容如下

#!/bin/bash

mix ecto.migrate -r EctoTest.Repo
?  ecto_test git:(master) ? ./migrate

12:38:10.930 [info]  == Running EctoTest.Repo.Migrations.CreateTableAccount.change/0 forward

12:38:10.930 [info]  create table accounts

12:38:10.943 [info]  == Migrated in 0.0s

定義模型

defmodule EctoTest.Model.Account do
  use Ecto.Schema
  alias EctoTest.Model.Settings
  schema "accounts" do
    field :name
    embeds_one :settings, Settings
  end
end

定義嵌入到 EctoTest.Model.Account 模型中的字段:

defmodule EctoTest.Model.Settings do
  use Ecto.Model
  embedded_schema do
    field :email_signature
    field :send_emails, :boolean
  end
end

嵌入的記錄行為上和電信的關聯( associations )一樣, 只是它只能夠通過變更集( changeset )來更新和刪除

插入數據測試

# 模擬 EctoTest.get/1 返回一個 %EctoTest.Model.Account() 結構
account = %EctoTest.Model.Account{name: "test"}

# 創建Settings結構
settings = %EctoTest.Model.Settings{email_signature: "developerworks", send_emails: true}

# 創建變更集
changeset = Ecto.Changeset.change(account)

# 嵌入
changeset = Ecto.Changeset.put_embed(changeset, :settings, settings)

# 插入數據
changeset |> EctoTest.Repo.insert!

當父記錄保存時, 會自動地的調用嵌入模型( Settings 模型)中的 changeset/2 函數.

嵌入的記錄可以通過 . 符號來訪問, 因此不必考慮在一般的關聯關系中的鏈接 JOIN 和預加載( preload )的問題:

account = Repo.get!(EctoTest.Model.Account, 1)
account.settings #=> %Settings{...}

使用 embeds_many 嵌入多個結構

embeds_many 允許你嵌入一個 Ecto 結構數組到一個關系數據庫字段中. 在數據庫底層 Postgres 使用了 array 和 jsonb 數據類型來實現 embeds_many .

defmodule EctoTest.Repo.Migrations.CreateTablePeople do
  use Ecto.Migration

  def up do
    create table(:people) do
      add :name, :string
      add :address, {:array, :map}, default: []
    end
  end

  def down do
    drop table(:people)
  end
end

定義 Person , Address 模型:

defmodule EctoTest.Model.Person do
  use Ecto.Schema
  alias EctoTest.Model.Address

  schema "people" do
    field :name
    embeds_many :addresses, Address
  end

  def test_insert do
    changeset = Ecto.Changeset.change(%__MODULE__{})
    addresses = [
      %Address{street_name: "20 Foobar Street",city: "Boston",state: "MA",zip_code: "02111"},
      %Address{street_name: "1 Finite Loop", city: "Cupertino",state: "CA",zip_code: "95014"}
    ]
    changeset = Ecto.Changeset.put_embed(changeset, :addresses, addresses)
    Repo.insert!(changeset)
  end
end


defmodule EctoTest.Model.Address do
  use Ecto.Schema

  embedded_schema do
    field :street_name
    field :city
    field :state
    field :zip_code
  end
end

設置多對多字段的值

iex(1)> EctoTest.Model.Person.test_insert

13:44:25.731 [debug] QUERY OK db=9.4ms
INSERT INTO "people" ("addresses") VALUES ($1) RETURNING "id" [[%{city: "Boston", id: "2de7fd44-a1cf-44cd-a060-d6260325ac90", state: "MA", street_name: "20 Foobar Street", zip_code: "02111"}, %{city: "Cupertino", id: "bb65fd96-f1b1-4f0d-a92e-712884e40a7c", state: "CA", street_name: "1 Finite Loop", zip_code: "95014"}]]
%EctoTest.Model.Person{__meta__: #Ecto.Schema.Metadata<:loaded>,
 addresses: [%EctoTest.Model.Address{city: "Boston", id: "2de7fd44-a1cf-44cd-a060-d6260325ac90", state: "MA",
   street_name: "20 Foobar Street", zip_code: "02111"},
  %EctoTest.Model.Address{city: "Cupertino", id: "bb65fd96-f1b1-4f0d-a92e-712884e40a7c", state: "CA",
   street_name: "1 Finite Loop", zip_code: "95014"}], id: 1, name: nil}

像 has_many 一樣訪問:

iex(2)> person = EctoTest.Repo.get!(EctoTest.Model.Person, 1)

13:45:04.634 [debug] QUERY OK db=1.4ms decode=9.5ms
SELECT p0."id", p0."name", p0."addresses" FROM "people" AS p0 WHERE (p0."id" = $1) [1]
%EctoTest.Model.Person{__meta__: #Ecto.Schema.Metadata<:loaded>,
 addresses: [%EctoTest.Model.Address{city: "Boston", id: "2de7fd44-a1cf-44cd-a060-d6260325ac90", state: "MA",
   street_name: "20 Foobar Street", zip_code: "02111"},
  %EctoTest.Model.Address{city: "Cupertino", id: "bb65fd96-f1b1-4f0d-a92e-712884e40a7c", state: "CA",
   street_name: "1 Finite Loop", zip_code: "95014"}], id: 1, name: nil}

iex(3)> person.addresses
[%EctoTest.Model.Address{city: "Boston", id: "2de7fd44-a1cf-44cd-a060-d6260325ac90", state: "MA",
  street_name: "20 Foobar Street", zip_code: "02111"},
 %EctoTest.Model.Address{city: "Cupertino", id: "bb65fd96-f1b1-4f0d-a92e-712884e40a7c", state: "CA",
  street_name: "1 Finite Loop", zip_code: "95014"}]

權衡

記錄的嵌入是 Ecto 提供的多個強大的能之一. 很容易給嵌入的記錄添加字段, 不需要運行時修改數據庫結構. 這些都是優勢, 但有的場景下也會帶來一些問題, 因此需要權衡是否應該使用嵌入.

使用非結構化數據, 丟失了SQL數據庫提供的關系特性, 例如, 一個記錄只能嵌入到一個父表中, 因此不能使用嵌入建模多對多關系, 也不能使用數據庫約束, 雖然可以在應用程序中檢查數據的完整性, 但是更好的方式是在數據庫級別進行驗證, 以保障數據的完整性.

示例代碼

https://github.com/developerworks/ecto_test

 

來自: https://segmentfault.com/a/1190000005037566

 本文由用戶 wlhp6428 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!