前書き
DynamoDB は空の値を登録しようとすると ValidationException が発生してしまいます。しかし、DynamoDB は NULL 型をサポートしており、DynamoDB document client には空の値を NULL 型に変換するためのオプトイン機能が存在します。
「んなこともう知ってるよハゲ!
」
という方は大丈夫なのですが、空の値を代入せずに済む仕様にしたり、空の値 (空文字) をスペースで登録したり、空の値を NULL 型に変換するためのオプトイン機能を自前で実装したり・・・という記事も存在します![]()
※ オプトイン機能未実装時に投稿された記事も多々有りましたので仕方ありません。
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 のコンストラクタに渡すだけです![]()
こちらの Issue により、
jeskew commented on 24 Jan
@brandonmbanks Converting empty values to
nullis now an opt-in feature of the document client; you can pass a booleanconvertEmptyValuesoption 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 が作成され、マージされたことによりオプトイン機能が使えるようになりました![]()
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}としてレンダリングします。