Alibaba Tablestore adapter for Ecto

Module VersionHex DocsTotal DownloadLicenseLast Updated

Notice

If you have used this library and upgrade it into 0.11.0 or later, there are some breaking changes in 0.11.0, you may need to make a data migration before upgrade and one use case check, please see this changelog for details, if you are new to use 0.11.0 or later, please ignore this comment.

Introduce

Ecto 3.x adapter for Alibaba Tablestore, this is built on top of ex_aliyun_ots to implement Ecto.Adapter and Ecto.Adapter.Schema behaviours.

Supported features:

Implement Tablestore row related functions in EctoTablestore.Repo module, please see document for details:

Migration

Provide a simple migration to create or drop the table, the secondary index and the search index, please see EctoTablestore.Migration for details.

Usage

1, Configure My instance(s) information of Alibaba Tablestore product.

use Mix.Config

# config for `ex_aliyun_ots`

config :ex_aliyun_ots, MyInstance,
  name: "MY_INSTANCE_NAME",
  endpoint: "MY_INSTANCE_ENDPOINT",
  access_key_id: "MY_OTS_ACCESS_KEY",
  access_key_secret: "MY_OTS_ACCESS_KEY_SECRET"

config :ex_aliyun_ots,
  instances: [MyInstance]

# config for `ecto_tablestore`

config :my_otp_app, EctoTablestore.MyRepo,
  instance: MyInstance

2, Create the EctoTablestore.MyRepo module mentioned earlier in the configuration, use EctoTablestore.Repo and set required otp_app option with your OTP application’s name.

defmodule EctoTablestore.MyRepo do
  use EctoTablestore.Repo,
    otp_app: :my_otp_app
end

3, Each repository in Ecto defines a start_link/0 function that needs to be invoked before using the repository. In general, this function is not called directly, but used as part of your application supervision tree.

Add EctoTablestore.MyRepo into your application start callback that defines and start your supervisor. You just need to edit start/2 function to start the repo as a supervisor on your application’s supervisor:

def start(_type, _args) do
  children = [
    {EctoTablestore.MyRepo, []}
  ]

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
end

4, After finish the above preparation, we can use MyRepo to operate data store.

Integrate Hashids

Base on unique integer of atomic-increment sequence, provides a way to simply integrate Hashids to generate your hash ids as the primary key when insert row(s), for Repo.insert/2 or Repo.batch_write/1.

Add a dependency to your Mix project

{:hashids, "~> 2.0"}

Use in schema

defmodule Module do
  use EctoTablestore.Schema

  schema "table_name" do
    field(:id, Ecto.Hashids, primary_key: true, autogenerate: true,
      hashids: [salt: "123", min_len: 2, alphabet: "..."])
    field(:content, :string)
  end

end

Options:

Notice: the salt, min_len and alphabet of hashids options also can be configured in the :ecto_tablestore config for each defined schema, for example:

config :ecto_tablestore,
  hashids: [
    {Module, salt: "...", min_len: 16, alphabet: "..."},
    ...
  ]

Use in migration

Use :hashids as a type in add operation to define the partition key, the principle behind this will use ecto_tablestore_default_seq as a global default table to maintain the sequences of all tables, if ecto_tablestore_default_seq is not existed, there will create this, if it is existed, please ignore the “OTSObjectAlreadyExist” error of requested table already exists.

defmodule EctoTablestore.TestRepo.Migrations.TestHashids do
  use EctoTablestore.Migration

  def change do
    create table("table_name") do
      add :id, :hashids, partition_key: true
      add :oid, :integer
    end
  end

end

Options:

References

Alibaba Tablestore product official references:

License

This project is licensed under the MIT license. Copyright (c) 2018- Xin Zou.