LoginSignup
28
19

More than 5 years have passed since last update.

DynamoDB は空の値を登録できないけど DynamoDB document client のコンストラクタにオプションを渡すだけで空の値を NULL 型には変換できるよ?

Posted at

前書き

DynamoDB は空の値を登録しようとすると ValidationException が発生してしまいます。しかし、DynamoDB は NULL 型をサポートしており、DynamoDB document client には空の値を NULL 型に変換するためのオプトイン機能が存在します。

「んなこともう知ってるよハゲ!:anger:

という方は大丈夫なのですが、空の値を代入せずに済む仕様にしたり、空の値 (空文字) をスペースで登録したり、空の値を NULL 型に変換するためのオプトイン機能を自前で実装したり・・・という記事も存在します:scream:

※ オプトイン機能未実装時に投稿された記事も多々有りましたので仕方ありません。

DynamoDB を始める方がそういうものなのかと思ってしまわないようにここでご紹介したいと思います。

どうやるの?

Class: AWS.DynamoDB.DocumentClient — AWS SDK for JavaScript

Constructor Details

new AWS.DynamoDB.DocumentClient(options) ⇒ void

Creates a DynamoDB document client with a set of configuration options.

Options Hash (options):

  • params (map) — An optional map of parameters to bind to every request sent by this service object.
  • service (AWS.DynamoDB) — An optional pre-configured instance of the AWS.DynamoDB service object to use for requests. The object may bound parameters used by the document client.
  • convertEmptyValues (Boolean) — set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.

convertEmptyValues オプションを true にして DynamoDB document client のコンストラクタに渡すだけです:sunglasses:

こちらの Issue により、

jeskew commented on 24 Jan

@brandonmbanks Converting empty values to null is now an opt-in feature of the document client; you can pass a boolean convertEmptyValues option to the document client constructor to have it do so. I'll open a PR to improve the documentation of this feature.

空の値を null に変換することは document client のオプトイン機能になりました。convertEmptyValues (Boolean) オプションを document client のコンストラクタに渡すことで変換することができます。

こちらの Pull Request が作成され、マージされたことによりオプトイン機能が使えるようになりました:sparkles:

jeskew commented on 14 Dec 2016

This change causes the document client's marshaller to render empty strings, buffers, and sets as {NULL: true} instead of {S: ''}, {B: new Buffer('')}, {SS: []}, respectively.

/cc @chrisradek

この変更により、document client の marshaller は、空の strings、buffers、および sets を {S: ''}{B: new Buffer('')}{SS: []} の代わりに {NULL: true} としてレンダリングします。

28
19
1

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
28
19