View Source EctoTablestore.Schema (ecto_tablestore v0.15.1)
Define an Ecto schema for Tablestore product use, there are some reasons make we do some wrapper base on Ecto.Schema:
- Since Tablestore product does not support(also not recommend) the first primary key(aka the partition key) autogenerated
in server side, e.g. an auto increment sequence integer in server is not supported, so we set
@primary_keyasfalseby default when useEctoTablestore.Schema. - Since Tablestore product's attribute column does not support DateTime or similar type in server side, so we use
@timestamps_optsto pre-configureEcto.Schema.timestamps/1macro as an integer type for the generatedinserted_atandupdated_atfields.
If you have explicitly set @primary_key and @timestamps_opts(or Ecto.Schema.timestamps/1) in the defined schema,
and then you can write use Ecto.Schema to make the schema definition more compatible in Ecto use, for example:
defmodule User do
use Ecto.Schema
@primary_key {:id, :id, autogenerate: false}
schema "user" do
field(:name, :string)
timestamps(
type: :integer,
autogenerate: {System, :os_time, [:second]}
)
end
endIt equals to:
defmodule User do
use EctoTablestore.Schema
schema "user" do
field(:id, :id, primary_key: true)
field(:name, :string)
timestamps()
end
endAbout primary key
- The primary key supports
:id(integer()) and:binary_id(binary()). - By default the
:primary_keyoption isfalse. - The first defined primary key by the written order in the
schemais the partition key. - Up to 4 primary key(s), it is limited by Tablestore product server side.
- Up to 1 primary key with
autogenerate: trueoption, it is limited by Tablestore product server side. - The primary key set with
autogenerate: truewill use the Tablestore product server's AUTO_INCREMENT feature. - If the partition key set as
autogenerate: trueis not allowed to take advantage of the AUTO_INCREMENT feature which it is limited by server, but there is a built-in implement to use theSequenceto achieve the same atomic increment operation inecto_tablestorelibrary.
Here is an example about the primary keys:
defmodule User do
use EctoTablestore.Schema
schema "user" do
field :outer_id, :binary_id, primary_key: true
field :internal_id, :id, primary_key: true, autogenerate: true
field :name, :string
field :desc
end
endBy default, if not explicitly set field type will process it as a :string type.
Summary
Functions
Due to the previous implements, this macro is reserved to make compatible when :hashids or EctoTablestore.Hashids
type defined (changes in 0.13.0).
Functions
Due to the previous implements, this macro is reserved to make compatible when :hashids or EctoTablestore.Hashids
type defined (changes in 0.13.0).
We may deprecate it in the future, please use Ecto.Schema.schema/2 instead.