LoginSignup
0
1

More than 5 years have passed since last update.

aws-recordでグローバルセカンダリインデックスの登録方法

Posted at

aws-recordいいですよね。

aws-recordでDynamoDBのmigrationを作成する際のグローバルセカンダリインデックスの登録方法がわからなかったので調べてみました。
(これに限らず、aws-recordのコードはコメントやサンプルが豊富で読みやすいです。)

以下のような感じで書きます。

class User
  include Aws::Record
  string_attr :name, hash_key: true
  string_attr :keywords, range_key: true
  string_attr :material
  string_attr :recepe
  string_attr :tips
  date_attr :updated_at

  global_secondary_index(
    :gsi_keywords,
    hash_key: :keywords,
    range_key: :name,
    projection: {
      projection_type: 'ALL'
    }
  )
end

# create db
cfg = Aws::Record::TableConfig.define do |t|
  t.model_class(User)
  t.read_capacity_units(5)
  t.write_capacity_units(2)

  t.global_secondary_index(:gsi_keywords) do |i|
    i.read_capacity_units(5)
    i.write_capacity_units(2)
  end
end

cfg.migrate!

この書き方はlib/aws-record/record/table_config.rbに記載されています。

テーブルのメタ情報を表示すると以下のようになります(伏せているところには実際のテーブル名が入ります)
今回はDynamoDBローカル で実行しましたが、実際のDynamoDBでも問題なく動くのではないでしょうか。

DynamoDB_Admin.jpg

改めてよく調べたらドキュメントにしっかり載ってましたね TableConfig

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1